Home gradle Create gradle project in eclipse and run the build

Create gradle project in eclipse and run the build

In this article we will see how to create a java gradle project in eclipse and how to run gradle build and unit tests of the java application using gradle build tasks.

Latest version of Eclipse or STS (Spring tool suite) has the built-in gradle support. You can easily create java gradle project in eclipse without any additional setup.

1. Create java gradle project in eclipse

1.1. Step 1

In eclipse click on File -> click on New -> click on Other option.

create new gradle project in eclipse step1

1.2. Step 2

In wizards box type gradle to filter wizards -> select Gradle Project -> Click on Next.

create gradle project in eclipse step 2

1.3. Step 3

Simply click on Next button.

create gradle project in eclipse step 3

1.4. Step 4

- Advertisement -

Enter the project name in project name text box -> click on Finish button.

create gradle project in eclipse step 4

1.5 Step 5

You will see following java gradle project structure in eclipse.

eclipse gradle project structure

2. Auto Generated files for Java gradle project in eclipse

2.1. Library.java

Library.java is a auto generated java file generated Gradle plugin.

public class Library {
    public boolean someLibraryMethod() {
        return true;
    }
}

2.2. LibraryTest.java

Gradle plugin also generates a simple junit test case LibraryTest.java to test the Library.java methods.

public class LibraryTest {
    @Test 
    public void testSomeLibraryMethod() {
        Library classUnderTest = new Library();
        assertTrue("someLibraryMethod should return 'true'", classUnderTest.someLibraryMethod());
    }
}

2.3. Auto generated build.gradle file

Auto generated java gradle project uses junit dependency junit:junit:4.12.

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.1.1/userguide/java_library_plugin.html
 */

plugins {
    id 'java-library'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    api 'org.apache.commons:commons-math3:3.6.1' //math library

    implementation 'com.google.guava:guava:28.1-jre' // google guava dependecy

    testImplementation 'junit:junit:4.12' // junit dependency for testing
}

2.4. Synchronize gradle tasks

If you updated source files or build.gradle file, refresh the gradle tasks to synchronize the changes to take effect on build tasks.

Refresh gradle project in eclipse

3. Run gradle build

In gradle tasks tab -> navigate to the project -> expand build folder -> right click on build -> Select Run Gradle tasks.

Run gradle tasks and tests in eclipse

Output :

Gradle Tasks: build

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :assemble
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
> Task :check
> Task :build

BUILD SUCCESSFUL in 2s
4 actionable tasks: 4 executed

4. Run unit tests using gradle task

Refer to above image, to run unit tests expand verification folder -> right click on test -> Select Run Gradle tasks.

All the unit tests will be executed. you will see output in console.

Gradle Tasks: test

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test

BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 executed

5. Conclusion

In this article we saw how to create a java gradle project in eclipse and how to run gradle build and unit tests of the java application using gradle build tasks.

References

  1. Gradle doc
  2. Eclipse buildship
  3. Gradle system properties via command line.
  4. Junit 5 gradle project
- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles