diff --git a/api/maven-api-model/src/main/mdo/maven.mdo b/api/maven-api-model/src/main/mdo/maven.mdo index d117d403fe..7df64f342b 100644 --- a/api/maven-api-model/src/main/mdo/maven.mdo +++ b/api/maven-api-model/src/main/mdo/maven.mdo @@ -2892,14 +2892,15 @@ missing 4.0.0+ String - The name of the file that must be missing to activate the - profile. + The name of the file that must be missing to activate the profile. Please note, that missing and exists + fields cannot be used together. Only one of them should be used at any one time. exists 4.0.0+ String - The name of the file that must exist to activate the profile. + The name of the file that must exist to activate the profile. Please note, that missing and exists + fields cannot be used together. Only one of them should be used at any one time. diff --git a/api/maven-api-settings/src/main/mdo/settings.mdo b/api/maven-api-settings/src/main/mdo/settings.mdo index b07d356534..f95229fefd 100644 --- a/api/maven-api-settings/src/main/mdo/settings.mdo +++ b/api/maven-api-settings/src/main/mdo/settings.mdo @@ -975,7 +975,8 @@ 1.0.0+ String - The name of the file that should be missing to activate a profile. + The name of the file that should be missing to activate a profile. Please note, that missing and exists + fields cannot be used together. Only one of them should be used at any one time. @@ -983,7 +984,8 @@ 1.0.0+ String - The name of the file that should exist to activate a profile. + The name of the file that should exist to activate a profile. Please note, that missing and exists + fields cannot be used together. Only one of them should be used at any one time. diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/profile/FileProfileActivator.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/profile/FileProfileActivator.java index bc1924854d..b0824a168a 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/profile/FileProfileActivator.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/profile/FileProfileActivator.java @@ -70,10 +70,22 @@ public class FileProfileActivator implements ProfileActivator { String path; boolean missing; - if (file.getExists() != null && !file.getExists().isEmpty()) { + boolean hasExists = file.getExists() != null && !file.getExists().isEmpty(); + boolean hasMissing = file.getMissing() != null && !file.getMissing().isEmpty(); + if (hasExists) { + if (hasMissing) { + problems.add( + BuilderProblem.Severity.WARNING, + ModelProblem.Version.BASE, + String.format( + "Profile '%s' file activation conflict: Both 'missing' (%s) and 'exists' assertions are defined. " + + "The 'missing' assertion will be ignored. Please remove one assertion to resolve this conflict.", + profile.getId(), file.getMissing()), + file.getLocation("missing")); + } path = file.getExists(); missing = false; - } else if (file.getMissing() != null && !file.getMissing().isEmpty()) { + } else if (hasMissing) { path = file.getMissing(); missing = true; } else { diff --git a/maven-api-impl/src/test/java/org/apache/maven/internal/impl/model/ComplexActivationTest.java b/maven-api-impl/src/test/java/org/apache/maven/internal/impl/model/ComplexActivationTest.java new file mode 100644 index 0000000000..3a7c5130df --- /dev/null +++ b/maven-api-impl/src/test/java/org/apache/maven/internal/impl/model/ComplexActivationTest.java @@ -0,0 +1,87 @@ +/* + * 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.internal.impl.model; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import org.apache.maven.api.Session; +import org.apache.maven.api.services.BuilderProblem; +import org.apache.maven.api.services.ModelBuilder; +import org.apache.maven.api.services.ModelBuilderRequest; +import org.apache.maven.api.services.ModelBuilderResult; +import org.apache.maven.api.services.ModelSource; +import org.apache.maven.internal.impl.standalone.ApiRunner; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * + */ +class ComplexActivationTest { + + Session session; + ModelBuilder builder; + + @BeforeEach + void setup() { + session = ApiRunner.createSession(); + builder = session.getService(ModelBuilder.class); + assertNotNull(builder); + } + + @Test + void testAndConditionInActivation() throws Exception { + ModelBuilderRequest request = ModelBuilderRequest.builder() + .session(session) + .requestType(ModelBuilderRequest.RequestType.BUILD_POM) + .source(ModelSource.fromPath(getPom("complex"))) + .systemProperties(Map.of("myproperty", "test")) + .build(); + ModelBuilderResult result = builder.newSession().build(request); + assertNotNull(result); + assertNotNull(result.getEffectiveModel()); + assertEquals("activated-1", result.getEffectiveModel().getProperties().get("profile.file")); + assertNull(result.getEffectiveModel().getProperties().get("profile.miss")); + } + + @Test + public void testConditionExistingAndMissingInActivation() throws Exception { + ModelBuilderRequest request = ModelBuilderRequest.builder() + .session(session) + .requestType(ModelBuilderRequest.RequestType.BUILD_POM) + .source(ModelSource.fromPath(getPom("complexExistsAndMissing"))) + .build(); + ModelBuilderResult result = builder.newSession().build(request); + assertNotNull(result); + assertTrue(result.getProblems().stream() + .anyMatch(p -> p.getSeverity() == BuilderProblem.Severity.WARNING + && p.getMessage().contains("The 'missing' assertion will be ignored."))); + } + + private Path getPom(String name) { + return Paths.get("src/test/resources/poms/factory/" + name + ".xml").toAbsolutePath(); + } +} diff --git a/maven-api-impl/src/test/resources/poms/factory/complex.xml b/maven-api-impl/src/test/resources/poms/factory/complex.xml new file mode 100644 index 0000000000..cb679b166a --- /dev/null +++ b/maven-api-impl/src/test/resources/poms/factory/complex.xml @@ -0,0 +1,49 @@ + + + + 4.0.0 + + test + test + 0.1-SNAPSHOT + pom + + + hello + + + + + two-conditions + + + complex.xml + + + myproperty + test + + + + activated-1 + + + + another-two-conditions + + + myproperty + test + + + complex.xml + + + + activated-2 + + + + diff --git a/maven-api-impl/src/test/resources/poms/factory/complexExistsAndMissing.xml b/maven-api-impl/src/test/resources/poms/factory/complexExistsAndMissing.xml new file mode 100644 index 0000000000..1a55d4ebec --- /dev/null +++ b/maven-api-impl/src/test/resources/poms/factory/complexExistsAndMissing.xml @@ -0,0 +1,24 @@ + + + + 4.0.0 + + test + test + 0.1-SNAPSHOT + pom + + + + two-conditions + + + simple.xml + true + + + + +