diff --git a/src/etc/checkstyle/checkstyle-suppress.xml b/src/etc/checkstyle/checkstyle-suppress.xml index c8c2db08..726eb29a 100644 --- a/src/etc/checkstyle/checkstyle-suppress.xml +++ b/src/etc/checkstyle/checkstyle-suppress.xml @@ -21,5 +21,5 @@ under the License. --> - + diff --git a/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java b/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java index 05f69704..72bac057 100644 --- a/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java +++ b/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java @@ -38,11 +38,15 @@ import org.apache.ivy.core.IvyPatternHelper; import org.apache.ivy.core.module.descriptor.Artifact; import org.apache.ivy.core.module.descriptor.DependencyArtifactDescriptor; import org.apache.ivy.core.module.descriptor.DependencyDescriptor; +import org.apache.ivy.core.module.descriptor.DependencyDescriptorMediator; import org.apache.ivy.core.module.descriptor.ExcludeRule; import org.apache.ivy.core.module.descriptor.ModuleDescriptor; +import org.apache.ivy.core.module.descriptor.OverrideDependencyDescriptorMediator; import org.apache.ivy.core.module.id.ModuleRevisionId; +import org.apache.ivy.core.module.id.ModuleRules; import org.apache.ivy.core.settings.IvySettings; import org.apache.ivy.core.settings.IvyVariableContainer; +import org.apache.ivy.plugins.matcher.MapMatcher; import org.apache.ivy.plugins.parser.m2.PomWriterOptions.ConfigurationScopeMapping; import org.apache.ivy.plugins.parser.m2.PomWriterOptions.ExtraDependency; import org.apache.ivy.util.ConfigurationUtils; @@ -112,6 +116,7 @@ public final class PomModuleDescriptorWriter { boolean dependencyManagement = false; boolean dependenciesPrinted = false; + boolean overridesPrinted = false; int lastIndent = 0; int indent = 0; @@ -135,14 +140,26 @@ public final class PomModuleDescriptorWriter { dependencyManagement = false; } - if (line.contains("") && !dependenciesPrinted && !dependencyManagement) { - printDependencies(md, out, options, indent, false); - dependenciesPrinted = true; + if (line.contains("")) { + if (!overridesPrinted && dependencyManagement) { + printOverrides(md, out, indent, false); + overridesPrinted = true; + } + if (!dependenciesPrinted && !dependencyManagement) { + printDependencies(md, out, options, indent, false); + dependenciesPrinted = true; + } } - if (line.contains("") && !dependenciesPrinted) { - printDependencies(md, out, options, lastIndent, true); - dependenciesPrinted = true; + if (line.contains("")) { + if (!overridesPrinted) { + printOverrides(md, out, lastIndent, true); + overridesPrinted = true; + } + if (!dependenciesPrinted) { + printDependencies(md, out, options, lastIndent, true); + dependenciesPrinted = true; + } } } out.println(line); @@ -342,8 +359,59 @@ public final class PomModuleDescriptorWriter { out.println(""); } - private static DependencyDescriptor[] getDependencies(ModuleDescriptor md, - PomWriterOptions options) { + private static void printOverrides(ModuleDescriptor md, PrintWriter out, int indent, boolean container) { + ModuleRules mr = md.getAllDependencyDescriptorMediators(); + Map m = new LinkedHashMap<>(mr.getAllRules()); + + m.values().removeIf(value -> !(value instanceof OverrideDependencyDescriptorMediator)); + m.keySet().removeIf(key -> { + String artifactId = key.getAttributes().get("module"); + return artifactId == null || artifactId.equals("*"); + }); + m.keySet().removeIf(key -> { + String groupId = key.getAttributes().get("organisation"); + return groupId == null || groupId.equals("*"); + }); + + if (m.isEmpty()) { + return; + } + + if (container) { + indent(out, indent); + out.println(""); + indent(out, indent * 2); + out.println(""); + } else { + indent /= 2; // is second-level child of + } + + for (Map.Entry entry : m.entrySet()) { + String artifactId = entry.getKey().getAttributes().get("module"); + String groupId = entry.getKey().getAttributes().get("organisation"); + String version = ((OverrideDependencyDescriptorMediator) entry.getValue()).getVersion(); + + indent(out, indent * 3); + out.println(""); + indent(out, indent * 4); + out.println("" + groupId + ""); + indent(out, indent * 4); + out.println("" + artifactId + ""); + indent(out, indent * 4); + out.println("" + version + ""); + indent(out, indent * 3); + out.println(""); + } + + if (container) { + indent(out, indent * 2); + out.println(""); + indent(out, indent); + out.println(""); + } + } + + private static DependencyDescriptor[] getDependencies(ModuleDescriptor md, PomWriterOptions options) { String[] confs = ConfigurationUtils.replaceWildcards(options.getConfs(), md); List result = new ArrayList<>(); diff --git a/test/java/org/apache/ivy/core/module/descriptor/IvyMakePomTest.java b/test/java/org/apache/ivy/core/module/descriptor/IvyMakePomTest.java index 430f4dbd..0395d527 100644 --- a/test/java/org/apache/ivy/core/module/descriptor/IvyMakePomTest.java +++ b/test/java/org/apache/ivy/core/module/descriptor/IvyMakePomTest.java @@ -100,6 +100,73 @@ public class IvyMakePomTest { assertTrue("Some expected dependencies " + expectedPomArtifactIds + " were not found in the generated POM file", expectedPomArtifactIds.isEmpty()); } + /** + * Test case for IVY-1653. + */ + @Test + public void testMakePom1653() throws Exception { + File ivyFile = workdir.newFile("ivy-1653.xml"); + writeLines(ivyFile, "UTF-8", Arrays.asList( + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + "" + )); + + File pomFile = workdir.newFile("ivy-1653.pom"); + + IvyMakePom task = new IvyMakePom(); + task.setIvyFile(ivyFile); + task.setPomFile(pomFile); + task.setPrintIvyInfo(false); + task.setProject(project); + + IvyMakePom.Mapping mapping = task.createMapping(); + mapping.setConf("default"); + mapping.setScope("compile"); + + task.execute(); + + String[] expect = { + "", + "", + "", + " 4.0.0", + " org", + " name", + " jar", + " 1.0.0-SNAPSHOT", + " ", + " ", + " ", + " org.aspectj", + " aspectjrt", + " 1.9.24", + " ", + " ", + " ", + " ", + " ", + " org.springframework", + " spring-aop", + " 6.2.9", + " compile", + " ", + " ", + "", + "" + }; + + assertEquals(String.join(System.lineSeparator(), expect), readFileToString(pomFile, "UTF-8")); + } + @Test public void testMakePomWithTemplate() throws Exception { File ivyFile = workdir.newFile("ivy.xml"); @@ -173,6 +240,92 @@ public class IvyMakePomTest { assertEquals(String.join(System.lineSeparator(), expect), readFileToString(pomFile, "UTF-8")); } + /** + * Test case for IVY-1653. + */ + @Test + public void testMakePomWithTemplate1653() throws Exception { + File ivyFile = workdir.newFile("ivy.xml"); + writeLines(ivyFile, "UTF-8", Arrays.asList( + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + "" + )); + + File pomFile = workdir.newFile("ivy.pom"); + + File templateFile = workdir.newFile("the.pom"); + writeLines(templateFile, "UTF-8", Arrays.asList( + "", + " ${ivy.pom.groupId}", + " ${ivy.pom.artifactId}", + " ${ivy.pom.version}", + " ", + " ", + " ", + " org.aspectj", + " aspectjrt", + " 1.9.24", + " ", + " ", + " ", + "" + )); + + IvyMakePom task = new IvyMakePom(); + task.setIvyFile(ivyFile); + task.setPomFile(pomFile); + task.setPrintIvyInfo(false); + task.setProject(project); + task.setTemplateFile(templateFile); + + IvyMakePom.Mapping mapping = task.createMapping(); + mapping.setConf("default"); + mapping.setScope("compile"); + + task.execute(); + + String[] expect = { + "", + " org", + " name", + " 1.0.0-SNAPSHOT", + " ", + " ", + " ", + " org.aspectj", + " aspectjrt", + " 1.9.24", + " ", + " ", + " org.springframework", + " spring-core", + " 6.2.19", + " ", + " ", + " ", + " ", + " ", + " org.springframework", + " spring-aop", + " 6.2.9", + " compile", + " ", + " ", + "", + "" + }; + + assertEquals(String.join(System.lineSeparator(), expect), readFileToString(pomFile, "UTF-8")); + } + /** * Test case for IVY-1667. */