- Why Use Gradle Versus Maven or Ant?
- Installing Gradle
- Creating Tasks
- Using the Gradle Wrapper
Installing Gradle
To install Gradle, make sure you have Java already on your machine, and download the binary release from http://gradle.org. Extract the archive somewhere, and add the location to your PATH. It would be handy to create a GRADLE_HOME environment variable as well.
You can check your installation by running
gradle -v
If you receive non-error text, you are all set.
Creating Your First Gradle Project
Before we get started, navigate to [link] and download our sample projects for this article. We will first be working with the directory labeled quickstart. It contains a simple Swing application that displays a String. To compile and run this project, we need a Gradle build file named build.gradle. It's a Java application so it will use the Java plugin. We didn't deviate from Maven standards so we only need the following line in our build.gradle file:
apply plugin: 'java'
The Java plugin provides a bunch of tasks such as building and testing the project, retrieving build dependencies, and creating an archive of the project. You can discover the enabled tasks by running the following at the command line:
gradle tasks
You execute a task but executing gradle <taskname>. If you are building a library that will be used in other projects and has no local or remote dependencies, you could get along with the tasks provided.
Our project shows a GUI display, so we need to add an additional plugin to actually run Quickstart's build.gradle file. This file has a couple extra lines. In its final state, the file appears as:
apply plugin: 'java' apply plugin: 'application' mainClassName = 'quickstart.QuickstartApp'
If we re-run gradle tasks, we can see that the application plugin adds tasks for running the application and packaging it. When you run one of the tasks, Gradle creates an executable script file using the specified mainClassName and includes the project's dependencies (at this point none). Run gradle run in the Quickstart directory and you should see Figure 1.