简体   繁体   English

用ant或maven执行我的groovy脚本

[英]Execute my groovy script with ant or maven

I have the following: 我有以下内容:

  • 1 java class 1个java类
  • 1 bat file (starts the groovy script) 1个bat文件(启动groovy脚本)
  • 1 groovy file 1个groovy文件

All in the same folder. 全部在同一个文件夹中。

Now I want to use Maven or Ant to run the groovy file but I can't get it to work. 现在我想使用Maven或Ant来运行groovy文件,但我无法让它工作。 Is there someone who can show me how to write this pom.xml or build.xml? 有人可以告诉我如何编写这个pom.xml或build.xml吗? I don't want to use the bat file anymore. 我不想再使用bat文件了。

With Maven, use the gmaven plugin. 使用Maven,使用gmaven插件。 From its documentation : 从其文件

Execute a Local Groovy Script 执行本地Groovy脚本

 <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <version>1.3</version> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <source>${pom.basedir}/src/main/script/myscript.groovy</source> </configuration> </execution> </executions> </plugin> 

And trigger the specified phase. 并触发指定的阶段。

Or, if you don't want to bind the plugin to a particular phase, you could configure it like this: 或者,如果您不想将插件绑定到特定阶段,可以像下面这样配置它:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.3</version>
    <configuration>
        <source>
           println "Hi"
        </source>
    </configuration>
</plugin>

And call 并致电

mvn groovy:execute

There is a groovy plugin for ANT that can invoke groovy scripts ANT有一个groovy插件 ,可以调用groovy脚本

<groovy src="helloWorld.groovy"/>

I would recommend combining it with ivy which can download the required jars for you, similar to the Maven example given previously. 我建议将它与常春藤相结合,它可以为您下载所需的罐子,类似于之前给出的Maven示例。

build.xml build.xml文件

<project name="demo" default="run" xmlns:ivy="antlib:org.apache.ivy.ant">

    <target name="resolve">
        <ivy:resolve/>
        <ivy:cachepath pathid="build.path"/>
    </target>

    <target name="run" depends="resolve">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

        <groovy src="helloWorld.groovy"/>
    </target>

</project>

ivy.xml 的ivy.xml

<ivy-module version="2.0">
    <info organisation="org.myorg" module="demo"/>
    <dependencies>
        <dependency org="org.codehaus.groovy" name="groovy-all" rev="1.7.4" conf="default"/>
    </dependencies>
</ivy-module>

You can do this via Ant as shown with a full example here . 您可以通过Ant执行此操作,如此处的完整示例所示。 See the Compiling and running with Ant section. 请参阅“ 编译并使用Ant运行”部分。

You'll have to download ant , make sure the ANT_HOME and JAVA_HOME variables are set, and put the ANT_HOME/bin in your PATH. 您必须下载ant ,确保设置了ANT_HOME和JAVA_HOME变量,并将ANT_HOME / bin放入PATH中。

Once you have the build.xml in place, you can call ant at the command line which will run the build.xml 一旦有了build.xml,就可以在运行build.xml的命令行中调用ant

More details on the Groovy ant task here 有关Groovy ant任务的更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM