Search code examples
mavenmaven-2maven-pluginpom.xmlmaven-profiles

Maven - skip plugin if property is empty/null


I want to obtain the following behavior: when I specify a value for the property "my.prop", I want the dependency and clean plugins to be executed. If a value is not specified for that property, I want them to be skipped.

I created "my.prop" like this:

<properties>
    <my.prop></my.prop>
</properties>

Then I read that profile activation works only for system properties, so I deleted the above and used the surefire plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <systemPropertyVariables>
            <my.prop></my.prop>
        </systemPropertyVariables>
    </configuration>
</plugin>

I tried using profiles, like this:

<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <skipDependecyAndCleanPlugins>false</skipDependecyAndCleanPlugins>
        </properties>
    </profile>
    <profile>
        <id>skip-dependency-and-clean-plugins</id>
        <activation>
            <property>
                <name>my.prop</name>
                <value></value>
                <!-- I also tried:  <value>null</value> without success.-->
            </property>
        </activation>
        <properties>
            <skipDependecyAndCleanPlugins>true</skipDependecyAndCleanPlugins>
        </properties>
    </profile>
</profiles>

Later, for each plugin I do something like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <skip>${skipDependecyAndCleanPlugins}</skip>
    </configuration>
    ....
</plugin>

But the plugins are still executed...

How can I determine Maven to skip the executions of the plugins when "my.prop" is empty/null?


Solution

  • The simplest solution would be to use the activation in the following form:

    <profiles>
      <profile>
        <activation>
          <property>
            <name>debug</name>
          </property>
        </activation>
        ...
      </profile>
    </profiles>
    

    The above means you can define any value for debug which means -Ddebug is enough.

    An empty value can't be defined a pom file cause <value></value> is equivalent to <value/> which means the same as not defined.

    Update:

    I would suggest to use a profile and NOT a property. So you can simply define on command line mvn -Pxyz install or leave it.