Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Gradle Effective Implementations Guide - Second Edition

You're reading from  Gradle Effective Implementations Guide - Second Edition

Product type Book
Published in May 2016
Publisher
ISBN-13 9781784394974
Pages 368 pages
Edition 2nd Edition
Languages
Author (1):
Hubert Klein Ikkink Hubert Klein Ikkink
Profile icon Hubert Klein Ikkink

Table of Contents (18) Chapters

Gradle Effective Implementations Guide - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. Starting with Gradle 2. Creating Gradle Build Scripts 3. Working with Gradle Build Scripts 4. Using Gradle for Java Projects 5. Dependency Management 6. Testing, Building, and Publishing Artifacts 7. Multi-project Builds 8. Mixed Languages 9. Maintaining Code Quality 10. Writing Custom Tasks and Plugins 11. Gradle in the Enterprise 12. IDE Support

Chapter 12.  IDE Support

When we develop applications, we usually use an integrated development environment (IDE). An IDE provides support for writing code for our applications. We can write our code in Java, Groovy, or Scala. We have seen how to use Gradle to define, for example, library dependencies to compile the code. We want to use the same information that we have defined in a Gradle build file in a project in our favorite IDE.

In this chapter, we will discuss how to use Gradle plugins to generate the project files with classpath dependencies for Eclipse and JetBrains IntelliJ IDEA. We will also discuss how to customize the file generation to add extra configuration data.

Next, we will see the Eclipse and IntelliJ IDEA support for running Gradle tasks from within the IDE.

In this chapter, we will cover the following subjects:

  • Using the Eclipse plugin

  • Using the IDEA plugin

  • Integrating Gradle with Eclipse

  • Running Gradle scripts from IDEA IntelliJ

Using the Eclipse plugin


The Eclipse plugin can generate the project files necessary to import the project in Eclipse. In this section, we will see the tasks that are added by the plugin and how to customize the generated output.

If we have a Java project and we want to import the project into Eclipse, we must use the Eclipse plugin to generate the Eclipse project files. Each Eclipse project has a .project and .classpath file as minimum. The .project file contains the metadata about the project, such as the project name. The .classpath file contains classpath entries for the project. Eclipse needs this in order to be able to compile the source files in the project. The Eclipse plugin will try to download the artifact with source files belonging to a dependency as well. So, if we import the project into Eclipse and the source files are available, we can directly see the source of dependent class files.

For a Java project, an additional Java Development Tools (JDT) configuration file is created...

Using the IntelliJ IDEA plugin


IntelliJ IDEA from JetBrains is another IDE that we can use to develop applications. Gradle has the IDEA plugin to generate the project files for IntelliJ IDEA. This means that we can simply open the project in IntelliJ IDEA. The dependencies are set correctly to compile the project in the IDE. In this section, we will see how to generate these files and customize file generation.

IntelliJ IDEA supports a folder-based and file-based format for the project files. The IDEA plugin generates files for the file-based format. The file format for the project files is XML. The workspace project file has the .iws extension and contains personal settings. The project information is stored in a file with the .ipr extension. The project file can be saved in a version control system as it doesn't have a reference to local paths. The workspace project file has a lot of personal settings and this shouldn't be put in a version control system.

For a Java project, we have a third...

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...

Running Gradle in IntelliJ IDEA


We can generate IDEA project files with the IDEA plugin in our build file. IntelliJ IDEA has a Gradle plugin to import a Gradle project without first creating the project files. In this section, we will discuss how to use the Gradle plugin with IntelliJ IDEA 15 Community Edition or Ultimate.

We use the same project that was used with the Eclipse Gradle plugin to import the project into IntelliJ IDEA.

Installing the plugin

The Gradle plugin can be installed through the IntelliJ IDEA plugin manager. We need to go to IDE Settings in the Settings window, as shown in the following screenshot:

Selecting the Gradle plugin from the list of available plugins

We will select the Gradle plugin to install it in the IDE. Once we have installed the plugin, we can then import a project. By default, it is already installed.

Importing a project

To import an existing Gradle project, perform the following steps:

  1. We will start the Import Project wizard from the startup screen. Alternatively...

Summary


When we develop applications, we usually develop the code with an IDE. In this chapter, we saw how to use the Gradle plugins in Eclipse, Eclipse WTP, and IDEA to generate project files for Eclipse and IntelliJ IDEA.

The plugins have a DSL to change the configuration before the files are generated. We can also use hooks to change the model objects before and after the DSL is applied. At the lowest level, we can use the withXml hook to alter the XML content before the file is written to the disk.

Both Eclipse and IntelliJ IDEA have plugins to import an existing Gradle project. We can then work with the project from within the IDE. Extra dependencies or changes are reflected in the classpath project files so that the code can be compiled with the IDE's compiler. We can also run Gradle tasks from within the IDE, so we don't have to leave our favorite IDE if we want to use Gradle.

In this book, we have seen the power of Gradle as a build tool. The Gradle syntax is very consistent and compact...

lock icon The rest of the chapter is locked
arrow left Previous Chapter
You have been reading a chapter from
Gradle Effective Implementations Guide - Second Edition
Published in: May 2016 Publisher: ISBN-13: 9781784394974
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}