Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions api/maven-api-model/src/main/mdo/maven.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -2892,14 +2892,15 @@
<name>missing</name>
<version>4.0.0+</version>
<type>String</type>
<description>The name of the file that must be missing to activate the
profile.</description>
<description>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.</description>
</field>
<field>
<name>exists</name>
<version>4.0.0+</version>
<type>String</type>
<description>The name of the file that must exist to activate the profile.</description>
<description>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.</description>
</field>
</fields>
</class>
Expand Down
6 changes: 4 additions & 2 deletions api/maven-api-settings/src/main/mdo/settings.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -975,15 +975,17 @@
<version>1.0.0+</version>
<type>String</type>
<description>
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.
</description>
</field>
<field>
<name>exists</name>
<version>1.0.0+</version>
<type>String</type>
<description>
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.
</description>
</field>
</fields>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,22 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
49 changes: 49 additions & 0 deletions maven-api-impl/src/test/resources/poms/factory/complex.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<my.filter.value>hello</my.filter.value>
</properties>

<profiles>
<profile>
<id>two-conditions</id>
<activation>
<file>
<exists>complex.xml</exists>
</file>
<property>
<name>myproperty</name>
<value>test</value>
</property>
</activation>
<properties>
<profile.file>activated-1</profile.file>
</properties>
</profile>
<profile>
<id>another-two-conditions</id>
<activation>
<property>
<name>myproperty</name>
<value>test</value>
</property>
<file>
<missing>complex.xml</missing>
</file>
</activation>
<properties>
<profile.miss>activated-2</profile.miss>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<profiles>
<profile>
<id>two-conditions</id>
<activation>
<file>
<exists>simple.xml</exists>
<missing>true</missing>
</file>
</activation>
</profile>
</profiles>
</project>