Setup Eclipse and Gradle and Build a Java project

临渊羡鱼不如退而结网。

We will discuss about the following two topics:

  1. How to setup Eclipse Gradle Plugin
  2. How to Develop Java Simple Example with Eclipse Gradle Plugin

Gradle uses it’s own DSL(Domain Specific Language) Groovy-based scripts to write build scripts. Unlike Ant and Maven, it does not use complex XML build scripts. Gradle is developed using Java and configuration elements are developed using Groovy. It uses Groovy to write build scripts.

Prerequisite:
To understand Gradle DSL scripts, we should have some knowledge about Groovy basics. Please go through some Groovy tutorials before going through this post.

NOTE:-
Groovy is a Dynamic Programming Language and it’s syntax similar to Java Programming Language. It is very easy to learn for a Java Developer.

Gradle Build Scripts

Now we will start writing simple Gradle build scripts. As we are already familiar with Ant and Maven build scripts, we know what is the starting point to start writing build scripts.

First and foremost thing we should know is Gradle default build script file name. Like Ant’s default build script name is build.xml and Maven’s default build script name is pom.xml, Gradle’s default build script name is “build.gradle”. When we run “gradle” command, it searches for this default file available in the current working directory. If it finds, it executes that build script. Otherwise, displays some useful default help message.

We will use Eclipse Gradle Plugin to develop and test all our Gradle examples. Before working with simple examples, first setup Gradle Plugin with Eclipse IDE

Setup Gradle Plugin with Eclipse IDE

I’m using Eclipse 4.4 Luna IDE. You can use same steps for other Eclipse versions too.

    • Open “Eclipse Marketplace…” from “Help” menu. Type “gradle” in Search box as shown below

graddle_instal_step1

    • Click on “Install” button for “Gradle Integration for Eclipse(4.4) 3.6.4.RELEASE” option to install Gradle Eclipse Plugin

graddle_instal_step2

    • Accept license by clicking radio button and click on “Finish” button.

graddle_instal_step3

This step installs Eclipse Gradle plugin and restarts Eclipse IDE. Now we can start developing applications using Gradle Build tool.

Gradle Simple Java Example

Now we are going to develop a Java Simple Example with Eclipse Gradle Plugin. Please use the following steps to develop and test this application.

    • Click on “File >> New >> Other” to open “New” Wizard window to create new Java Gradle Project.

Select “Gradle Project” option under “Gradle” Category as shown below:

gradle_example_new_wizard1

Click on “Next” Button to see “New Gradle Project” Window as shown below

gradle_example_new_wizard2

    • In “New Gradle Project” Window, we need to provide two details
      1. Please provide our project name: “JavaGradleSimpleExample”
      2. Select “Java Quickstart” option from “Sample project” Dropdown box

gradle_example_new_wizard3

Now click on “Finish” Button to create new Java Gradle project.

    • Now our Java Gradle project structure looks like this:

gradle_example_project_structure

NOTE:-
If you observe this project structure, Gradle Project is same as Maven Project structure. Yes, Gradle uses Maven Project structure but instead of pom.xml file, we need to use build.gradle file.

    • Our project build.gradle file contains this content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.5
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version
    }
}
repositories {
    mavenCentral()
}
dependencies {
compile group:'commons-collections',name:'commons-collections',version:'3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
    systemProperties 'property': 'value'
}
uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}

NOTE:- If you don’t understand this file content at this stage, don’t worry. We will discuss this file content in detail in coming posts and also provide you one post about Gradle and Maven build scripts differences in detail.

    • This default Java Gradle project, creates one Java file: Person.java and one JUnit:PersonTest.java as shown below.

Person.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package org.gradle;
import org.apache.commons.collections.list.GrowthList;
public class Person {
    private final String name;
    public Person(String name) {
        this.name = name;
        new GrowthList();
    }
    public String getName() {
        return name;
    }
}

PersonTest.java

1
2
3
4
5
6
7
8
9
10
11
12
package org.gradle;
import org.junit.Test;
import static org.junit.Assert.*;
public class PersonTest {
    @Test
    public void canConstructAPersonWithAName() {
        Person person = new Person("Larry");
        assertEquals("Larry", person.getName());
    }
}

It does NOT much logic. It just created a Person POJO class and one JUnit to test it.

    • It’s time to run our application. Please use the following steps to build and test our Gradle build script
      1. Right click on our “JavaGradleSimpleExample” Project’s build.gradle file and select “Run As” >> “Gradle build” option as shown below:

gradle_example_select_build_command

      1. It opens “Edit Configuration” Wizard window as shown below

gradle_example_edit_configuration

Here observe “Type tasks in the editor below. Use + to activate content assistant.” text.

      1. We need to type our required Gradle commands. When we keep control point in that Editor, it will display all available Gradle commands as shown below

gradle_example_edit_configuration_commands_list

      1. Now type “build” Gradle command in that Text editor as shown below.

gradle_example_edit_configuration_build_command

      1. Click on “Apply” button to apply our changes. Then click on “Run” button to start our Gradle build commnad “gradle build”

Observe the Eclipse IDE Console log.

gradle_example_build_command_output

If you observe the console output, it shows “BUILD SUCCESSFUL” message. That means our Gradle build command has executed successfully.

When we run this Grald command “gradle build”, it does the following things:

    1. It compiles both Java files
    2. It generate jar file with name “JavaGradleSimpleExample-1.0.jar” at ${PROJECT_ROOT_DIR}\build\libs that is “JavaGradleSimpleExample\build\libs” as shown below:

gradle_example_jar_file

  1. It executes JUnit file

If something goes wrong in any one of these steps, we will see “BUILD FAILED” error message.

That’s it about basic Gradle Example. We will explore build.gralde file content and Gradle commands in coming posts.