gmaven插件:如何在外部groovy脚本的pom.xml中设置属性 [英] gmaven plugin: how to set property in pom.xml for external groovy script

查看:166
本文介绍了gmaven插件:如何在外部groovy脚本的pom.xml中设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过pom.xml中的gmaven插件运行外部groovy脚本。
外部脚本是'myscript.groovy'。



我想通过maven pom.xml为myscript.groovy提供一些参数/参数。在插件'gmaven-plugin'执行中];但无法做到这一点..



我尝试过在;但不知道如何检索groovy脚本中的值。只需调用properties.get就不会给出属性值。



Pom文件快照:

 <插件> 
< groupId> org.codehaus.gmaven< / groupId>
< artifactId> gmaven-plugin< / artifactId>
<执行次数>
<执行>
< id> generate-resources-execute-groovyscript< / id>
<阶段>生成资源< /阶段>
<目标>
< goal>执行< / goal>
< /目标>
<配置>
<属性>
<属性>
< name> installation.dir< / name>
< value> $ {installation.dir}< /值>
< / property>
< / properties>
< source> $ {pom.basedir} /src/main/groovy/configure.groovy</source>
< / configuration>
< /执行>
< /执行次数>
< / plugin>

不知道如何在'configure.groovy'脚本中检索'installation.dir'属性的值。

这方面的任何提示都会很有用。谢谢

解决方案

<有两种方法可以绑定和检索属性。一个是通过插件特定的属性。

 < plugin> 
< groupId> org.codehaus.gmaven< / groupId>
< artifactId> gmaven-plugin< / artifactId>
<执行次数>
<执行>
< id> generate-resources-execute-groovyscript< / id>
<阶段>生成资源< /阶段>
<目标>
< goal>执行< / goal>
< /目标>
<配置>
<属性>
< installation.dir> $ {installation.dir}< /installation.dir>
< / properties>
< source> $ {pom.basedir} /src/main/groovy/configure.groovy</source>
< / configuration>
< /执行>
< /执行次数>
< / plugin>

这些将在脚本中检索,如 project.properties ['installation。 dir']



GMaven不再维护(我是最后一个维护者)。如果您想要使用比2.0.0更新的Groovy版本,请查看 GMavenPlus 。以下是等效的POM:

 < plugin> 
< groupId> org.codehaus.gmavenplus< / groupId>
< artifactId> gmavenplus-plugin< / artifactId>
< version> 1.5< / version>
<执行次数>
<执行>
<目标>
< goal>执行< / goal>
< /目标>
<阶段>生成资源< /阶段>
< /执行>
< /执行次数>
<配置>
< bindPropertiesToSeparateVariables> false< / bindPropertiesToSeparateVariables>
<属性>
<属性>
< name> installation.dir< / name>
< value> $ {installation.dir}< /值>
< / property>
< / properties>
< scripts>
< script> file:/// $ {pom.basedir} /src/main/groovy/configure.groovy</script>
< / scripts>
< / configuration>
<依赖关系>
< dependency>
< groupId> org.codehaus.groovy< / groupId>
< artifactId> groovy-all< / artifactId>
< version> 2.4.3< / version>
< scope>运行时< / scope>
< /依赖关系>
< /依赖关系>
< / plugin>

这种情况下的检索就像 properties ['installation.dir'] 。我知道 file:/// 很烦人。我已经在下一个版本中删除了这个要求。



对于GMaven或GMavenPlus,如果您选择插件属性方法,则需要在其他位置设置值在你的项目中有类似的东西POM

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

或者像 mvn -Dinstallation.dir = C: \someDir



另一个选项是直接绑定到项目级属性。您可以将它放在您的项目级别属性或您的调用中,如上所述,并且不要在插件<< code>< properties> 中包含< ;结构> 。如果你走这条路线,你可以通过 project.properties ['installation.dir'] 在你的脚本中访问GMaven或者GMavenPlus(同样取出< bindPropertiesToSeparateVariables> for GMavenPlus在这种情况下)。

如果这不起作用,请尝试重命名 installation.dir 类似于 installationDir 。如果期间有问题,我不记得手。


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

解决方案

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.

这篇关于gmaven插件:如何在外部groovy脚本的pom.xml中设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆