This article describes how to use the Gradle tooling in Eclipse.

1. Eclipse Gradle support

The Eclipse IDE supports the Gradle build system via the Buildship project. Buildship provides a wizard to create Gradle projects, imports existing Gradle projects and runs Gradle tasks from the IDE. See Buildship project page for details. == Install the Gradle tooling (Buildship)

The Eclipse IDE for Java Developers already includes Gradle support. To check, select Help  About Eclipse IDE and look for the Gradle logo.

Gradle support in the About dialogGradle support in the About dialog
Installation via the Marketplace

Select Help  Eclipse Marketplace…​, search for Buildship and install it.

Search result for Buildship in the Eclipse Marketplace clientSearch result for Buildship in the Eclipse Marketplace client
Installation via the update manager

Select Help  Install New Software…​ and use the release update site, for example https://download.eclipse.org/releases/latest. Enter Buildship as filter and select the Buildship: Eclipse Plug-ins for Gradle feature.

Available Software dialog with the Buildship feature selectedAvailable Software dialog with the Buildship feature selected

Buildship provides additional update sites, for example for snapshot builds, see https://projects.eclipse.org/projects/tools.buildship/downloads.

2. Create a Gradle project

Select File  New  Other…​  Gradle  Gradle Project.

New Gradle project wizardNew Gradle project wizard

Press Next > and enter a project name.

Project name of the new Gradle projectProject name of the new Gradle project

Press Next > to configure the Gradle distribution. The default Gradle version of the wizard may be too old to run on the Java version of your Eclipse IDE. In this case, select Override workspace settings and Specific Gradle version and choose the latest version.

Gradle distribution optionsGradle distribution options
To use this Gradle version for all projects, set it under Window  Preferences  Gradle.

Press Finish. The wizard runs gradle init and imports the result. Use Next > instead to see a preview of the configuration first.

Preview page of the Gradle project wizardPreview page of the Gradle project wizard

The wizard creates a root project and a lib project with a Java library and its test.

The new Gradle projectThe new Gradle project

3. How to create a new Gradle powered Spring Boot application with Eclipse

The following demonstrates how to create a Spring Boot webapplication with Gradle.

First, create a standard Gradle project called com.vogella.springboot.gradle.minimal.

Change the build.gradle file to the following:

buildscript {
    ext {
        springBootVersion = '1.5.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'com.vogella.springboot'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-cache')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-hateoas')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')

    compile('com.h2database:h2')

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}

Ensure you update the project classpath, see Updating the Java class path via Gradle for details.

Create the following class.

package com.vogella.springboot.gradle.minimal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

Select this class, and select via the Run-As  Java Application context menu.

This starts String Boot and the selected application on https://localhost:8080.

To learn more about Spring Boot, see the Spring tutorial.

4. Import an existing Gradle project

Select File  Import…​  Gradle  Existing Gradle Project.

Gradle import wizardGradle import wizard

Press Next > and select the root directory of your Gradle project.

Root directory of the Gradle projectRoot directory of the Gradle project

Press Finish to import with the default settings. Or press Next > to configure the Gradle distribution and other options.

Import optionsImport options

The last page shows a preview of the import.

Import previewImport preview

5. Working with Gradle projects

5.1. Running Gradle tasks

The Gradle Tasks view shows the tasks of your projects.

Gradle Tasks viewGradle Tasks view

To run a task, double-click it or use its context menu.

Run a Gradle taskRun a Gradle task

The Gradle Executions view shows the progress of the build. The Console view shows its output, as you would see it on the command line.

5.2. Refreshing the classpath after build file changes

Eclipse does not update the classpath automatically if you change a build file. Right-click the project or the build.gradle file and select Gradle  Refresh Gradle Project.

Refresh Gradle ProjectRefresh Gradle Project

You can enable automatic synchronization of build file changes in the Gradle preferences.

Automatic project synchronizationAutomatic project synchronization

5.3. Adding Gradle support to an existing project

Right-click a Java project and select Configure  Add Gradle Nature.

Add Gradle NatureAdd Gradle Nature

If the project has no Gradle build files yet, run gradle init in its directory. == Annotation processing (apt) with Gradle in Eclipse

5.4. Overview

The gradle-apt-plugin supports annotation processing for Gradle. It supports using this in Eclipse and IntelliJ.

5.5. Setup in the Eclipse IDE

The following example describes it usage using the com.google.auto.value to automatically generate builders for your data model.

Adjust your build.gradle to enable the gradle-apt-plugin.

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "net.ltgt.gradle:gradle-apt-plugin:0.13"
  }
}

apply plugin: "net.ltgt.apt-eclipse"
apply plugin: 'java-library'

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

dependencies {
    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

5.6. Example: Using Auto-value with apt-processing

The following demonstrates the usage of the Auto value apt-processor in a Java project.

dependencies {
  // more stuff

  // auto-factory contains both annotations and their processor, neither is needed at runtime
  compileOnly "com.google.auto.value:auto-value:1.5"
  apt         "com.google.auto.value:auto-value:1.5"
}

Afterwards, run the eclipse task. This configures apt-processing within the Eclipse IDE.

import com.google.auto.value.AutoValue;

@AutoValue
abstract class Task {
    static Task create(String summary, String description, int duration) {
        // See "How do I...?" below for nested classes.
        return new AutoValue_Task(summary, description, duration); // generated by the apt processor
      }

      abstract String summary();
      abstract String description();
      abstract int duration();

}

Using it:

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

public class TaskTest {
    @Test public void testSomeLibraryMethod() {
        Task create = Task.create("Learn apt", "Now", 10);
        System.out.println(create);

    }
}

6. Android support

To compile Android projects with the Eclipse IDE, you can use gradle-android-eclipse plug-in from https://plugins.gradle.org/plugin/com.greensopinion.gradle-android-eclipse. Afterwards, the Android project should compile in the Eclipse IDE and you can run the Gradle tasks via the Gradle Task view.

7. Contribute to the buildship project

After you installed the Buildship tooling, you can help the project by reporting issues in the Gradle Forum or by contributing code.

7.1. Reporting bugs and feature requests

To help the Buildship project, please report all issues with the tooling via: New Bug report for Buildship You can report bugs or feature requests.

7.2. Get the source code

Since the project is hosted on GitHub, you can obtain the source by cloning it from the following URL:

git@github.com:eclipse/buildship.git

The project provides a Oomph setup for the development, which is explained further in the Buildship Docs.

8. Eclipse Gradle (Buildship) resources

Home Tutorials Training Consulting Books Company Contact us


Get more...