mavengroovypom.xmlgmavenplus

How do I set a maven project property with a groovy script run with gmavenplus?


I have a pom file that uses a property that I'd like to set within an external groovy script. This groovy script needs some existing properties to determine what to set the new property is, but, when I set the property bindPropertiesToSeparateVariables to false (within the script execution), all the necessary properties are exposed to the groovy script, but I am unable to set my new property (project.properties.setProperty('myproperty', value) complains about 'project' not existing, and properties.setProperty('myproperty', value) doesn't work). When I set bindPropertiesToSeparateVariables to true, not all the necessary properties are exposed to the groovy script (project.properties does not have all the properties), but I can set my new property using project.properties.setProperty('myproperty', value) and it is set successfully.

I am a bit confused as to what exactly bindPropertiesToSeparateVariables does, because the description of the property is basically just restating what is already in the name of the property. I tried using parent.properties but that didn't work. Can I manually define the properties in the plugin's configuration (right above where the script is executed) and then access them with project.properties? If that worked, what if new properties are added that I didn't manually define like that? Does session.properties work?

Here is part of my pom file where the script is executed. The last line is where I need to access my new property.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.example.group</groupId>
        <artifactId>example-parent-pom</artifactId>
        <version>1.0.0</version>
        <relativePath>../../..</relativePath>
    </parent>

    <groupId>com.example.group</groupId>
    <artifactId>example.artifact.id</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <someOtherProperty>someValue</someOtherProperty>
        <anotherProperty>anotherValue</anotherProperty>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <outputDirectory>${project.build.directory}/sql</outputDirectory>
                    </configuration>
                    <executions>
                        <!-- copy the basescript groovy files so they are accessible on the classpath during script execution -->
                        <execution>
                            <id>Copy basescripts</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/classes/</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${basedir}/../../../config/build/scripts</directory>
                                        <includes>
                                            <include>**/*.groovy</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                        <execution>
                            <id>Copy some of the scripts we use</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/classes/</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${basedir}/../scripts</directory>
                                        <includes>
                                            <include>**/SomeScriptWeUse.groovy</include>
                                            <include>**/SomeOtherScriptWeUse.groovy</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <configuration>
                    <allowSystemExits>true</allowSystemExits>
                    <skip>someValue</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>set-myProperty</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <scripts>
                                <script>file:///${basedir}/../scripts/setMyProperty.groovy</script>
                            </scripts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.example.another.group.id</groupId>
                <artifactId>homemade-plugin</artifactId>
                <configuration>
                    <skip>${someSkipProp}</skip>
                    <verbose>true</verbose>
                    <forceRemove>true</forceRemove>
                    <someVersionProp>1</someVersionProp>
                </configuration>
                <executions>
                    <execution>
                        <id>Just another execution</id>
                        <goals>
                            <goal>some-goal</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <someProperty>${myProperty}</someProperty>
                        </configuration>
                    </execution>

Any suggestions are welcome. Please let me know if more information is needed.


Solution

  • You can keep bindPropertiesToSeparateVariables set to false and replace properties.setProperty('myproperty', value) with project.properties.setProperty('myproperty', value). Then, to access the property further down in the pom file, reference it as ${myproperty}. Don't define <myproperty>value</myproperty anywhere in the pom file or this will not work.