I observed a Maven behavior today that left me puzzled. Either the project properties have some sort of write-once semantics or... I failed to find any reference documentation that would explain this behavior.
Can I dynamically set Maven properties in the validate phase to have them evaluated as plugin properties in later phases?
Goal
I want to skip the execution of a plugin in a profile based on the existence of some other property.
Plan
- Run the Groovy plugin in the
validate
phase. - Evaluate the existence of that other property.
- Set the skip project property.
- Use the skip property for the configuration of plugins that run in later phases.
POM excerpt
<profile>
<id>my-id</id>
<activation>
<file>
<exists>${basedir}/somefile</exists>
</file>
</activation>
<properties>
<skipIt>false</skipIt> <!-- intention: use this as default value -->
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>
<![CDATA[
def someOtherProp = project.properties['some-other-property']
def skipIt = someOtherProp ? 'false' : 'true'
project.properties['skipIt'] = skipIt
println skipIt
println project.properties['skipIt']
]]>
</script>
</scripts>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<skip>${skipIt}</skip>
</configuration>
<executions>
<execution>
<id>my-unpack</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
Behavior
If some-other-property
does not exist (i.e. is not set) this prints true
twice. That means the Groovy evaluation is correct and skipIt
was correctly set. Yet, the dependency-plugin still runs the my-unpack
execution. Why?
However, if I remove <skipIt>false</skipIt>
on line 9 everything works as I expect. Why?