Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute my groovy script with ant or maven

I have the following:

  • 1 java class
  • 1 bat file (starts the groovy script)
  • 1 groovy file

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. Is there someone who can show me how to write this pom.xml or build.xml? I don't want to use the bat file anymore.

like image 631
Mikael Avatar asked Oct 12 '10 12:10

Mikael


People also ask

Can you use Maven with Groovy?

The execution of the Groovy script can be bound to any Maven phase; furthermore, inside the script, the objects session and project can be used to interact with the Maven build.

Can we use Maven and ant together?

You can use the maven-antrun-plugin to invoke the ant build. Then use the build-helper-maven-plugin to attach the jar produced by ant to the project. The attached artifact will be installed/deployed alongside the pom. If you specify your project with packaging pom , Maven will not conflict with the ant build.

How do I run an ant script from maven?

Action. Configure the run goal form the Maven AntRun plugin. Define any properties you wish to pass to the external Ant build, and then call the external Ant build with the ant task, specifying the antfile and target you wish to execute. To execute this external Ant build, run mvn package.


2 Answers

With Maven, use the gmaven plugin. From its documentation:

Execute a Local Groovy Script

<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
like image 73
Pascal Thivent Avatar answered Sep 30 '22 01:09

Pascal Thivent


There is a groovy plugin for ANT that can invoke groovy scripts

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

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-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>
like image 22
Mark O'Connor Avatar answered Sep 30 '22 02:09

Mark O'Connor