From 576d21abbead1c070d5a1b3c416e4f939b3f0b0b Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 1 Sep 2025 15:16:23 +0200 Subject: [PATCH 1/3] ReactorReader: prefer consumer POM from project-local repo when resolving POMs When only part of a reactor is built, ReactorReader mirrors artifacts into a project-local repo. If a module depends on another reactor module not in the current session, POM resolution may hit that repo. Today, the main POM path contains the build POM, which can include Maven-4 build-time constructs and be invalid as a repository POM (e.g., missing parent.*). As a result, dependency resolution can fail with fatal model errors. Teach ReactorReader to prefer the consumer POM (classifier=consumer) for POM artifacts when present in the project-local repo, falling back to the main POM otherwise. This avoids consuming a build POM from the project-local repo without changing the repo contents or layout. --- .../java/org/apache/maven/ReactorReader.java | 12 ++++ ...084ReactorReaderPreferConsumerPomTest.java | 55 +++++++++++++++++++ .../apache/maven/it/TestSuiteOrdering.java | 1 + .../a/pom.xml | 25 +++++++++ .../a/src/main/java/p/A.java | 25 +++++++++ .../b/pom.xml | 31 +++++++++++ .../pom.xml | 30 ++++++++++ 7 files changed, 179 insertions(+) create mode 100644 its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11084ReactorReaderPreferConsumerPomTest.java create mode 100644 its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/pom.xml create mode 100644 its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/p/A.java create mode 100644 its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/pom.xml create mode 100644 its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/pom.xml diff --git a/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java b/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java index db2efa77205a..fdca07ee8024 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java +++ b/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java @@ -330,6 +330,18 @@ private static boolean isTestArtifact(Artifact artifact) { } private File findInProjectLocalRepository(Artifact artifact) { + // Prefer the consumer POM when resolving POMs from the project-local repository, + // to avoid treating a build POM as a repository (consumer) POM. + if ("pom".equals(artifact.getExtension())) { + String classifier = artifact.getClassifier(); + if (classifier == null || classifier.isEmpty()) { + Path consumer = getArtifactPath( + artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(), "consumer", "pom"); + if (Files.isRegularFile(consumer)) { + return consumer.toFile(); + } + } + } Path target = getArtifactPath(artifact); return Files.isRegularFile(target) ? target.toFile() : null; } diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11084ReactorReaderPreferConsumerPomTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11084ReactorReaderPreferConsumerPomTest.java new file mode 100644 index 000000000000..da7dc3ccb986 --- /dev/null +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11084ReactorReaderPreferConsumerPomTest.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.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +/** + * This is a test set for GH-11084. + */ +class MavenITgh11084ReactorReaderPreferConsumerPomTest extends AbstractMavenIntegrationTestCase { + MavenITgh11084ReactorReaderPreferConsumerPomTest() { + super("[4.0.0-rc-2,)"); + } + + @Test + void partialReactorShouldResolveUsingConsumerPom() throws Exception { + File testDir = extractResources("/gh-11084-reactorreader-prefer-consumer-pom"); + + // First build module a to populate project-local-repo with artifacts including consumer POM + Verifier v1 = newVerifier(testDir.getAbsolutePath()); + v1.setAutoclean(false); + v1.addCliArguments("compile"); + v1.execute(); + v1.verifyErrorFreeLog(); + + // Now build only module b; ReactorReader should pick consumer POM from project-local-repo + Verifier v2 = newVerifier(testDir.getAbsolutePath()); + v2.setAutoclean(false); + v2.addCliArguments("-f", "b", "compile"); + v2.execute(); + v2.verifyErrorFreeLog(); + String log = v2.loadLogContent(); + assertFalse(log.contains("The POM for org.apache.maven.its.gh11084:a:jar:1.0.0-SNAPSHOT is invalid"), log); + } +} diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java index 23a6e332abb7..ecfec0145c65 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java @@ -103,6 +103,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(MavenITgh11084ReactorReaderPreferConsumerPomTest.class); suite.addTestSuite(MavenITgh10312TerminallyDeprecatedMethodInGuiceTest.class); suite.addTestSuite(MavenITgh10937QuotedPipesInMavenOptsTest.class); suite.addTestSuite(MavenITgh2532DuplicateDependencyEffectiveModelTest.class); diff --git a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/pom.xml b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/pom.xml new file mode 100644 index 000000000000..930df59e3265 --- /dev/null +++ b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/pom.xml @@ -0,0 +1,25 @@ + + + + + + a + jar + diff --git a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/p/A.java b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/p/A.java new file mode 100644 index 000000000000..1da3c9402fb4 --- /dev/null +++ b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/p/A.java @@ -0,0 +1,25 @@ +/* + * 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 p; + +public class A { + public static String v() { + return "A"; + } +} diff --git a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/pom.xml b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/pom.xml new file mode 100644 index 000000000000..b826855e3c87 --- /dev/null +++ b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/pom.xml @@ -0,0 +1,31 @@ + + + + + + + b + + + org.apache.maven.its.gh11084 + a + + + diff --git a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/pom.xml b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/pom.xml new file mode 100644 index 000000000000..068bf84c5f92 --- /dev/null +++ b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/pom.xml @@ -0,0 +1,30 @@ + + + + org.apache.maven.its.gh11084 + reactor-root + 1.0.0-SNAPSHOT + pom + + + a + b + + From 4cebc5cf77f16bbb881e70df6fe59280d4fa4c35 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 8 Sep 2025 20:30:11 +0000 Subject: [PATCH 2/3] Fix ReactorReader to prefer consumer POMs over build POMs When building modules in isolation (outside of the full reactor), the ReactorReader should prefer consumer POMs from the project-local repository over build POMs to avoid invalid POM elements like . The findModel method now: 1. First checks if the project is in the current reactor session 2. If not found, looks for a consumer POM in the project-local repository 3. Uses the same consumer POM preference logic as findInProjectLocalRepository This eliminates 'invalid POM' warnings when building modules in isolation while maintaining backward compatibility for reactor builds. Fixes gh-11084 --- .../java/org/apache/maven/ReactorReader.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java b/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java index fdca07ee8024..bad006df1097 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java +++ b/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java @@ -151,7 +151,29 @@ public List findVersions(Artifact artifact) { @Override public Model findModel(Artifact artifact) { MavenProject project = getProject(artifact); - return project == null ? null : project.getModel().getDelegate(); + if (project != null) { + return project.getModel().getDelegate(); + } + + // Check if we can find a consumer POM in the project-local repository + Artifact pomArtifact = new org.eclipse.aether.artifact.DefaultArtifact( + artifact.getGroupId(), artifact.getArtifactId(), "", "pom", artifact.getBaseVersion()); + File pomFile = findInProjectLocalRepository(pomArtifact); + if (pomFile != null && pomFile.exists()) { + try { + org.apache.maven.api.services.xml.XmlReaderRequest request = + org.apache.maven.api.services.xml.XmlReaderRequest.builder() + .path(pomFile.toPath()) + .strict(false) + .build(); + return new org.apache.maven.impl.DefaultModelXmlFactory().read(request); + } catch (Exception e) { + // If we can't read the POM, fall back to null + return null; + } + } + + return null; } // From 5c6eb8ac00e88d8f1071929af9772c076da67c23 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 11 Sep 2025 08:59:08 +0200 Subject: [PATCH 3/3] Fix --- .../java/org/apache/maven/ReactorReader.java | 24 +-------------- ...084ReactorReaderPreferConsumerPomTest.java | 12 +++----- .../a/pom.xml | 8 +++++ .../a/src/main/java/{p => a}/A.java | 11 +++++-- .../b/pom.xml | 1 + .../b/src/main/java/b/B.java | 30 +++++++++++++++++++ 6 files changed, 52 insertions(+), 34 deletions(-) rename its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/{p => a}/A.java (76%) create mode 100644 its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/src/main/java/b/B.java diff --git a/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java b/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java index bad006df1097..fdca07ee8024 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java +++ b/impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java @@ -151,29 +151,7 @@ public List findVersions(Artifact artifact) { @Override public Model findModel(Artifact artifact) { MavenProject project = getProject(artifact); - if (project != null) { - return project.getModel().getDelegate(); - } - - // Check if we can find a consumer POM in the project-local repository - Artifact pomArtifact = new org.eclipse.aether.artifact.DefaultArtifact( - artifact.getGroupId(), artifact.getArtifactId(), "", "pom", artifact.getBaseVersion()); - File pomFile = findInProjectLocalRepository(pomArtifact); - if (pomFile != null && pomFile.exists()) { - try { - org.apache.maven.api.services.xml.XmlReaderRequest request = - org.apache.maven.api.services.xml.XmlReaderRequest.builder() - .path(pomFile.toPath()) - .strict(false) - .build(); - return new org.apache.maven.impl.DefaultModelXmlFactory().read(request); - } catch (Exception e) { - // If we can't read the POM, fall back to null - return null; - } - } - - return null; + return project == null ? null : project.getModel().getDelegate(); } // diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11084ReactorReaderPreferConsumerPomTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11084ReactorReaderPreferConsumerPomTest.java index da7dc3ccb986..647333abfd98 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11084ReactorReaderPreferConsumerPomTest.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11084ReactorReaderPreferConsumerPomTest.java @@ -22,8 +22,6 @@ import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertFalse; - /** * This is a test set for GH-11084. */ @@ -38,18 +36,16 @@ void partialReactorShouldResolveUsingConsumerPom() throws Exception { // First build module a to populate project-local-repo with artifacts including consumer POM Verifier v1 = newVerifier(testDir.getAbsolutePath()); - v1.setAutoclean(false); - v1.addCliArguments("compile"); + v1.addCliArguments("clean", "package", "-X"); + v1.setLogFileName("log-1.txt"); v1.execute(); v1.verifyErrorFreeLog(); // Now build only module b; ReactorReader should pick consumer POM from project-local-repo Verifier v2 = newVerifier(testDir.getAbsolutePath()); - v2.setAutoclean(false); - v2.addCliArguments("-f", "b", "compile"); + v2.setLogFileName("log-2.txt"); + v2.addCliArguments("clean", "compile", "-f", "b", "-X"); v2.execute(); v2.verifyErrorFreeLog(); - String log = v2.loadLogContent(); - assertFalse(log.contains("The POM for org.apache.maven.its.gh11084:a:jar:1.0.0-SNAPSHOT is invalid"), log); } } diff --git a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/pom.xml b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/pom.xml index 930df59e3265..11ba754ca1f1 100644 --- a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/pom.xml +++ b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/pom.xml @@ -22,4 +22,12 @@ a jar + + + + org.codehaus.plexus + plexus-utils + 4.0.2 + + diff --git a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/p/A.java b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/a/A.java similarity index 76% rename from its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/p/A.java rename to its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/a/A.java index 1da3c9402fb4..6e4af4118b59 100644 --- a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/p/A.java +++ b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/a/src/main/java/a/A.java @@ -16,10 +16,15 @@ * specific language governing permissions and limitations * under the License. */ -package p; +package a; + +import java.io.IOException; +import java.nio.file.Path; + +import org.codehaus.plexus.util.io.CachingOutputStream; public class A { - public static String v() { - return "A"; + public static CachingOutputStream newCachingOutputStream(Path p) throws IOException { + return new CachingOutputStream(p); } } diff --git a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/pom.xml b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/pom.xml index b826855e3c87..67fdb8e0dd57 100644 --- a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/pom.xml +++ b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/pom.xml @@ -22,6 +22,7 @@ b + jar org.apache.maven.its.gh11084 diff --git a/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/src/main/java/b/B.java b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/src/main/java/b/B.java new file mode 100644 index 000000000000..8198e69cd272 --- /dev/null +++ b/its/core-it-suite/src/test/resources/gh-11084-reactorreader-prefer-consumer-pom/b/src/main/java/b/B.java @@ -0,0 +1,30 @@ +/* + * 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 b; + +import java.nio.file.Paths; + +import a.A; +import org.codehaus.plexus.util.io.CachingOutputStream; + +public class B { + public static void v() throws Exception { + try (CachingOutputStream is = A.newCachingOutputStream(Paths.get("."))) {} + } +}