Running Gradle in Eclipse

We can generate the Eclipse project files using the Eclipse plugin. We can also import a Gradle build file in Eclipse and then execute Gradle tasks from within Eclipse. In this section, we will see how to install the Gradle plugin in Eclipse and also how to use it to import a Gradle build file and execute tasks.

Gradle developed an Eclipse plugin, named Buildship. The plugin adds Gradle integration to the Eclipse IDE. The plugin has an import wizard to import existing Gradle builds. Multi-project builds are also supported by it.

The plugin keeps track of dependencies defined in the Gradle build file as project dependencies. This means that if we change a dependency in the build file, the Eclipse classpath will be updated with the change so that the compiler can use it.

To execute the tasks, the plugin adds an additional view to Eclipse. From the view, we can execute the tasks. The Eclipse launching framework is used to execute the tasks.

Installing the buildship plugin

The following steps can be used to install the buildship plugin:

  1. To install the plugin, we will use the Eclipse marketplace. From the Help menu, we will select Eclipse Marketplace.... We will see then a new dialog window. In the Search field, we will type buildship and wait for the search result, as shown in the following screenshot:

    Installing the buildship plugin

    Searching Buildship plugin in the Eclipse Marketplace

  2. We then click on the Install button to install the plugin in our Eclipse IDE. In the install dialog window, we will select the plugin features and click on the Confirm button:

    Installing the buildship plugin

    Selecting the Buildship plugin features

  3. We have to accept the licenses, and Eclipse downloads the plugin components. Restart Eclipse after all the plugin components have been downloaded:

    Installing the buildship plugin

    License for the Buildship plugin

Importing a Gradle project

After we have installed the plugin, we can use the import wizard to import a Gradle project. We will use a very simple Java project with the following build file:

apply plugin: 'java' 
 
version = '1.0' 
group = 'sample.gradle' 
 
description = 'Sample Java project' 
 
repositories { 
    jcenter() 
} 
 
dependencies { 
    testCompile 'junit:junit:4.11' 
} 
  1. In Eclipse, we select the Import option from the File menu.
  2. In the Import dialog window, we will select the Gradle Project option before we click on the Next button, as shown in the following screenshot:

    Importing a Gradle project

    Importing a Gradle project

  3. In the next step of the import wizard, we must specify the root folder for our Gradle project. For a multi-project build, we should select the root folder. If we only have a single project, we can select the project folder then.
  4. We will click on the Next button to look at other options. However, we can also click on the Finish button to import the project into Eclipse:

    Importing a Gradle project

    Import Gradle Project overview

  5. In the Import Gradle Project dialog window, we can specify some extra options:

    Importing a Gradle project

    Configuration for importing Gradle project

  6. In the Gradle distribution section, we can specify the Gradle location and distribution that we want to use to build our project. We use the default Gradle wrapper (recommended) option to use the wrapper find in the project. We can also choose a Local installation directory to reference a Gradle installation on our computer. With the Remote distribution location option, we can set a download location for a Gradle distribution. This could, for example, also be corporate intranet site with a distribution.
  7. Finally, we can use Specific Gradle version to select a version. The version is downloaded to our computer if we don't have it already.
  8. In the Advanced options section, we can specify some more option for running Gradle. For example, to change the GRADLE_USER_HOME directory or extra command-line options.
  9. We will click on the Next button and get an overview of all settings before the project is imported:

    Importing a Gradle project

    Preview of options for importing a Gradle project

  10. Next, we will click on the Finish button to import the project into Eclipse:

    Importing a Gradle project

    Imported Gradle project in Eclipse

Running tasks

To execute tasks, we will first open the Gradle Tasks view. Normally, it is already visible at the bottom of our workspace. If not, we can open it from the Window menu, we will select Show View and then Other.... We will type gradle in the search field to search for the Gradle Tasks view.

  1. In our workspace, we will now have the Gradle Tasks view. The tasks for this project are then displayed as shown in the following screenshot:

    Running tasks

    Overview of Gradle task in the Gradle Tasks view

  2. To execute a task, we will simply have to double-click on the task name. In the Gradle Executions view, we can see the tasks that are executed and the time each task took:

    Running tasks

    Results after running a Gradle task

  3. To see the output of the task, we will open the Console view. Here, we can see the console output of the executed task:

    Running tasks

    Console output after running a Gradle task

  4. We can also use the launch framework of Eclipse to run tasks. We must right-click on the project and select the Gradle Build or Gradle Build... option from the Run As option. The launch configuration is opened and here we can configure the tasks to be executed, as shown in following screenshot:

    Running tasks

    Configuring a Gradle task as run configuration

  5. Here we can type the name of the Gradle task that we want to execute.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.144.248