Ant, Gradle and Maven - comparison - checking build prerequisites

So you decided to check some build prerequisites before doing any real job. You want to check if proper Java version is installed, if some configuration files are available etc.

I'll show you how you can do this using Ant, Gradle and Maven.

Please notice, that these code snippets are not equivalent. They are here only to illustrate how you can achieve similar effect using each of aforementioned tools.

So, let the code speak!

Ant

This snippet of code is taken from TestNG's build script (slightly changed).

<target name="validate">
  <condition property="requiredJavaVersion">
      <equals arg1="${ant.java.version}" arg2="1.3"/>
  </condition>
  <fail unless="requiredJavaVersion"
        message="Java version 1.3 required."/>
</target>
</project>
  • this way of doing "if" seems very unnatural,
  • too verbose,
  • not so easy to customize - in "then clause" you can do only what Ant tasks allow you to do (the good thing is, there are many Ant tasks available),
  • you need to know a lot about Ant and API of Ant tasks - condition, fail, ${ant.java.version}.

Gradle

task validate {
        if (!['1.3', '1.4'].contains(System.getProperty('java.version'))) {
                println "ERROR Java 1.3 or 1.4 required !
                   (found Java ${System.getProperty('java.version')})"
                System.exit(1)
        }
}
  • very straightforward and easy to understand,
  • no restriction on what you can do in "then clause",
  • you need to know Java or Groovy,
  • no standard way of expressing such build requirement, so the code you come up with might be very different from what your next-desk-colleague came up with.

Maven

This snippet of code is taken from maven-enforce-plugin usage (slightly changed).

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-enforcer-plugin</artifactId>
   <executions>
     <execution>
       <id>enforce-versions</id>
       <goals>
         <goal>enforce</goal>
       </goals>
       <configuration>
         <rules>
           <requireJavaVersion>
             <version>1.3</version>
           </requireJavaVersion>      
         </rules>
       </configuration>
     </execution>
   </executions>
</plugin>
  • can do easily only what plugin authors predicted,
  • adding custom behavior is possible, but cumbersome (take a look here),
  • unnecessary verbose (goals, execution, id) for such a simple task,
  • this is a standard way of doing this kind of task with Maven,
  • you need to know such plugin exists (many Maven users don't - believe me).

Conclusions

This simple example shows what is the difference between XML based tools like Ant and Maven, and DSL (Groovy) based Gradle. First two are way too verbose and limit you only to things that were planned by tool author. This might be enough for simple cases, but will pinch in real-world development. Adding custom behavior will cost you a lot of effort.

Gradle allows you to do whatever you want, not limiting you with only a set of beforehand prepared choices. It allows to write more explicit, more verbose code. And I like it a lot.

Links

 
 
 
This used to be my blog. I moved to http://tomek.kaczanowscy.pl long time ago.

 
 
 

Please comment using