我正在通过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”属性的值。

在这方面的任何提示将是有用的..谢谢

最佳答案

您可以通过两种方式绑定(bind)和检索属性。一种是通过特定于插件的属性。

<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一样将其包含在您的通话中

另一个选择是直接绑定(bind)到项目级别的属性。如上所述,您会将其放在项目级别的属性中或调用中,并且不要在插件<properties>中包括<configuration>。如果走这条路线,您将通过project.properties['installation.dir']在脚本中访问GMaven或GMavenPlus(在这种情况下,也要删除<bindPropertiesToSeparateVariables>来访问GMavenPlus)。

如果这对您不起作用,请尝试将installation.dir重命名为installationDir。如果月经出现问题,我不会立刻就记起来。

关于maven - 外部Groovy脚本的gmaven插件: how to set property in pom. xml,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30051201/

10-11 22:16
查看更多