Gradle Eclipse Plugin Tutorial With Examples

Today we will look into Gradle Eclipse plugin. In my previous post, we have discussed about Gradle Basics and Gradle installation. Before reading this post, Please refer my previous post about gradle tutorial.

Gradle Eclipse Plugin

In this post, we will discuss about the following two topics.

  1. How to setup Gradle Eclipse Plugin
  2. How to Develop Java Simple Example with Gradle Eclipse 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 is 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 default build script name is pom.xml, Gradle 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, let’s first setup Gradle Plugin with Eclipse IDE.

Gradle Eclipse Plugin installation

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

    1. Open “Eclipse Marketplace…” from “Help” menu. Type “gradle” in Search box as shown below
    2. graddle_instal_step1-383x450
    1. Click on “Install” button for “Gradle Integration for Eclipse(4.4) 3.6.4.RELEASE” option to install Gradle Eclipse Plugin
    2. graddle_instal_step1-383x450

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

Gradle Eclipse Plugin Example

Now we are going to develop a simple java example with Eclipse Gradle Plugin. Please use the following steps to develop and test this application.

    1. 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:

graddle_instal_step1-383x450

    1. Now our Java Gradle project structure looks like below image.
    2. gradle_example_project_structure1

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 have build.gradle file.

    1. Our project build.gradle file contains below content.

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.

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

Person.java

PersonTest.java

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

      1. Now type “build” Gradle command in that Text editor as shown below.
      2. gradle_example_jar_file1-450x171
      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_jar_file1-450x171

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

When we run the gradle build command, 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}buildlibs that is “JavaGradleSimpleExamplebuildlibs” as shown below:
    3. gradle_example_jar_file1-450x171
  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 for Gradle Eclipse plugin example. We will explore build.gradle file content and Gradle commands in coming posts.

By admin

Leave a Reply

%d bloggers like this: