Ilija Matoski

    ← Articles

    Getting started with gradle and java

    I always used ant with Apache Ivy or maven to build java applications, so I thought there has to be something better and easier to build the application instead of using them. So I decided to give gradle a go.

    To build a project with gradle is quite simple, and unlike ant and maven, managing dependencies is really easy.

    build.gradle

    apply plugin: 'java'
    apply plugin: 'eclipse'
    version = '1.0'
    eclipse {
    classpath {
    downloadSources=true
    downloadJavadoc=true
    }
    }
    repositories {
    mavenCentral()
    }
    dependencies {
    compile 'log4j:log4j:1.2.17'
    compile 'com.google.guava:guava:18.0'
    compile 'io.netty:netty-all:4.0.23.Final'
    testCompile 'junit:junit:4.11'
    }
    jar {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    manifest {
    attributes 'Implementation-Title': '', 'Implementation-Version': version
    attributes 'Main-Class': 'com.matoski.blog.demo.Hello'
    }
    }

    src/main/java/com/matoski/blog/demo/Hello.java

    package com.matoski.blog.demo;
    public class Hello {
    public static void main(String[] args) {
    System.out.println("Hello");
    }
    }

    If you run the build command gradle build it will build the project and create a jar file so we can run it.

    # gradle build
    :compileJava
    :processResources UP-TO-DATE
    :classes
    :jar
    :assemble
    :compileTestJava UP-TO-DATE
    :processTestResources
    :testClasses
    :test UP-TO-DATE
    :check UP-TO-DATE
    :build
    BUILD SUCCESSFUL
    Total time: 12.48 secs

    And now we can run the application easily by running

    # java -jar build/libs/hello.jar
    Hello

    It was really simple to build everything with gradle.