Skip to content

Commit

Permalink
[MNG-8286] Add a condition profile based on a simple expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 25, 2024
1 parent 2a6fc5a commit e82940f
Show file tree
Hide file tree
Showing 16 changed files with 2,274 additions and 7 deletions.
43 changes: 41 additions & 2 deletions api/maven-api-model/src/main/mdo/maven.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -2742,10 +2742,43 @@
<class>
<name>Activation</name>
<version>4.0.0+</version>
<description>The conditions within the build runtime environment which will trigger the
<description><![CDATA[
The conditions within the build runtime environment which will trigger the
automatic inclusion of the build profile. Multiple conditions can be defined, which must
be all satisfied to activate the profile.
</description>
<p>In addition to the traditional activation mechanisms (JDK version, OS properties,
file existence, etc.), Maven now supports a powerful condition-based activation
through the {@code condition} field. This new mechanism allows for more flexible
and expressive profile activation rules.</p>
<h2>Condition Syntax</h2>
<p>The condition is specified as a string expression that can include various
functions, comparisons, and logical operators. Some key features include:</p>
<ul>
<li>Property access: {@code ${property.name}}</li>
<li>Comparison operators: {@code ==}, {@code !=}, {@code <}, {@code >}, {@code <=}, {@code >=}</li>
<li>Logical operators: {@code &&} (AND), {@code ||} (OR), {@code not(...)}</li>
<li>Functions: {@code exists(...)}, {@code missing(...)}, {@code matches(...)}, {@code inrange(...)}</li>
</ul>
<h2>Examples</h2>
<ol>
<li>JDK version range: {@code inrange(${java.version}, '[11,)')} (JDK 11 or higher)</li>
<li>OS check: {@code ${os.name} == 'windows'}</li>
<li>File existence: {@code exists('${project.basedir}/some-file.txt')}</li>
<li>Property check: {@code ${my.property} != 'some-value'}</li>
<li>Regex matching: {@code matches(${os.version}, '.*aws')}</li>
<li>Complex condition: {@code ${os.name} == 'windows' && ${os.arch} != 'amd64' && inrange(${os.version}, '[10,)')}</li>
</ol>
<p>This flexible condition mechanism allows for more precise control over profile
activation, enabling developers to create profiles that respond to a wide range of
environmental factors and project states.</p>
]]></description>
<fields>
<field>
<name>activeByDefault</name>
Expand Down Expand Up @@ -2798,6 +2831,12 @@
<type>String</type>
<description>Specifies that this profile will be activated based on the project's packaging.</description>
</field>
<field>
<name>condition</name>
<version>4.1.0+</version>
<type>String</type>
<description>The condition which must be satisfied to activate the profile.</description>
</field>
<!--
This could be included once we teach Maven to deal with multiple versions of the model
<field>
Expand Down
8 changes: 8 additions & 0 deletions api/maven-api-settings/src/main/mdo/settings.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,14 @@
Specifies that this profile will be activated based on the project's packaging.
</description>
</field>
<field>
<name>condition</name>
<version>2.0.0+</version>
<type>String</type>
<description>
The condition which must be satisfied to activate the profile.
</description>
</field>
</fields>
</class>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import org.apache.maven.api.di.Inject;
import org.apache.maven.api.di.Named;
Expand All @@ -43,6 +44,7 @@
import org.apache.maven.api.services.xml.SettingsXmlFactory;
import org.apache.maven.api.services.xml.XmlReaderException;
import org.apache.maven.api.services.xml.XmlReaderRequest;
import org.apache.maven.api.settings.Activation;
import org.apache.maven.api.settings.Profile;
import org.apache.maven.api.settings.Repository;
import org.apache.maven.api.settings.RepositoryPolicy;
Expand Down Expand Up @@ -233,10 +235,23 @@ private Settings interpolate(Settings settings, SettingsBuilderRequest request,
Map<String, String> systemProperties = request.getSession().getSystemProperties();
src = Interpolator.chain(userProperties::get, systemProperties::get);
}
return new SettingsTransformer(value -> value != null ? interpolator.interpolate(value, src) : null)
return new DefSettingsTransformer(value -> value != null ? interpolator.interpolate(value, src) : null)
.visit(settings);
}

static class DefSettingsTransformer extends SettingsTransformer {
DefSettingsTransformer(Function<String, String> transformer) {
super(transformer);
}

@Override
protected Activation.Builder transformActivation_Condition(
Supplier<? extends Activation.Builder> creator, Activation.Builder builder, Activation target) {
// do not interpolate the condition activation
return builder;
}
}

@Override
public List<BuilderProblem> validate(Settings settings, boolean isProjectSettings) {
ArrayList<BuilderProblem> problems = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public static Profile convertToSettingsProfile(org.apache.maven.api.model.Profil

activation.packaging(modelActivation.getPackaging());

activation.condition(modelActivation.getCondition());

profile.activation(activation.build());
}

Expand Down Expand Up @@ -212,6 +214,8 @@ public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Prof

activation.packaging(settingsActivation.getPackaging());

activation.condition(settingsActivation.getCondition());

profile.activation(activation.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,13 @@ public Profile apply(Profile p) {
.build();
}

@Override
protected Activation.Builder transformActivation_Condition(
Supplier<? extends Activation.Builder> creator, Activation.Builder builder, Activation target) {
// do not interpolate the condition activation
return builder;
}

@Override
protected ActivationFile.Builder transformActivationFile_Missing(
Supplier<? extends ActivationFile.Builder> creator,
Expand Down
Loading

0 comments on commit e82940f

Please sign in to comment.