Getting Started With Gradle
To use gradle, download the binaries and add the bin directory from the extracted directory to the environment PATH
. Then, create the directory structure below:
settings.gradle
app
build.gradle
src
main
java
com
pockettheories
Main.java
The settings.gradle file contains the name of the project and the include directive has the name of the directory containing build.gradle:
rootProject.name = 'gradled'
include('app')
Within the sub-project directory (app), the build.gradle file is the equivalent of Maven’s pom.xml
file - it identifies whether the sub-project is an application or a library, tells gradle to use Maven’s repositories, defines repositories, and sets the class name for the main class:
plugins {
id 'application' // application | lib
}
repositories {
mavenCentral()
}
dependencies {
}
application { // application | lib
mainClass='com.pockettheories.Main'
}
The app/src/main/java/com/pockettheories/Main.java
file contains the Java source code, which just contains a System.out.println
. Execute the application with:
gradle run
You can get gradle to perform other build stages: gradle clean, gradle build [–scan], gradle jar
When creating a jar file, you ideally want the manifest to contain the main class name. This can be done by adding the following to the build.gradle file:
tasks.named('jar') {
manifest {
attributes('Main-Class': 'com.pockettheories.Main')
}
}