|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.it; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.Reader; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | + |
| 26 | +import org.apache.maven.api.model.Model; |
| 27 | +import org.apache.maven.model.v4.MavenStaxReader; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 33 | + |
| 34 | +/** |
| 35 | + * Integration test for <a href="https://github.com/apache/maven/issues/11772">GH-11772</a>. |
| 36 | + * <p> |
| 37 | + * Verifies that when a parent+child project uses model version 4.1.0 namespace |
| 38 | + * (with subprojects, root), the installed consumer POMs are model version 4.0.0, |
| 39 | + * while the build POMs retain the original 4.1.0 content. |
| 40 | + * <p> |
| 41 | + * This ensures backward compatibility with Maven 3 and Gradle for consumer POMs |
| 42 | + * while Maven 4 builds can resolve the full-fidelity build POM. |
| 43 | + */ |
| 44 | +class MavenITgh11772ConsumerPom410Test extends AbstractMavenIntegrationTestCase { |
| 45 | + |
| 46 | + private static final String GROUP_ID = "org.apache.maven.its.gh11772"; |
| 47 | + |
| 48 | + @Test |
| 49 | + void testConsumerPomsAre400BuildPomsAre410() throws Exception { |
| 50 | + File basedir = extractResources("/gh-11772-consumer-pom-410"); |
| 51 | + |
| 52 | + Verifier verifier = newVerifier(basedir.getAbsolutePath()); |
| 53 | + verifier.deleteArtifacts(GROUP_ID); |
| 54 | + verifier.addCliArguments("install"); |
| 55 | + verifier.execute(); |
| 56 | + verifier.verifyErrorFreeLog(); |
| 57 | + |
| 58 | + // Verify parent consumer POM (main artifact) is 4.0.0 |
| 59 | + Path parentConsumerPom = |
| 60 | + Path.of(verifier.getArtifactPath(GROUP_ID, "parent", "1.0.0-SNAPSHOT", "pom")); |
| 61 | + assertTrue(Files.exists(parentConsumerPom), "Parent consumer POM should exist"); |
| 62 | + Model parentConsumer = readModel(parentConsumerPom); |
| 63 | + assertEquals("4.0.0", parentConsumer.getModelVersion(), "Parent consumer POM should be 4.0.0"); |
| 64 | + |
| 65 | + // Verify parent build POM retains 4.1.0 features |
| 66 | + Path parentBuildPom = |
| 67 | + Path.of(verifier.getArtifactPath(GROUP_ID, "parent", "1.0.0-SNAPSHOT", "pom", "build")); |
| 68 | + assertTrue(Files.exists(parentBuildPom), "Parent build POM should exist"); |
| 69 | + Model parentBuild = readModel(parentBuildPom); |
| 70 | + // Build POM should retain subprojects (4.1.0 feature) |
| 71 | + assertNotNull(parentBuild.getSubprojects(), "Build POM should retain subprojects"); |
| 72 | + assertTrue(!parentBuild.getSubprojects().isEmpty(), "Build POM should retain subprojects"); |
| 73 | + |
| 74 | + // Verify child consumer POM is 4.0.0 |
| 75 | + Path childConsumerPom = |
| 76 | + Path.of(verifier.getArtifactPath(GROUP_ID, "child", "1.0.0-SNAPSHOT", "pom")); |
| 77 | + assertTrue(Files.exists(childConsumerPom), "Child consumer POM should exist"); |
| 78 | + Model childConsumer = readModel(childConsumerPom); |
| 79 | + assertEquals("4.0.0", childConsumer.getModelVersion(), "Child consumer POM should be 4.0.0"); |
| 80 | + |
| 81 | + // Child consumer POM should have a parent reference (not flattened by default) |
| 82 | + assertNotNull(childConsumer.getParent(), "Child consumer POM should have a parent reference"); |
| 83 | + assertEquals(GROUP_ID, childConsumer.getParent().getGroupId()); |
| 84 | + assertEquals("parent", childConsumer.getParent().getArtifactId()); |
| 85 | + |
| 86 | + // Verify child build POM exists |
| 87 | + Path childBuildPom = |
| 88 | + Path.of(verifier.getArtifactPath(GROUP_ID, "child", "1.0.0-SNAPSHOT", "pom", "build")); |
| 89 | + assertTrue(Files.exists(childBuildPom), "Child build POM should exist"); |
| 90 | + } |
| 91 | + |
| 92 | + private static Model readModel(Path pomFile) throws Exception { |
| 93 | + try (Reader r = Files.newBufferedReader(pomFile)) { |
| 94 | + return new MavenStaxReader().read(r); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments