From 6c28e170095d383ae574304b3c23a9988f205a16 Mon Sep 17 00:00:00 2001 From: Juul Hobert Date: Fri, 29 Mar 2024 17:17:45 +0100 Subject: [PATCH 1/4] [MNG-7344] Integration test for effective pom with imports --- .../MavenITmng7344EffectivePomPluginTest.java | 55 +++++++++++++ .../apache/maven/it/TestSuiteOrdering.java | 1 + .../bom-intermediate/pom.xml | 22 +++++ .../mng-7344-effective-pom/bom-parent/pom.xml | 20 +++++ .../mng-7344-effective-pom/consumer/pom.xml | 65 +++++++++++++++ .../mng-7344-effective-pom/dep-x/pom.xml | 9 ++ .../mng-7344-effective-pom/plugin/pom.xml | 76 +++++++++++++++++ .../main/java/testmojo/EffectivePomMojo.java | 82 +++++++++++++++++++ 8 files changed, 330 insertions(+) create mode 100644 core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java create mode 100644 core-it-suite/src/test/resources/mng-7344-effective-pom/bom-intermediate/pom.xml create mode 100644 core-it-suite/src/test/resources/mng-7344-effective-pom/bom-parent/pom.xml create mode 100644 core-it-suite/src/test/resources/mng-7344-effective-pom/consumer/pom.xml create mode 100644 core-it-suite/src/test/resources/mng-7344-effective-pom/dep-x/pom.xml create mode 100644 core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/pom.xml create mode 100644 core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/src/main/java/testmojo/EffectivePomMojo.java diff --git a/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java new file mode 100644 index 000000000..b3dc2421b --- /dev/null +++ b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.it; + +import java.io.File; + +import org.apache.maven.shared.verifier.Verifier; +import org.apache.maven.shared.verifier.util.ResourceExtractor; +import org.junit.jupiter.api.Test; + +public class MavenITmng7344EffectivePomPluginTest extends AbstractMavenIntegrationTestCase { + public MavenITmng7344EffectivePomPluginTest() { + super("[4.0.0-alpha-13,)"); + } + + @Test + public void testIt() throws Exception { + File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-7344-effective-pom"); + + Verifier verifier = newVerifier(new File(testDir, "plugin").getAbsolutePath(), "remote"); + verifier.addCliArgument("install"); + verifier.execute(); + verifier.verifyErrorFreeLog(); + + for (String childFolder : new String[] {"dep-x", "bom-parent", "bom-intermediate"}) { + verifier = newVerifier(new File(testDir, childFolder).getAbsolutePath()); + verifier.addCliArgument("install"); + verifier.execute(); + verifier.verifyErrorFreeLog(); + } + + verifier = newVerifier(new File(testDir, "consumer").getAbsolutePath()); + verifier.setForkJvm(true); + verifier.addCliArgument("compile"); + verifier.execute(); + + verifier.verifyTextInLog("[INFO] bom-intermediate-1.0-SNAPSHOT.pom @ 8:3"); + } +} diff --git a/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java b/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java index 45e97d647..414fb56bb 100644 --- a/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java +++ b/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java @@ -777,6 +777,7 @@ public TestSuiteOrdering() { suite.addTestSuite(MavenIT0010DependencyClosureResolutionTest.class); suite.addTestSuite(MavenIT0009GoalConfigurationTest.class); suite.addTestSuite(MavenIT0008SimplePluginTest.class); + suite.addTestSuite(MavenITmng7344EffectivePomPluginTest.class); /* * Add tests in reverse alpha order above. */ diff --git a/core-it-suite/src/test/resources/mng-7344-effective-pom/bom-intermediate/pom.xml b/core-it-suite/src/test/resources/mng-7344-effective-pom/bom-intermediate/pom.xml new file mode 100644 index 000000000..c45bfd4f3 --- /dev/null +++ b/core-it-suite/src/test/resources/mng-7344-effective-pom/bom-intermediate/pom.xml @@ -0,0 +1,22 @@ + + + + 4.0.0 + + org.apache.maven.its.mng7344 + bom-intermediate + 1.0-SNAPSHOT + pom + + + + + org.apache.maven.its.mng7344 + bom-parent + 1.0-SNAPSHOT + pom + import + + + + diff --git a/core-it-suite/src/test/resources/mng-7344-effective-pom/bom-parent/pom.xml b/core-it-suite/src/test/resources/mng-7344-effective-pom/bom-parent/pom.xml new file mode 100644 index 000000000..88a09bbb8 --- /dev/null +++ b/core-it-suite/src/test/resources/mng-7344-effective-pom/bom-parent/pom.xml @@ -0,0 +1,20 @@ + + + + 4.0.0 + + org.apache.maven.its.mng7344 + bom-parent + 1.0-SNAPSHOT + pom + + + + + org.apache.maven.its.mng7344 + dep-x + 2 + + + + diff --git a/core-it-suite/src/test/resources/mng-7344-effective-pom/consumer/pom.xml b/core-it-suite/src/test/resources/mng-7344-effective-pom/consumer/pom.xml new file mode 100644 index 000000000..fbe9cacba --- /dev/null +++ b/core-it-suite/src/test/resources/mng-7344-effective-pom/consumer/pom.xml @@ -0,0 +1,65 @@ + + + + 4.0.0 + + org.apache.maven.its.mng7344 + consumer + 1.0-SNAPSHOT + + + + + org.apache.maven.its.mng7344 + bom-intermediate + 1.0-SNAPSHOT + pom + import + + + + + + + org.apache.maven.its.mng7344 + dep-x + + + + + + + org.apache.maven.its.mng7344 + mng-7344-maven-plugin + 1.0-SNAPSHOT + + + effective-pom + + effective-pom + + compile + + + + + + + diff --git a/core-it-suite/src/test/resources/mng-7344-effective-pom/dep-x/pom.xml b/core-it-suite/src/test/resources/mng-7344-effective-pom/dep-x/pom.xml new file mode 100644 index 000000000..ffc681473 --- /dev/null +++ b/core-it-suite/src/test/resources/mng-7344-effective-pom/dep-x/pom.xml @@ -0,0 +1,9 @@ + + + + 4.0.0 + + org.apache.maven.its.mng7344 + dep-x + 2 + diff --git a/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/pom.xml b/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/pom.xml new file mode 100644 index 000000000..5f3afc1dd --- /dev/null +++ b/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/pom.xml @@ -0,0 +1,76 @@ + + + + 4.0.0 + + org.apache.maven.its.mng7344 + mng-7344-maven-plugin + 1.0-SNAPSHOT + maven-plugin + + + ${mavenVersion} + + + + 8 + 3.6.3 + 3.9.0 + + + + + org.apache.maven + maven-plugin-api + ${mavenVersion} + provided + + + org.apache.maven + maven-core + ${mavenVersion} + provided + + + org.apache.maven + maven-model + ${mavenVersion} + provided + + + org.apache.maven.plugin-tools + maven-plugin-annotations + ${mavenPluginToolsVersion} + provided + + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + ${mavenPluginToolsVersion} + + + + + diff --git a/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/src/main/java/testmojo/EffectivePomMojo.java b/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/src/main/java/testmojo/EffectivePomMojo.java new file mode 100644 index 000000000..6bf90cc1f --- /dev/null +++ b/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/src/main/java/testmojo/EffectivePomMojo.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package testmojo; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.lang.reflect.Field; +import java.util.List; + +import org.apache.maven.model.Dependency; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; + +@Mojo(name = "effective-pom", aggregator = true) +public class EffectivePomMojo extends AbstractMojo { + + @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true) + private List projects; + + public void execute() throws MojoExecutionException, MojoFailureException { + projects.stream() + .map(MavenProject::getDependencyManagement) + .map(depMgt -> depMgt != null ? depMgt.getDependencies() : null) + .filter(dependencies -> dependencies != null) + .flatMap(List::stream) + .forEach(dependency -> { + getLog().info("" + dependency); + try { + getLog().info(getImportedFrom(dependency)); + } catch (Exception ex) { + getLog().error("Failed to get importedFrom"); + } + }); + } + + private String getImportedFrom(Dependency dependency) throws Exception { + Field delegateField = Class.forName("org.apache.maven.model.BaseObject").getDeclaredField("delegate"); + delegateField.setAccessible(true); + Object delegate = delegateField.get(dependency); + delegateField.setAccessible(false); + Object importedFrom = delegate.getClass().getMethod("getImportedFrom").invoke(delegate); + + return ("" + importedFrom).replaceAll("^.*[\\\\/]", ""); + } +} From 50263d6e5e94746e19e082a6654040796e4bc976 Mon Sep 17 00:00:00 2001 From: Juul Hobert Date: Fri, 12 Jul 2024 15:03:18 +0200 Subject: [PATCH 2/4] Removed line ref, added prefix for matching the right log part --- .../apache/maven/it/MavenITmng7344EffectivePomPluginTest.java | 2 +- .../plugin/src/main/java/testmojo/EffectivePomMojo.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java index b3dc2421b..270bfd104 100644 --- a/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java +++ b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java @@ -50,6 +50,6 @@ public void testIt() throws Exception { verifier.addCliArgument("compile"); verifier.execute(); - verifier.verifyTextInLog("[INFO] bom-intermediate-1.0-SNAPSHOT.pom @ 8:3"); + verifier.verifyTextInLog("[IMPORTED FROM] bom-intermediate-1.0-SNAPSHOT.pom"); } } diff --git a/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/src/main/java/testmojo/EffectivePomMojo.java b/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/src/main/java/testmojo/EffectivePomMojo.java index 6bf90cc1f..c27a0ef0b 100644 --- a/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/src/main/java/testmojo/EffectivePomMojo.java +++ b/core-it-suite/src/test/resources/mng-7344-effective-pom/plugin/src/main/java/testmojo/EffectivePomMojo.java @@ -63,7 +63,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { .forEach(dependency -> { getLog().info("" + dependency); try { - getLog().info(getImportedFrom(dependency)); + getLog().info("[IMPORTED FROM] " + getImportedFrom(dependency)); } catch (Exception ex) { getLog().error("Failed to get importedFrom"); } From debf6607cded76c17072c2a9edbd6c9f5ec633b4 Mon Sep 17 00:00:00 2001 From: Juul Hobert Date: Fri, 2 Aug 2024 14:27:26 +0200 Subject: [PATCH 3/4] Moved test to the top in the test suite ordering --- .../src/test/java/org/apache/maven/it/TestSuiteOrdering.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java b/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java index 414fb56bb..c300c6deb 100644 --- a/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java +++ b/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java @@ -120,6 +120,7 @@ public TestSuiteOrdering() { * the tests are to finishing. Newer tests are also more likely to fail, so this is * a fail fast technique as well. */ + suite.addTestSuite(MavenITmng7344EffectivePomPluginTest.class); suite.addTestSuite(MavenITmng8123BuildCacheTest.class); suite.addTestSuite(MavenITmng8106OverlappingDirectoryRolesTest.class); suite.addTestSuite(MavenITmng8005IdeWorkspaceReaderUsedTest.class); @@ -777,7 +778,6 @@ public TestSuiteOrdering() { suite.addTestSuite(MavenIT0010DependencyClosureResolutionTest.class); suite.addTestSuite(MavenIT0009GoalConfigurationTest.class); suite.addTestSuite(MavenIT0008SimplePluginTest.class); - suite.addTestSuite(MavenITmng7344EffectivePomPluginTest.class); /* * Add tests in reverse alpha order above. */ From 7be71e69a10ba119a1e9b296a0d5d7f52c64c936 Mon Sep 17 00:00:00 2001 From: Juul Hobert Date: Fri, 2 Aug 2024 14:29:03 +0200 Subject: [PATCH 4/4] Updated version range to start from beta-4 for mng7334 test --- .../apache/maven/it/MavenITmng7344EffectivePomPluginTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java index 270bfd104..5a141d839 100644 --- a/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java +++ b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7344EffectivePomPluginTest.java @@ -26,7 +26,7 @@ public class MavenITmng7344EffectivePomPluginTest extends AbstractMavenIntegrationTestCase { public MavenITmng7344EffectivePomPluginTest() { - super("[4.0.0-alpha-13,)"); + super("[4.0.0-beta-4-SNAPSHOT,)"); } @Test