Provide flexible Maven profile activation via script.
Reference feature implementation, vote for MNG-6345.
Install | Production Release | Development Release |
---|---|---|
Artifact |
Similar extensions
Extension features
- comprehensive logging
- exposes resolved project pom.xml
- works with Maven 3.5 (activation via AND)
- scripting with Groovy, JavaScript, MVEL Script
Profile activator is configured in two steps:
- Register project extension
${project.basedir}/.mvn/extensions.xml
<extension>
<groupId>com.carrotgarden.maven</groupId>
<artifactId>profile-activator-extension</artifactId>
</extension>
- Provide activation script inside the magic property
<profile>
<activation>
<property>
<name>[ACTIVATOR:MVELSCRIPT]</name>
<value>
<![CDATA[
isdef property1 && isdef property2 && property1 == "hello-maven" && project.packaging == "bundle"
]]>
</value>
</property>
</activation>
</profile>
- example activator in Groovy
- example activator in JavaScript
- example activator in MVEL Script
Tip: check examples first.
Activator script has access to
- merged properties, as a map of type
java.util.Map<String, String>
namedvalue
- resolved project model, as an object of type
org.apache.maven.model.Model
namedproject
Activator script has ordered and merged view of
project
properties:pom.xml/<properties>
system
properties form:System.getProperties()
user
properties from user-provided command line-D
options
Merged properties scopes
- properties are injected in the default script variable scope (for scripting engines that have such)
- there is also an extension-provided map
value
, which contains a copy of the default scope - use
value
map to access variables with dot in the name or when there is no default scope
Example default scope access syntax:
the following expression extracts the value of
pom.xml/<properties>/<property1>
- Groovy syntax: not available, use
value["property1"]
- JavaScript syntax:
property1
(same result asvalue["property1"]
) - MVEL Script syntax:
property1
(same result asvalue["property1"]
)
Example value
map access syntax:
the following expression extracts the value of user.name
which originally comes from a system property,
and which is considered "invalid name" in the default scope:
- Groovy syntax:
value["user.name"]
- JavaScript syntax:
value["user.name"]
- MVEL Script syntax:
value["user.name"]
Resolved interpolated project descriptor is exposed in the form of project model bean .
Activator script has access to the project model as an object named project
.
Example project
object access syntax:
the following expression extracts the value of pom.xml/<packaging>
which is available as public String getPackaging()
- Groovy syntax:
project.packaging
(same result asproject.getPackaging()
) - JavaScript syntax:
project.packaging
(same result asproject.getPackaging()
) - MVEL Script syntax:
project.packaging
(same result asproject.getPackaging()
)
Normally, scripting engine will throw an error when trying to access a variable which is not defined.
Example variable existence check syntax:
- Groovy syntax: not available, use null-test:
if ( value["property1"] ) { ... }
- JavaScript syntax:
if ( typeof property1 !== 'undefined' ) { ... }
- MVEL Script syntax:
if ( isdef property1 ) { ... }
In practice, null-test
is sufficient in most cases
if ( value["property1"] ) { ... }
Project model members
are always defined, but can return null
.
cd /tmp
git clone [email protected]:random-maven/profile-activator-extension.git
cd profile-activator-extension
./mvnw.sh clean install -B -P skip-test