views:

58

answers:

4

I have the following

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

All is in the same folder

Now I want to use Maven or Ant to run the groovy file but I cant get it to work. Is there someone who can show me how to write this pom.xml or build.xml? I dont want to use the bat file anymore.

+1  A: 

You can do this via Ant as shown with a full example here. See the Compiling and running with Ant section.

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.

Once you have the build.xml in place, you can call ant at the command line which will run the build.xml

More details on the Groovy ant task here

JoseK
+5  A: 

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
Pascal Thivent
@Pascal: You stole my answer :-) (+1)
seanizer
@seanizer Yeah, I confess. But I gave you 15mn :)
Pascal Thivent
well I get as close that I have been before. I have written this now
Mikael Floberg
@Mikael Sorry, I didn't get you. Any particular problem pending?
Pascal Thivent
@Pascal BTW the artifactId has changed. current versions can be found here: http://mvnrepository.com/artifact/org.codehaus.gmaven/gmaven-plugin (the docs are outdated)
seanizer
@seanizer Thanks, updated.
Pascal Thivent
A: 

Well I get as close as I have been before

I have written this:

see link on first comment

My classpath GROOVY_HOME is C:/groovy

what am I suppose to edit in the text above?

My files are

a.groovy b.java

a.groovy depends on b.java

Mikael Floberg
@Mikael I'm sure you'll figure it out if you actually read the GMaven docs referenced by pascal
seanizer
A: 

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>
Mark O'Connor