【问题标题】:gmaven plugin: how to set property in pom.xml for external groovy scriptgmaven 插件:如何在 pom.xml 中为外部 groovy 脚本设置属性
【发布时间】:2015-05-07 21:08:05
【问题描述】:

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

我想通过 maven pom.xml 向 myscript.groovy 提供一些参数/参数 [即在插件“gmaven-plugin”执行中];但不能这样做..

我试过使用 in ;但不确定如何在 groovy 脚本中检索其值。简单地调用 properties.get 并没有给出属性值。

pom 文件截图:

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

不确定如何在“configure.groovy”脚本中检索“installation.dir”属性的值。

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

【问题讨论】:

  • 如果没有 pom 文件以及如何调用 groovy 脚本,就不可能说出问题所在等。
  • 你好 khmarbaise.. 我已经为使用的 gmaven 插件添加了示例 pom snap ..

标签: maven groovy gmaven-plugin


【解决方案1】:

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

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

这些将在 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>
  <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>

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

对于 GMaven 或 GMavenPlus,如果您选择插件属性方法,则需要在项目 POM 中使用类似这样的方式在其他地方设置值

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

或将其包含在您的通话中,例如 mvn -Dinstallation.dir=C:\someDir

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

如果这对您不起作用,请尝试将 installation.dir 重命名为 installationDir 之类的名称。如果月经有问题,我不记得了。

【讨论】:

  • 你好 Keegan,谢谢你的回复我要去插件就像你上面提到的那样。所以在项目级别包含属性而不是在插件配置中。但是在 groovy 脚本中,当我尝试获取 project.properties['installation.dir'] 时,脚本不知道“project”变量。我在这里错过了什么吗..?
  • 不,但我刚刚意识到 GMaven 需要project.properties,即使属性没有在项目级别绑定。我编辑了答案以反映这一点。对于您的问题,如果您在插件级别使用,则需要将properties['installation.dir'] 用于 GMavenPlus。
  • 哎呀,您在项目级别使用,所以我的评论的第一个版本是正确的...对于您的问题,听起来您正在使用带有&lt;bindPropertiesToSeparateVariables&gt;fasle&lt;/bindPropertiesToSeparateVariables&gt; 的 GMavenPlus。删除该行,它应该可以工作。
  • 感谢 Keegan 提供的详细而有用的信息
  • 你好 Keegan,我现在正在尝试以下步骤: 1. 使用 gmaven plus(1.5 版) 2. 属性处于项目级别而不是插件级别 3. 我正在访问groovy 脚本中的属性为 def String installDir = project.properties['installation.dir']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多