[MNG-5910] Warn if both exists and missing file activation are set (#1773)

This commit is contained in:
Guillaume Nodet 2024-10-08 15:46:42 +02:00 committed by GitHub
parent 81af852fce
commit 6c8b808760
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 182 additions and 7 deletions

View File

@ -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>

View File

@ -975,7 +975,8 @@
<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>
@ -983,7 +984,8 @@
<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>

View File

@ -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 {

View File

@ -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();
}
}

View File

@ -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>

View File

@ -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>