Tutorial

Gradle Eclipse Plugin Tutorial

Published on August 3, 2022
author

Rambabu Posa

Gradle Eclipse Plugin Tutorial

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 gradle eclipse plugin install marketplace3. Click on “Install” button for “Gradle Integration for Eclipse(4.4) 3.6.4.RELEASE” option to install Gradle Eclipse Plugin gradle eclipse plugin5. Accept license by clicking radio button and click on “Finish” button. gradle eclipse plugin installed

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: eclipse gradle example Click on “Next” Button to see “New Gradle Project” Window as shown below eclipse gradle new project4. In “New Gradle Project” Window, we need to provide two details

  2. Please provide our project name: “JavaGradleSimpleExample”

  3. Select “Java Quickstart” option from “Sample project” Dropdown box

gradle eclipse project wizard Now click on “Finish” Button to create new Java Gradle project.7. Now our Java Gradle project structure looks like below image. eclipse gradle project structure 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.10. Our project build.gradle file contains below content.

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'
       }
    }
}

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.12. This default Java Gradle project, creates one Java file: Person.java and one JUnit test class PersonTest.java as shown below. Person.java

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

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 have much logic. It just created a Person POJO class and one JUnit to test it.17. 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: build gradle project in eclipse3. It opens “Edit Configuration” Wizard window as shown below gradle build configurations eclipse Here observe “Type tasks in the editor below. Use + to activate content assistant.” text.5. 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 eclipse example edit configuration commands list7. Now type “build” Gradle command in that Text editor as shown below. gradle eclipse example edit configuration build command9. 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. run gradle project in eclipse

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}\build\libs that is “JavaGradleSimpleExample\build\libs” as shown below: gradle example jar file4. 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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Rambabu Posa

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
December 2, 2019

sir thank u so much great explanation in simple language… sir please tell me , is spring boot frameworks only works on four components or anything else please tell me.

- swapnil patil

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 22, 2019

    Hi we are using eclipse oxygen, will creating sample gradle project external/ internal dependencies are not getting created, what could be the possible reason?

    - Loopa C

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      January 16, 2019

      Is this gradle integration in eclipse is free one OR Need to pay for this . Thanks. Pandurang

      - Pandurang

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        August 28, 2018

        Hi, Thanks for the explanation. I am new to Gradle and it is being used in my project. I am using eclipse STS with jdk 7 to build one of my Gradle project but I am continuously getting ‘Tools.jar not found error’ while doing 'Gradle STS --> Refresh All. I have checked the tools.jar is present in the jdk --> jre --> ext folder and is also being shown in JRE Libraries. Could you please help.

        - Manish Chauhan

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          August 15, 2018

          Sir I don’t found “gradle integration eclipse(4.4) 3.6.4 release” in my eclipse so plz give m another link for download or install it

          - Swechchha jain

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            May 10, 2018

            thank you ;)

            - keerthana

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              May 31, 2017

              Very useful for beginners like me :)

              - Kapil

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                February 22, 2017

                Hi Ram, I am new Gradle, And your blog helping me a lot. Please mention your next gradle post link here in this post. So that we can get it easily.

                - Ram Thota

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  October 5, 2016

                  Where can I get this Persons Java example… or is it should be created on our own

                  - sravan

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    August 24, 2016

                    I am new to gradle tool. can u pls explain how to write a builde.gradle file for existing project after installing gradle plugins. Thanks.

                    - Thirupathi

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Become a contributor for community

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      DigitalOcean Documentation

                      Full documentation for every DigitalOcean product.

                      Resources for startups and SMBs

                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                      Get our newsletter

                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                      New accounts only. By submitting your email you agree to our Privacy Policy

                      The developer cloud

                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Get started for free

                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                      *This promotional offer applies to new accounts only.