2

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

  1. Run the Groovy plugin in the validate phase.
  2. Evaluate the existence of that other property.
  3. Set the skip project property.
  4. 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?

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • Does this answer your question? [Maven - activate profile based on project property](https://stackoverflow.com/questions/18889554/maven-activate-profile-based-on-project-property) – Illya Kysil Apr 06 '21 at 20:10
  • This has nothing to do with my question AFAICS (other than Maven, profiles, and properties being involved). – Marcel Stör Apr 06 '21 at 20:47
  • Does the gmavenplus-plugin actually change the project properties or only some copy of it? – J Fabian Meier Apr 07 '21 at 10:20
  • @JFabianMeier good thinking! However, if it were operating on a copy then the behavior wouldn't change if I remove `false`, would it? – Marcel Stör Apr 07 '21 at 12:08

0 Answers0