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

纵然是瞬间 提交于 2019-12-01 17:49:39

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!