Skip to content

rrialq/jsr223-profile-activator-extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#JSR223 Profile Activator Maven Extension

About

According to the documentation at Sonatype a profile is activated when all trigger conditions are met.

But conditions are very simple: scripts are not supported, therefore it is not possible to write complex conditions.

This extension allows you to use the JSR223 support in Java6 and following for writing scripts to determine whether to activate or not a profile.

Implementation details

Instead of changing the schema of Project Object Model (or POM) this extension works as property condition activation, delegating to the original PropertyProfileActivator when it does not apply the following syntax for JSR223:

  • The name of the property must starts with '=' (equals char), and, optionally, followed by the short name of the ScriptEngine, mime type or file extension (i.e. JavaScript).
  • The default short name of the ScriptEngine is "JavaScript", because it comes with the JDK.
  • If you want to use another language supported by JSR223 you must to add the libraries to the Maven classpath.

How this extension works

The value tag of the property profile activator is the script to execute. Last sentence in script will return a boolean value, true to activate the profile, false otherwise.

Which properties are visible

The following set of properties are visible (project properties and user properties are experimental):

  • System properties.
  • Project properties
  • User properties.

Resources available in script

The expression manager

The expression manager is a simplified vision of the Maven interpolator. The expression manager evaluates a simple Maven expression, returning an InterpolatedExpression.

You can use the expression manager as an object in the script language; its default name is aitor.

The interpolated expression

The interpolated expression is the object returned by the expression manager. It encapsulates the interpolated value, and it has some methods to get the interpolated value or a default value, String, boolean or int, directly.

How to use

Since 0.2.0 version the extension supports the .mvn/extensions.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<extensions>
    ...
    <extension>
        <groupId>com.coutemeier.maven</groupId>
        <artifactId>jsr223-profile-activator-extension</artifactId>
        <version>0.2.0</version>
    </extension>
</extensions>

Examples

About the examples

Please, keep in mind that the following examples was written considering the simplicity of the same, not in its correct syntax, so the XML presented here is not well formed. For example, instead of the && you must write &amp;&amp;

A simple profile in Maven

<profiles>
    <profile>
        <id>MY_SITE_SKIP_ACTIVATED</id>
        <activation>
            <property>
                <name>MY_SITE_SKIP</name>
                <value>true</value>
            </property>
        </activation>
        ...
    </profile>
</profiles>

The above example written with jsr223-profile-activator-extension

<profiles>
    <profile>
        <id>MY_SITE_SKIP_ACTIVATED</id>
        <activation>
            <property>
                <name>=</name>
                <value>
                    aitor.eval('${MY_SITE_SKIP}').getBoolean(false);
                </value>
            </property>
        </activation>
        ...
    </profile>
</profiles>

Of course, it is a script...

<profiles>
    <profile>
        <id>MY_SITE_SKIP_ACTIVATED</id>
        <activation>
            <property>
                <name>=</name>
                <value>
                    var mySiteSkip = aitor.eval('${MY_SITE_SKIP}').getBoolean(false);
                    var reportsEnabled = aitor.eval('${REPORTS_ENABLED}').getBoolean(false);
                    var reportsLevel = aitor.eval('${REPORTS_LEVEL}').getInt(0);
                    var siteUrl = aitor.eval('${SITE_URL}').getString('');
                    var siteDefined = ( siteUrl != '' );
                    mySiteSkip &amp;&amp; reportsEnabled &amp;&amp; ( reportsLevel > 0 ) &amp;&amp; siteDefined
                </value>
            </property>
        </activation>
        ...
    </profile>
</profiles>

About

Maven extension to allow profile activation with jsr223 scripts

Resources

License

Stars

Watchers

Forks

Packages

No packages published