I’ve been making some useful discoveries the last few weeks.
I’m trying to decide on the best technology stack for my own work, and I’ve now decided on the build tool (Gradle) and the version control system (Mercurial). I’ll explain my reasons for choosing Mercurial in a future blog. In this one I’ll explain why I’ve chosen Gradle over Ant or Maven.
Some people swear by Maven. Its approach of convention-over-configuration makes setting up a build and deploy really straightforward. But if you want to do something slightly out-of-the-ordinary it’s like having your fingernails removed.
Ant, on the other hand, is highly versatile, but every build target needs to be defined explicitly, just as it would be in an old-fashioned makefile.
Gradle gives you the best of both worlds. It takes the convention-over-configuration approach of Maven, but instead of trying to force every project into the same mould, it allows you to reconfigure the mould according to your own requirements.
Here, for example, is the complete Gradle build file you need to build a Java project and package all the classes and resources into a Jar file (by default Gradle expects the same folder structure as a Maven build):
apply plugin: 'java'
Yes, that’s it. You put that in a file called build.gradle and run gradle build at the command line, and your build is done.
What if you want to write some tests, using JUnit, and you want to use the JUnit jar stored in a Maven repository? Well, just add a reference to the Maven repository and define the dependency on the JUnit jar:
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
Maybe you’ve also got some local jar files your build is dependent on. Do you have to pull your hair out to get it to work, as you do with Maven? No, you just need to add another dependency like the following:
dependencies {
compile fileTree(dir: 'lib', include: '*.jar')
}
I’ve got to the point now where I’m generating an eclipse project, building a jar, creating javadoc, creating a binary distribution (complete with Gradle-generated Windows batch file and Unix shell script), all with a Gradle build file just 26 lines long!
Next up is a multi-project build :-).