Gmaven plugin: how to set property in pom.xml for external groovy script

I am running an external groovy script via a gmaven plugin in the pom.xml. The external script will say 'myscript.groovy'.

I want to provide some myscript.groovy options / arguments via maven pom.xml [ie inside the plugin execution 'gmaven-plugin']; but cannot do it.

I've tried using; but not sure how to get its values ​​in groovy script. Just calling the .get property does not give the property value.

Pom file binding:

<plugin>
                    <groupId>org.codehaus.gmaven</groupId>
                    <artifactId>gmaven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-resources-execute-groovyscript</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <properties>
                                    <property>
                                        <name>installation.dir</name>
                                        <value>${installation.dir}</value>
                                    </property>
                                </properties>
                                <source>${pom.basedir}/src/main/groovy/configure.groovy</source>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

      

Not sure how to get the value of the property 'installation.dir' in the 'configure.groovy' script.

Any hint in this regard would be helpful .. thanks

+3


source to share


1 answer


There are two ways to bind and retrieve properties. One of them will be associated with plugins specific properties.

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <executions>
    <execution>
      <id>generate-resources-execute-groovyscript</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <properties>
          <installation.dir>${installation.dir}</installation.dir>
        </properties>
        <source>${pom.basedir}/src/main/groovy/configure.groovy</source>
      </configuration>
    </execution>
  </executions>
</plugin>

      

They will be found in the script as project.properties['installation.dir']

.

GMaven is no longer supported (I was the last maintainer). If you want to use Groovy versions newer than 2.0.0, check out GMavenPlus . Here's the POM equivalent:

<plugin>
  <groupId>org.codehaus.gmavenplus</groupId>
  <artifactId>gmavenplus-plugin</artifactId>
  <version>1.5</version>
  <executions>
    <execution>
      <goals>
        <goal>execute</goal>
      </goals>
      <phase>generate-resources</phase>
    </execution>
  </executions>
  <configuration>
    <bindPropertiesToSeparateVariables>false</bindPropertiesToSeparateVariables>
    <properties>
      <property>
        <name>installation.dir</name>
        <value>${installation.dir}</value>
      </property>
    </properties>
    <scripts>
      <script>file:///${pom.basedir}/src/main/groovy/configure.groovy</script>
    </scripts>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.3</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</plugin>

      

The search in this case will be similar to properties['installation.dir']

. I know it's file:///

annoying. I removed this requirement in the next version.



For GMaven or GMavenPlus, if you choose the plugin properties approach, you will need to set the value elsewhere or something similar in your POM project

<properties>
  <installation.dir>C:\someDir</installation.dir>
</properties>

      

Or include it in your call like mvn -Dinstallation.dir=C:\someDir

Another option is to directly bind to project level properties. You would put it in your project level properties or in your call as mentioned above and not include it <properties>

in the plugin <configuration>

. If you follow this route, you will be able to access your script on project.properties['installation.dir']

for GMaven or GMavenPlus (also output <bindPropertiesToSeparateVariables>

for GMavenPlus in this case).

If that doesn't work for you, try renaming installation.dir

to something like installationDir

. I can't remember if the periods were problematic.

+4


source







All Articles