Skip to content

Commit d566cc1

Browse files
committed
[GH-11456] Add validation for mixins in consumer POM without flattening
Maven now fails with a clear error message when a POM contains mixins but consumer POM flattening is disabled. Mixins require model version 4.2.0 and cannot be part of the consumer POM, so they must be removed during transformation through flattening. Changes: - Added validation in DefaultConsumerPomBuilder to check for mixins when flattening is disabled and throw MavenException with helpful message - Added integration test MavenITgh11456MixinsConsumerPomTest with two scenarios: 1. testMixinsWithoutFlattening: Verifies build fails with proper error message 2. testMixinsWithFlattening: Verifies build succeeds and mixins are removed from consumer POM when flattening is enabled - Created test resources with POMs using mixins and local repository The error message guides users to either enable flattening by setting maven.consumer.pom.flatten=true or remove mixins from their POM. Fixes #11456
1 parent 76ad77c commit d566cc1

File tree

9 files changed

+257
-2
lines changed

9 files changed

+257
-2
lines changed

impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.maven.api.model.Profile;
4141
import org.apache.maven.api.model.Repository;
4242
import org.apache.maven.api.model.Scm;
43+
import org.apache.maven.api.services.MavenException;
4344
import org.apache.maven.api.services.ModelBuilder;
4445
import org.apache.maven.api.services.ModelBuilderException;
4546
import org.apache.maven.api.services.ModelBuilderRequest;
@@ -75,6 +76,16 @@ public Model build(RepositorySystemSession session, MavenProject project, ModelS
7576
Model model = project.getModel().getDelegate();
7677
boolean flattenEnabled = Features.consumerPomFlatten(session.getConfigProperties());
7778

79+
// Check if mixins are present without flattening enabled
80+
if (!flattenEnabled && !model.getMixins().isEmpty()) {
81+
throw new MavenException("The consumer POM for "
82+
+ project.getId()
83+
+ " cannot be created because the POM contains mixins, which require flattening to be enabled. "
84+
+ "Mixins cannot be part of the consumer POM and must be removed during transformation. "
85+
+ "Please enable flattening by setting the property 'maven.consumer.pom.flatten=true' "
86+
+ "or remove the mixins from your POM.");
87+
}
88+
7889
// Check if consumer POM flattening is disabled
7990
if (!flattenEnabled) {
8091
// When flattening is disabled, treat non-POM projects like parent POMs
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.Reader;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
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.assertTrue;
31+
32+
/**
33+
* This is a test set for <a href="https://github.com/apache/maven/issues/11456">GH-11456</a>.
34+
* @since 4.1.0
35+
*/
36+
class MavenITgh11456MixinsConsumerPomTest extends AbstractMavenIntegrationTestCase {
37+
38+
/**
39+
* Verify that Maven fails when a POM has non-empty mixins without flattening being enabled.
40+
*/
41+
@Test
42+
void testMixinsWithoutFlattening() throws Exception {
43+
Path basedir = extractResources("/gh-11456-mixins-consumer-pom").toPath();
44+
45+
Verifier verifier = newVerifier(basedir.toString());
46+
verifier.addCliArgument("-Dmaven.repo.local=" + basedir.resolve("repo").toString());
47+
verifier.addCliArgument("package");
48+
verifier.setLogFileName("log-without-flattening.txt");
49+
try {
50+
verifier.execute();
51+
} catch (VerificationException e) {
52+
// Expected to fail due to mixins without flattening
53+
}
54+
55+
verifier.verifyTextInLog("cannot be created because the POM contains mixins");
56+
verifier.verifyTextInLog("maven.consumer.pom.flatten=true");
57+
}
58+
59+
/**
60+
* Verify that Maven succeeds when mixins are used with flattening enabled.
61+
*/
62+
@Test
63+
void testMixinsWithFlattening() throws Exception {
64+
Path basedir = extractResources("/gh-11456-mixins-consumer-pom-flattened").toPath();
65+
66+
Verifier verifier = newVerifier(basedir.toString());
67+
verifier.addCliArgument("-Dmaven.repo.local=" + basedir.resolve("repo").toString());
68+
verifier.addCliArgument("-Dmaven.consumer.pom.flatten=true");
69+
verifier.addCliArgument("package");
70+
verifier.setLogFileName("log-with-flattening.txt");
71+
verifier.execute();
72+
verifier.verifyErrorFreeLog();
73+
74+
// Verify consumer POM was created
75+
Path consumerPom = basedir.resolve(Paths.get(
76+
"target",
77+
"project-local-repo",
78+
"org.apache.maven.its.gh11456",
79+
"mixins-consumer-pom-flattened",
80+
"1.0",
81+
"mixins-consumer-pom-flattened-1.0-consumer.pom"));
82+
assertTrue(Files.exists(consumerPom), "consumer pom not found at " + consumerPom);
83+
84+
// Verify mixins are removed from consumer POM
85+
Model consumerPomModel;
86+
try (Reader r = Files.newBufferedReader(consumerPom)) {
87+
consumerPomModel = new MavenStaxReader().read(r);
88+
}
89+
assertTrue(
90+
consumerPomModel.getMixins().isEmpty(),
91+
"Mixins should be removed from consumer POM when flattening is enabled");
92+
}
93+
}
94+

its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng5102MixinsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void testWithGav() throws Exception {
9090
verifier = newVerifier(new File(testDir, "project").getAbsolutePath());
9191
verifier.setAutoclean(false);
9292
verifier.deleteDirectory("target");
93-
verifier.addCliArgument("install");
93+
verifier.addCliArguments("install", "-Dmaven.consumer.pom.flatten");
9494
verifier.execute();
9595
verifier.verifyErrorFreeLog();
9696

@@ -126,7 +126,7 @@ public void testWithClassifier() throws Exception {
126126
verifier = newVerifier(new File(testDir, "project").getAbsolutePath());
127127
verifier.setAutoclean(false);
128128
verifier.deleteDirectory("target");
129-
verifier.addCliArgument("install");
129+
verifier.addCliArguments("install", "-Dmaven.consumer.pom.flatten");
130130
verifier.execute();
131131
verifier.verifyErrorFreeLog();
132132

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" root="true" xsi:schemaLocation="http://maven.apache.org/POM/4.2.0 http://maven.apache.org/xsd/maven-4.2.0.xsd">
21+
<modelVersion>4.2.0</modelVersion>
22+
<groupId>org.apache.maven.its.gh11456</groupId>
23+
<artifactId>mixins-consumer-pom-flattened</artifactId>
24+
<version>1.0</version>
25+
<packaging>jar</packaging>
26+
27+
<name>Maven Integration Test :: GH-11456 :: Mixins with Consumer POM Flattened</name>
28+
<description>Test that Maven succeeds when mixins are used with flattening enabled.</description>
29+
30+
<properties>
31+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
32+
</properties>
33+
34+
<mixins>
35+
<mixin>
36+
<groupId>org.apache.maven.its.gh11456</groupId>
37+
<artifactId>test-mixin-flattened</artifactId>
38+
<version>1.0</version>
39+
</mixin>
40+
</mixins>
41+
</project>
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.2.0">
3+
<modelVersion>4.2.0</modelVersion>
4+
<groupId>org.apache.maven.its.gh11456</groupId>
5+
<artifactId>test-mixin-flattened</artifactId>
6+
<version>1.0</version>
7+
<packaging>pom</packaging>
8+
9+
<properties>
10+
<test.property>from-mixin-flattened</test.property>
11+
</properties>
12+
</project>
13+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
public class Test {}
20+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" root="true" xsi:schemaLocation="http://maven.apache.org/POM/4.2.0 http://maven.apache.org/xsd/maven-4.2.0.xsd">
21+
<modelVersion>4.2.0</modelVersion>
22+
<groupId>org.apache.maven.its.gh11456</groupId>
23+
<artifactId>mixins-consumer-pom</artifactId>
24+
<version>1.0</version>
25+
<packaging>jar</packaging>
26+
27+
<name>Maven Integration Test :: GH-11456 :: Mixins with Consumer POM</name>
28+
<description>Test that Maven fails when mixins are used without flattening enabled.</description>
29+
30+
<properties>
31+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
32+
</properties>
33+
34+
<mixins>
35+
<mixin>
36+
<groupId>org.apache.maven.its.gh11456</groupId>
37+
<artifactId>test-mixin</artifactId>
38+
<version>1.0</version>
39+
</mixin>
40+
</mixins>
41+
</project>
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.2.0">
3+
<modelVersion>4.2.0</modelVersion>
4+
<groupId>org.apache.maven.its.gh11456</groupId>
5+
<artifactId>test-mixin</artifactId>
6+
<version>1.0</version>
7+
<packaging>pom</packaging>
8+
9+
<properties>
10+
<test.property>from-mixin</test.property>
11+
</properties>
12+
</project>
13+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
public class Test {}
20+

0 commit comments

Comments
 (0)