Skip to content

Commit 8b85efe

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

File tree

6 files changed

+182
-7
lines changed

6 files changed

+182
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.ModelSource;
31+
import org.apache.maven.internal.impl.standalone.ApiRunner;
32+
import org.junit.jupiter.api.BeforeEach;
33+
import org.junit.jupiter.api.Test;
34+
35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
import static org.junit.jupiter.api.Assertions.assertNotNull;
37+
import static org.junit.jupiter.api.Assertions.assertNull;
38+
import static org.junit.jupiter.api.Assertions.assertTrue;
39+
40+
/**
41+
*
42+
*/
43+
class ComplexActivationTest {
44+
45+
Session session;
46+
ModelBuilder builder;
47+
48+
@BeforeEach
49+
void setup() {
50+
session = ApiRunner.createSession();
51+
builder = session.getService(ModelBuilder.class);
52+
assertNotNull(builder);
53+
}
54+
55+
@Test
56+
void testAndConditionInActivation() throws Exception {
57+
ModelBuilderRequest request = ModelBuilderRequest.builder()
58+
.session(session)
59+
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
60+
.source(ModelSource.fromPath(getPom("complex")))
61+
.systemProperties(Map.of("myproperty", "test"))
62+
.build();
63+
ModelBuilderResult result = builder.newSession().build(request);
64+
assertNotNull(result);
65+
assertNotNull(result.getEffectiveModel());
66+
assertEquals("activated-1", result.getEffectiveModel().getProperties().get("profile.file"));
67+
assertNull(result.getEffectiveModel().getProperties().get("profile.miss"));
68+
}
69+
70+
@Test
71+
public void testConditionExistingAndMissingInActivation() throws Exception {
72+
ModelBuilderRequest request = ModelBuilderRequest.builder()
73+
.session(session)
74+
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
75+
.source(ModelSource.fromPath(getPom("complexExistsAndMissing")))
76+
.build();
77+
ModelBuilderResult result = builder.newSession().build(request);
78+
assertNotNull(result);
79+
assertTrue(result.getProblems().stream()
80+
.anyMatch(p -> p.getSeverity() == BuilderProblem.Severity.WARNING
81+
&& p.getMessage().contains("The 'missing' assertion will be ignored.")));
82+
}
83+
84+
private Path getPom(String name) {
85+
return Paths.get("src/test/resources/poms/factory/" + name + ".xml").toAbsolutePath();
86+
}
87+
}
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)