Skip to content

Commit fe1d41a

Browse files
committed
[MNG-5910] Warn if both exists and missing file activation are set
1 parent 1f1a0f9 commit fe1d41a

File tree

6 files changed

+183
-7
lines changed

6 files changed

+183
-7
lines changed

api/maven-api-model/src/main/mdo/maven.mdo

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,14 +2892,15 @@
28922892
<name>missing</name>
28932893
<version>4.0.0+</version>
28942894
<type>String</type>
2895-
<description>The name of the file that must be missing to activate the
2896-
profile.</description>
2895+
<description>The name of the file that must be missing to activate the profile. Please note, that missing and exists
2896+
fields cannot be used together. Only one of them should be used at any one time.</description>
28972897
</field>
28982898
<field>
28992899
<name>exists</name>
29002900
<version>4.0.0+</version>
29012901
<type>String</type>
2902-
<description>The name of the file that must exist to activate the profile.</description>
2902+
<description>The name of the file that must exist to activate the profile. Please note, that missing and exists
2903+
fields cannot be used together. Only one of them should be used at any one time.</description>
29032904
</field>
29042905
</fields>
29052906
</class>

api/maven-api-settings/src/main/mdo/settings.mdo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,15 +975,17 @@
975975
<version>1.0.0+</version>
976976
<type>String</type>
977977
<description>
978-
The name of the file that should be missing to activate a profile.
978+
The name of the file that should be missing to activate a profile. Please note, that missing and exists
979+
fields cannot be used together. Only one of them should be used at any one time.
979980
</description>
980981
</field>
981982
<field>
982983
<name>exists</name>
983984
<version>1.0.0+</version>
984985
<type>String</type>
985986
<description>
986-
The name of the file that should exist to activate a profile.
987+
The name of the file that should exist to activate a profile. Please note, that missing and exists
988+
fields cannot be used together. Only one of them should be used at any one time.
987989
</description>
988990
</field>
989991
</fields>

maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/profile/FileProfileActivator.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,22 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model
7070
String path;
7171
boolean missing;
7272

73-
if (file.getExists() != null && !file.getExists().isEmpty()) {
73+
boolean hasExists = file.getExists() != null && !file.getExists().isEmpty();
74+
boolean hasMissing = file.getMissing() != null && !file.getMissing().isEmpty();
75+
if (hasExists) {
76+
if (hasMissing) {
77+
problems.add(
78+
BuilderProblem.Severity.WARNING,
79+
ModelProblem.Version.BASE,
80+
String.format(
81+
"Profile '%s' file activation conflict: Both 'missing' (%s) and 'exists' assertions are defined. "
82+
+ "The 'missing' assertion will be ignored. Please remove one assertion to resolve this conflict.",
83+
profile.getId(), file.getMissing()),
84+
file.getLocation("missing"));
85+
}
7486
path = file.getExists();
7587
missing = false;
76-
} else if (file.getMissing() != null && !file.getMissing().isEmpty()) {
88+
} else if (hasMissing) {
7789
path = file.getMissing();
7890
missing = true;
7991
} else {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.internal.impl.model;
20+
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
23+
import java.util.Map;
24+
25+
import org.apache.maven.api.Session;
26+
import org.apache.maven.api.services.BuilderProblem;
27+
import org.apache.maven.api.services.ModelBuilder;
28+
import org.apache.maven.api.services.ModelBuilderRequest;
29+
import org.apache.maven.api.services.ModelBuilderResult;
30+
import org.apache.maven.api.services.ModelProblem;
31+
import org.apache.maven.api.services.ModelSource;
32+
import org.apache.maven.internal.impl.standalone.ApiRunner;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertNotNull;
38+
import static org.junit.jupiter.api.Assertions.assertNull;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
40+
41+
/**
42+
*
43+
*/
44+
class ComplexActivationTest {
45+
46+
Session session;
47+
ModelBuilder builder;
48+
49+
@BeforeEach
50+
void setup() {
51+
session = ApiRunner.createSession();
52+
builder = session.getService(ModelBuilder.class);
53+
assertNotNull(builder);
54+
}
55+
56+
@Test
57+
void testAndConditionInActivation() throws Exception {
58+
ModelBuilderRequest request = ModelBuilderRequest.builder()
59+
.session(session)
60+
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
61+
.source(ModelSource.fromPath(getPom("complex")))
62+
.systemProperties(Map.of("myproperty", "test"))
63+
.build();
64+
ModelBuilderResult result = builder.newSession().build(request);
65+
assertNotNull(result);
66+
assertNotNull(result.getEffectiveModel());
67+
assertEquals("activated-1", result.getEffectiveModel().getProperties().get("profile.file"));
68+
assertNull(result.getEffectiveModel().getProperties().get("profile.miss"));
69+
}
70+
71+
@Test
72+
public void testConditionExistingAndMissingInActivation() throws Exception {
73+
ModelBuilderRequest request = ModelBuilderRequest.builder()
74+
.session(session)
75+
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
76+
.source(ModelSource.fromPath(getPom("complexExistsAndMissing")))
77+
.build();
78+
ModelBuilderResult result = builder.newSession().build(request);
79+
assertNotNull(result);
80+
assertTrue(result.getProblems().stream()
81+
.anyMatch(p -> p.getSeverity() == BuilderProblem.Severity.WARNING
82+
&& p.getMessage().contains("The 'missing' assertion will be ignored.")));
83+
}
84+
85+
private Path getPom(String name) {
86+
return Paths.get("src/test/resources/poms/factory/" + name + ".xml").toAbsolutePath();
87+
}
88+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>test</groupId>
9+
<artifactId>test</artifactId>
10+
<version>0.1-SNAPSHOT</version>
11+
<packaging>pom</packaging>
12+
13+
<properties>
14+
<my.filter.value>hello</my.filter.value>
15+
</properties>
16+
17+
<profiles>
18+
<profile>
19+
<id>two-conditions</id>
20+
<activation>
21+
<file>
22+
<exists>complex.xml</exists>
23+
</file>
24+
<property>
25+
<name>myproperty</name>
26+
<value>test</value>
27+
</property>
28+
</activation>
29+
<properties>
30+
<profile.file>activated-1</profile.file>
31+
</properties>
32+
</profile>
33+
<profile>
34+
<id>another-two-conditions</id>
35+
<activation>
36+
<property>
37+
<name>myproperty</name>
38+
<value>test</value>
39+
</property>
40+
<file>
41+
<missing>complex.xml</missing>
42+
</file>
43+
</activation>
44+
<properties>
45+
<profile.miss>activated-2</profile.miss>
46+
</properties>
47+
</profile>
48+
</profiles>
49+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>test</groupId>
9+
<artifactId>test</artifactId>
10+
<version>0.1-SNAPSHOT</version>
11+
<packaging>pom</packaging>
12+
13+
<profiles>
14+
<profile>
15+
<id>two-conditions</id>
16+
<activation>
17+
<file>
18+
<exists>simple.xml</exists>
19+
<missing>true</missing>
20+
</file>
21+
</activation>
22+
</profile>
23+
</profiles>
24+
</project>

0 commit comments

Comments
 (0)