Gradle is a build automation tool used to automate build processes. There are many ways of integrating Gradle into a project. This project template uses the Gradle wrapper approach.
- Basics
- Adding Gradle to the Project
- Running Gradle Tasks
- Adding Plugins
- Adding Dependencies
- Further Reading
You use a build file (named build.gradle
) to describes the project to Gradle.
A build file mainly consists of plugins, tasks and properties.
-
Plugins extend the functionality of Gradle. For example, the
java
plugin adds support forJava
projects. -
Tasks are reusable blocks of logic.
For example, the taskclean
simply deletes the project build directory. Tasks can be composed of, or dependent on, other tasks. -
Properties change the behavior of tasks. For instance,
mainClassName
of theapplication
plugin is a compulsory property which tells Gradle which class is the entrypoint to your application. As Gradle favors convention over configuration, there is not much to you need to configure if you follow the recommended directory structure.
This project template already supports Gradle. No further actions are required.
If you followed the setup instructions given, IntelliJ IDEA will identify your project as a Gradle project and you will gain access to the Gradle Toolbar
.
Through the toolbar, you run Gradle tasks and view your project's dependencies.
If the Gradle tasks don't appear in the Gradle window, click the 'refresh' button in the tooolbar to reimport the Gradle project.
To run a task, locate the task in the Gradle toolbar, right-click on a task, and choose run
.
Alternatively, you can type the command in the terminal.
- On Windows:
gradlew <task1> <task2> …
e.g.gradlew clean test
- On Mac/Linux:
./gradlew <task1> <task2> …
e.g../gradlew clean test
Gradle plugins are reusable units of build logic. Most common build tasks are bundled into core plugins provided by Gradle. CheckStyle, Shadow, and JUnit are three plugins already added to this project.
CheckStyle is a tool for checking if the code complies with certain style rules.
Checkstyle expects configuration files for checkstyle (e.g., files that specify which style rules to follow) to be in ./config/checkstyle/
by convention.
A sample checkstyle rule configuration is provided in this project as well.
The plugin adds a few tasks to your project.
checkstyleMain
: checks if the main code complies with the style rulescheckstyleTest
: checks if the test code complies with the style rules
For example, you can run gradlew checkstyleMain checkstyleTest
to verify that all your code complies with the style rules.
Resources:
Shadow is a plugin that packages an application into an executable jar file.
The plugin can be configured by setting some properties.
By default, it produces a jar file with the name in the format of {archiveBaseName}-{archiveVersion}.jar
(the two properties can be set in the build.gradle
file).
You can generate an executable jar by running the command gradlew shadowJar
which publishes an executable jar to ./build/libs/
.
Resources:
JUnit is a testing framework for Java. By convention, java tests belong in src/test/java
folder. A sample test file is included in this project.
If you have imported your Gradle project into IntelliJ IDEA, you will notice that IDEA is able to mark the src/test/java
folder as the Test root (colored in green by default) automatically.
You can run a test (e.g., test/java/seedu/duke/DukeTest.java
) by right-clicking on it and choosing Run
.
Resources:
Gradle can automate the management of dependencies to third-party libraries. You just need to add the dependency into the build.gradle
file and Gradle will do the rest.
For example, to add the Natty (a third-party library used for parsing natural lanaguage dates e.g., today
), you simply have to add the following line to the dependencies
section of the build.gradle
file.
compile group: 'com.joestelmach', name: 'natty', version: '0.6'
Tip: Most third-party libararies specify how to add it as a Gradle dependency (example).
Authors:
- Initial Version: Jeffry Lum