Skip to content

random-maven/profile-activator-extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Profile Activator Extension

Provide flexible Maven profile activation via script.

Reference feature implementation, vote for MNG-6345.

Apache License, Version 2.0, January 2004 Travis Status

Install Production Release Development Release
Artifact Central Bintray

Similar extensions

Extension features

Usage Examples:

Profile activator is configured in two steps:

  1. Register project extension
${project.basedir}/.mvn/extensions.xml
<extension>
   <groupId>com.carrotgarden.maven</groupId>
   <artifactId>profile-activator-extension</artifactId>
</extension>
  1. 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>

Script variables

Tip: check examples first.

Activator script has access to

Merged Properties

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 as value["property1"])
  • MVEL Script syntax: property1 (same result as value["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 Project Model

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 as project.getPackaging())
  • JavaScript syntax: project.packaging (same result as project.getPackaging())
  • MVEL Script syntax: project.packaging (same result as project.getPackaging())

Variable existence check

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.

Build yourself

cd /tmp
git clone git@github.com:random-maven/profile-activator-extension.git
cd profile-activator-extension
./mvnw.sh clean install -B -P skip-test