Build Java projects with Gradle

Pete Houston
5 min readNov 20, 2015

--

Assuming that you’ve already had Gradle installed on your dev machine. In my case, I have Gradle 2.9 installed via Homebrew.

$ brew install gradle

Let’s start by creating a new directory, and issue the command:

$ gradle init
:wrapper
:init
BUILD SUCCESSFULTotal time: 4.308 secsThis build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.9/userguide/gradle_daemon.html

The following directories and files are generated,

├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle

File build.gradle is where we will define tasks for build; and the rest, just forget about them, those are not covered in this post.

After removing all comments, we have this clean build.gradle

apply plugin: ‘java’ /* (1) */repositories {
jcenter() /* (2) */
}
dependencies { /* (3) */
compile ‘org.slf4j:slf4j-api:1.7.13’
testCompile ‘junit:junit:4.12’
}

A little explanation about it.

  • At (1), Gradle is a build system and it has a plugin system that helps develop to execute common tasks. In this case, we have a build task for Java application.
  • At (2), this is where we tell Gradle about the repositories that it should connect to query information about dependencies.
  • At (3), name of the dependencies required for compiling source and execute tests.

Next, we start with a very simple Java project. Create the following source file structure,

src
├── main
│ └── java
│ └── com
│ └── petehouston
│ └── greet
│ ├── Greet.java
│ └── Program.java

Write a very basic Java code just for testing with Gradle build,

Program.java

package com.petehouston.greet;public class Program {  public static void main(String[] args) 
{
Greet greet = new Greet();
System.out.println( greet.hello(“Pete”) );
System.out.println( greet.bye(“Pete”) );
}
}

Greet.java

package com.petehouston.greet;public class Greet {  public Greet() { }  public String hello(String name) 
{
return “Hello “ + name;
}
public String bye(String name)
{
return “Bye “ + name;
}
}

Now, we need to know what tasks are available for build,

$ gradle tasks — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — 
All tasks runnable from root project
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Build tasks
— — — — — -
assemble — Assembles the outputs of this project.
build — Assembles and tests this project.
buildDependents — Assembles and tests this project and all projects that depend on it.
buildNeeded — Assembles and tests this project and all projects it depends on.
classes — Assembles main classes.
clean — Deletes the build directory.
jar — Assembles a jar archive containing the main classes.
testClasses — Assembles test classes.
Build Setup tasks
— — — — — — — — -
init — Initializes a new Gradle build. [incubating]
wrapper — Generates Gradle wrapper files. [incubating]
Documentation tasks
— — — — — — — — — -
javadoc — Generates Javadoc API documentation for the main source code.
Help tasks
— — — — —
components — Displays the components produced by root project ‘gradle-java’. [incubating]
dependencies — Displays all dependencies declared in root project ‘gradle-java’.
dependencyInsight — Displays the insight into a specific dependency in root project ‘gradle-java’.
help — Displays a help message.
model — Displays the configuration model of root project ‘gradle-java’. [incubating]
projects — Displays the sub-projects of root project ‘gradle-java’.
properties — Displays the properties of root project ‘gradle-java’.
tasks — Displays the tasks runnable from root project ‘gradle-java’.
Verification tasks
— — — — — — — — —
check — Runs all checks.
test — Runs the unit tests.

Wow, there are many…Don’t be afraid, you just need to focus on what you need. You will gradually get familiar with it soon.

Okay so, one thing to do is to replace the repositories by using maven central host,

build.gradle

apply plugin: ‘java’repositories {
mavenCentral() // <-- replace the old part: jcenter() by mavenCentral()
}
dependencies {
compile ‘org.slf4j:slf4j-api:1.7.13’
testCompile ‘junit:junit:4.12’
}

To build project, issue the following command,

$ gradle build

A newly-generated directory build appears,

build
├── classes
│ └── main
│ └── com
│ └── petehouston
│ └── greet
│ ├── Greet.class
│ └── Program.class
├── dependency-cache
├── libs
│ └── gradle-java.jar
└── tmp
├── compileJava
│ └── emptySourcePathRef
└── jar
└── MANIFEST.MF
11 directories, 4 files

You can see that Gradle has generated source files into classes along with a JAR package. You can test the JAR,

$ java -jar build/libs/gradle-java.jar
no main manifest attribute, in build/libs/gradle-java.jar

The JAR is not executed, of course, since we haven’t defined the main entry class in in manifest. Let’s define it by adding a new JAR entry in build.gradle

jar {
manifest {
attributes ‘Main-Class’ : ‘com.petehouston.greet.Program’
}
}

The project will be re-built and you can try to execute the JAR once again.

$ gradle build$ java -jar build/libs/*.jar
Hello Pete
Bye Pete

We can do even more by specifying the JAR output name along with version,

jar {
manifest {
attributes ‘Main-Class’ : ‘com.petehouston.greet.Program’
}
baseName = ‘greet-program’
version = ‘0.1.0’
}

After rebuilding, a new file is generated build/libs/greet-program-0.1.0.jar

It looks nice, but it would be better to avoid typing the typical java -jar … all the time. Okay, let’s do it by adding the application plugin,

apply plugin: ‘application’mainClassName = ‘com.petehouston.greet.Program’

This plugin allows us to execute any JAR file in the project by specifying the main entry class for execution as above. To execute, we issue this,

$ gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Hello Pete
Bye Pete
BUILD SUCCESSFULTotal time: 4.1 secsThis build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.9/userguide/gradle_daemon.html

It works nicely, but too much verbose. We can suppress the output build log by adding the quiet option.

$ gradle run -q
Hello Pete
Bye Pete

That should do the trick!

Hope that you have basic understandings on Gradle and know how to work around with it. Additionally, refer to following links to discovery more about Gradle.

The code for this project is at: https://github.com/petehouston/demo-java-gradle

Have fun Gradle-ing :)

--

--