/ Java

Migrating from Maven to Gradle

Java has a long history of using XML configurations for everything. If you are like me, you do not enjoy writing or editing XML files as they are almost never human readable.

Adding plugins to your Maven pom.xml usually means copying an XML snippet from a page and manually tinkering with it until it does what you want it to do. I finally had enough of that and decided to look for an alternative. Having recently read about gradle I decided to give it a shot.

I am not going to talk about why I chose gradle over some other build system. If you need a comparison of existing build systems for Java, this article provides a good list.

Installing Gradle

I'm mostly developing on Mac OS X so installing gradle is as simple as:

brew update
brew install gradle

After that, you will have gradle installed. gradle -v should show you the information about the version of gradle you have installed.

Converting pom.xml to build.gradle

Head over to your Maven project and execute gradle init. This will generate a bunch of files for you, most importantly a build.gradle which you can now use to run all your builds. gradle init automatically detects the pom.xml and creates a gradle build with the java and maven plugin loaded. Existing Maven dependencies are automatically converted and added to your gradle build file.


For distribution, it is sometimes easier to distribute a single .jar-File instead of a jar and all its dependencies. With Maven, we can build a so-called 'jar-with-dependencies'. To do the same in gradle you can use the code below:

// Create a single Jar with all dependencies
task bigJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',
        	'Implementation-Version': version,
        	'Main-Class': 'eva2.gui.Main'
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar

Simply add this to your build.gradle file and adjust the manifest attributes to your liking. You can then run gradle bigJar to execute the task.