Using Groovy instead of Ant…

Рубрика: Development, Groovy, Java | 17 August 2007, 09:26 | Vadim Voituk

… or flexible build process using Groovy

Consider you need to create 30-40 build artifacts (depending of some params) during build process.
Or maybe to embed some static data, gathered from remote servers using network protocols into your application.
Or fill-in your test database before starting unit/functional testing with data in a non-trivial way.

The classic way to solve this issue is to create a-thousands-line-long build script that is hardly readable and impossible to support.

Groovy AntBuilder can help you to solve this issue much more elegantly.
A built-in ant support makes it possible to use any ant task from within a groovy script. To do that you just need to use AntBuilder class.

Lets create a build-project scratch. Will improve it later on.

class GroovyBuilder {
  // Application entry point
  static void main(args) {
    if (args.length <= 0) {
      println "Usage: groovy GroovyBuilder.groovy "
      System.exit(0);
    }
    def target = args[0]
    GroovyBuilder build = new GroovyBuilder()
    build."$target"(args)
  }
}

As you can see, this code simply runs a method, specified via the command line.

Now let’s enchant it with some AntBuilder calls:

// Constructor
def GroovyBuilder() {
  ant = new AntBuilder()
}

Then define build methods using Ant-like targets:

// INIT
void init(args) {
  ant.mkdir(dir: basedir + "/bin")
  ant.mkdir(dir: basedir + "/build")
}
// BUILD
void build(args) {
  init() // 'build' target depends of 'init' target
  ant.buildnumber()
  ant.javac(srcdir:basedir+"/src", destdir:basedir+"/bin",)
}
// RUN
void run(args) {
  build() // 'run' target depends of 'build' target
  ant.java(classpath:basedir+"/bin",classname: "Test")
}
// JAR
void jar(args) {
  build();
  ant.jar(
    destfile:"build/my-test-proj.${ant.project.properties['build.number']}.jar",
    basedir:basedir+"/bin"
  )
}
// CLEAR
void clear(args) {
  ant.delete(dir: basedir + "/bin", failonerror:false)
  ant.delete(dir: basedir + "/build", failonerror:false)
}

Now lets try to run the script and see the output:

E:\my\workspace\GG>groovy GroovyBuilder.groovy build
[mkdir] Created dir: E:\my\workspace\GG\bin
[mkdir] Created dir: E:\my\workspace\GG\build
[javac] Compiling 1 source file to E:\my\workspace\GG\bin

And the project is built! Piece of cake :-)

Notice that AntBuilder method names are same as corresponding Ant tasks names. What we got here – is a build.xml file written in slightly different notation.
Now its simple to add some more advanced behavior like flow control or environment initialization.

There is nice example in “Groovy in Action”: a build.xml file that checks a JDK version and fails if it below 1.5. With groovy scripting capabilities this task is accomplished with a single line of code.

Комментариев: 20

20 Responses to “Using Groovy instead of Ant…”

Комментарии:

  1. Rober

    There is also Gant now…

    http://groovy.codehaus.org/Gant

  2. Mark

    You’re better off using Maven 2. God, it makes life easier.

  3. Robert

    I have not heard much good about it to date. That is what “I” have read and it could be wrong. I see most of the Java world using Ant and Gant makes that easier to deal with.

  4. Mark

    Maven 1 was a pain but Maven 2 is definitely king of built tools. Most of the java world is using Ant and Gant because that’s what they started with and invested time in but if you’re going to start a new project giving Maven 2 a look is a must.

  5. vadim

    I think maven is more complex and difficult tool, than agile-like Groovy`s AntBuilder/GAnt.
    But groovy is not so widely used…

  6. Mark

    Complex in what sense?

  7. Juriy

    Hi, Mark.

    I didn’t have an opporunity to work with maven2 but I guess it will take some time to set it up and running correctly and then to create a nice build script with a lot of complex operations.

    I agree with you: maven2 is here to become a de-facto standard ;-) Still it _might_ be easier to build a quick prototype of build script using groovy rather then maven.

  8. Mark

    Hi Vadim,

    Yes, getting a complex Maven 2 build system setup has a steep learning curve but getting a simple prototype build script going, however, is pretty straightforward and easy. Like you said, Maven 2 is here to stay and as someone coming from Ant world I believe it’s best build tool by far.

  9. vadim

    It was Juriy in previous comment :)

  10. juriy

    I’ve just found a post about maven: http://tapestryjava.blogspot.com/2007/08/maven-love-to-hate-or-hate-to-love.html guys say it’s really hard to set everything up and running if you’re using maven for a non-standard project.

    Please look at Peter’s reply: he uses Gant (yeap, that’s a new special fancy word for Groovy + Ant) for building his project: http://prometheus.codehaus.org and he seems to feel quite happy about that: “The power of ANT combined with the power of a full blown programming language”.

  11. Dave

    Bleh to Maven2. Much prefer script builders. If you want something very strutured like M2 but with script flexibility you can also try Buildr.

  12. vadim

    Dave, unfortunately buildr can be used by ruby-programmers only.
    Unlike Groovy, that is easy undestandable for any Java developer.

  13. Vadim Voituk

    Just another article about Groovy+Ant, published on javaworld.com

  14. Andrey

    Could you please give any hints on how to get access to java classes from a NetBeans project (in order to make other work with groovy, for example)?
    NetBeans uses Ant as building shell so we need to get java classpath from the corresponding build-impl.xml and force to make it available when groovy starts…

  15. Juriy

    I’ve never worked with NetBeans so I may be wrong.

    Did I get you right: you have a NetBeans project with classpath that is already set up. You need to extract a classpath from autogenerated build file and use it with your groovy shell?

    Try searching for the classpath property name in the build-impl.xml then import all this file into the new one and start groovy from there. Use ant’s “import” directive. In this way you’ll leave original file intact still you’ll use it’s properties.

    I’ve never done things like this with groovy though.

  16. Andrey

    I installed a groovy plug-in for NetBeans. It imports following ant task to the project build-impl.xml:

    So, as I see, I need to add another element to with project classpath…

  17. Andrey

    <project name=”groovy-compile-impl” default=”groovyc” basedir=”..”>

    <property file=”nbproject/project.properties”/>

    <target name=”groovyc” description=”groovyc”>
    <taskdef name=”groovyc”
    classpath=”dist/lib/groovy-all-1.1-rc-2-SNAPSHOT.jar”
    classname=”org.codehaus.groovy.ant.Groovyc”/>
    <groovyc srcdir=”${src.dir}” destdir=”groovy”>
    <classpath path=”dist/lib/groovy-all-1.1-rc-2-SNAPSHOT.jar”/>
    </groovyc>
    </target>

    </project>

  18. Andrey

    NetBeans generates project *.jar file, so for a simple situation is it enough just to add <classpath path=”dist/project.jar”/>?

  19. juriy

    I’m not sure about that – the best way to find it out is to try.

Leave a Reply