How to create a java project with gradle in eclipse

Posted on April 19, 2017


In this tutorial, we will learn how to create a java application using the Gradle build tool and Eclipse IDE.

Tools and technologies used-

  • JDK1.8.0_71
  • Eclipse Neon.3
  • Buildship, Eclipse Plug-ins for Gradle
  • Gradle 3.4
  • JUnit 4.12

Let’s begin step by step to see how a gradle java project is created in eclipse.

Install Gradle plug-ins (Buildship)

You can install the Buildship gradle tool from Eclipse Marketplace. Go to HelpEclipse Marketplace… menu to install the Buildship gradle tool.

Gradle-java-project01.png

Create Gradle project

In Eclipse, go to FileNewOther… menu path to select new project.

In New wizard, select Gradle Project and click on Next.

Gradle-java-project02.png

In next wizard, type the Project Name and click on Next.

Gradle-java-project03.png

In next wizard, select gradle distribution and click on Next.

Gradle-java-project04.png

In next wizard, preview the project configuration and click on Finish.

Gradle-java-project05.png

 

Review project structure

Review the following project structure generated by the Gradle build tool.

Gradle-java-project06.png

Gradle build file - build.gradle

Open build.gradle file and add the following code in it.

build.gradle

apply plugin: 'java'
apply plugin: 'application'

repositories {
    jcenter()
}

dependencies {
   compile 'com.google.guava:guava:20.0'  // Google Guava dependency
   testCompile 'junit:junit:4.12'         // JUnit dependency for testing
}

mainClassName = 'com.boraji.tutorial.MainApp'  // Main class with main method

Refresh the Gradle Tasks view after changes in the build.gradle file as follows.

Gradle-java-project07.png

Create Java programs

Remove the java files, generated by the gradle build tool, from project and create new files as follows.

Create MainApp.java file under src/main/java folder and write the following code in it.

MainApp.java

package com.boraji.tutorial;

/**
 * @author imssbora
 *
 */
public class MainApp {

   public String sayHello() {
      return "Hello Gradle";
   }

   public static void main(String[] args) {
      MainApp app = new MainApp();
      System.out.println(app.sayHello());
   }
}

Create MainAppTest.java file under src/test/java folder and write the following JUnit testcase in it.

MainAppTest.java

package com.boraji.tutorial;

import org.junit.Test;
import static org.junit.Assert.*;

/**
 * @author imssbora
 *
 */
public class MainAppTest {

   @Test
   public void testSayHello() {
      MainApp app = new MainApp();
      assertNotNull("Success", app.sayHello());
   }
}

Build project

In Gradle Tasks view/tab, right click on the build task and select Run Gradle Tasks to build the java project.

Gradle-java-project08.png

Console output-

Gradle Tasks: build

:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Could not find tools.jar. Please check that C:\Program Files\Java\jre1.8.0_121 contains a valid JDK installation.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 20.754 secs

As you can see in the console output, the project build is failed because the gradle build tool did not find the JDK environment.

To resolve the above error, create gradle.properties file under project’s root folder and write the following property (i.e. java home path) in it.

gradle.properties

org.gradle.java.home=C:\\Program Files\\Java\\jdk1.8.0_71

Now execute the build task again. After the build is finished, the console output looks like as follows.

Gradle Tasks: build

:compileJava
:processResources NO-SOURCE
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:compileTestJava
:processTestResources NO-SOURCE
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

Total time: 4.911 secs

 

Review final project Structure

The final project structure of gradle java project will look like as follows.

Gradle-java-project09.png

Run project

In Gradle Tasks view, right click on the run task and select Run Gradle Tasks to run the java project.

Gradle-java-project10.png

Console output-

Gradle Tasks: run

:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:run
Hello Gradle

BUILD SUCCESSFUL

Total time: 0.758 secs

Run JUnit testcase

In Gradle Tasks view, right click on the test task and select Run Gradle Tasks to run the JUnit testcase.

Gradle-java-project11.png

Console output-

Gradle Tasks: test

:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test UP-TO-DATE

BUILD SUCCESSFUL

Total time: 0.277 secs