Create Java projects with Gradle

So far Ant was my preferred tool, to create and build Java projects. But in the last days, I’ve looked into the next gen – Gradle.

It is a matter of taste, if you prefer XML or a DSL document. At the moment, I think XML is a little bit more user friendly, because you can validate the structure before you run it. I don’t see that for Gradle at the moment (maybe, the Eclipse plugin created by the Spring team is such a solution).

Gradle is, like I’ve read it in a book, the next evolution step after Ant and Maven. The reason for that is, because Gradle is different, and it makes some parts „easier“, as the old Java build tools. But to make it easier, you always need to define some requirements, which were not so defined in Ant. In an Ant project, you as the creator of the project, were responsible for the project folder structure. You’ve defined, where the sources and the classes are stored. Gradle out of the box, does this for you. So you need to follow this requirements.
Benefit is, if you know one Gradle project, you know them all, from the structure perspective. I don’t need to think about, where are the sources, I will always find them under src/main/java, same for the compiled classes, they are always under build/classes/main. With this pre defined structure, I need only one line, to create a build script for my simple project.

An example

Let’s start with the installation of Gradle, simply follow the instruction on the web page (decompress ZIP and add gradle/bin to your path).

In the next step we create folder „Example“, in which we will create our project. For that, we need to add a sub folder structure like src/main/java and finally we need a simple Java file, typically a „Hello World“ program – place it in the java subfolder.

public class GradleTest {
	public static void main(String[] args) {
		System.out.println("Hello World");

In the project root folder we create a single line build script.


apply plugin: 'java'

That’s it. In the terminal run gradle build, located in our porject folder.

The result is big enough, for one line of build script. The java files are compiled, a JAR file was created, and test cases are performed (if we had added some, before):

  - classes
    - main
      - GradleTest.class
  - dependency-cache
  - libs
    - Example.jar
  - reports
    - tests
      - base-style.css
      - index.html
      - report.js
      - style.css
  - test-results
    - binary
      - test
        - result.bin
  - tmp
    - jar

The related Ant script, would be much longer.
Do some more examples. If you want to add the version into your MANIFEST, you can just add a section to your build.gradle, to do so.


apply plugin: 'java'

def myVersion = '1.0'

jar {
  manifest {
    attributes 'Implementation-Title': 'My Gradle Example', 
               'Implementation-Version': myVersion

def myVersion defines a variable, which can be accessed later on. This is similar to the property elements in Ant.
In the section jar { } we customize the behaivour of the JAR file, and we want to customize the manfiest, which can be done by the subsection manifest { }.

But there is a „nicer“ way, to do that. We also want the version number in the JAR file name (this is very common in Open Source projects), so let’s try the keyword version, instead of myVersion.


apply plugin: 'java'

version = '1.0'

jar {
  manifest {
    attributes 'Implementation-Title': 'My Gradle Example', 
               'Implementation-Version': version

With this change, we get the version number in our Manifest file and we get the filename Example-1.0.jar.
Damit wird zum einen die Version in mein Manifest geschrieben und zeitgleich bekomme ich eine Datei „Beispiel-1.0.jar„.

As a wrap up, I would say, Gradle looks very interessting, and I would follow the next evolution step, after Ant and Maven.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.