gmaven plugin: how to set property in pom.xml for external groovy script
Asked Answered
W

1

6

I'm running an external groovy script via gmaven plugin in pom.xml. The external script is say 'myscript.groovy'.

I want to provide some parameters/arguments to myscript.groovy via the maven pom.xml [i.e. inside the plugin 'gmaven-plugin' execution]; but unable to do so..

I've tried using in ; but not sure how to retrieve its values in the groovy script. Simply calling properties.get is not giving the property value.

Snap of pom file:

<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 retrieve the value of 'installation.dir' property in 'configure.groovy' script.

Any hint in this regard will be useful.. thanks

Wilderness answered 5/5, 2015 at 11:3 Comment(2)
Without pom file and how you call the groovy script it's imposible to say what's wrong etc.Soldier
hello khmarbaise.. I've added the sample pom snap for gmaven plugin used ..Wilderness
D
9

There are two ways you can bind and retrieve properties. One would be through plugin-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>

These would be retrieved in the script like project.properties['installation.dir'].

GMaven isn't maintained anymore (I was the last maintainer). If you want to use versions of Groovy newer than 2.0.0, have a look at GMavenPlus. Here's the equivalent POM:

<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>

Retrieval in this case would be like properties['installation.dir']. I know the file:/// is annoying. I've removed the requirement for that in the next release.

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

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

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

The other option is to bind to project level properties directly. You would put it in your project level properties or in your call, like mentioned above, and don't include <properties> in the plugin <configuration>. If you go this route, you'd access in your script by project.properties['installation.dir'] for either GMaven or GMavenPlus (also take out <bindPropertiesToSeparateVariables> for GMavenPlus in this case).

If this doesn't work for you, try renaming installation.dir to something like installationDir. I can't remember offhand if periods were problematic.

Diaster answered 6/5, 2015 at 17:39 Comment(9)
Hello Keegan, thanks for the reply I'm going the plugin was as you've mentioned above. So included the properties at the project level & not in the plugin configuration. But in the groovy script when I try to get project.properties['installation.dir'] then 'project' variable is unknown to the script. Am I missing something here..?Wilderness
No, but I just realized GMaven needs project.properties, even if the properties aren't bound at the project level. I edited the answer to reflect that. For your problem, if you are using at the plugin level, you need to use properties['installation.dir'] for GMavenPlus.Diaster
Oops, you're using at project level, so the first version of my comment was correct... For your problem, it sounds like you're using GMavenPlus with <bindPropertiesToSeparateVariables>fasle</bindPropertiesToSeparateVariables>. Remove that line and it should work.Diaster
Thanks for the detailed & useful info KeeganWilderness
Hello Keegan, I'm trying it now with following steps: 1. Using gmaven plus (version 1.5) 2. Property is at the project level & not at the plugin level 3. I'm accessing the property in the groovy script as def String installDir = project.properties['installation.dir']Wilderness
But error comes in running the maven build while executing the groovy script as: Error occurred while calling a method on a Groovy class from classpath groovy.lang.MissingPropertyException: No such property: System for class: org.apache.maven.project.MavenProject .. at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:49) at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:63)Wilderness
Also in IntelliJ IDE, for the statement in groovy script: project.properties['installation.dir'] th 'project' variable is unresolvable.. Am I missing something here..?Wilderness
You can't execute the script from IntelliJ (unless you use the Maven panel to execute the associated goal). Because otherwise it just runs as a standalone script, and it must execute within the context of a Maven project.Diaster
For the System property issue, can you share the line it's complaining about?Diaster

© 2022 - 2024 — McMap. All rights reserved.