Show List

Sample Gradle Script

Gradle builds are described via one or more build.gradle files. The file is located in the root folder of the project. The build file defies and project and its tasks. Each task represents a piece of work such as compiling or running the code.

Gradle supports automatic download and configuration of the dependent jars from Maven and Ivy repositories. 

Gradle supports plugins to extend its core functionality. For example "java" plugin is used for tasks such as compiling java code and creating jar. "application" plugin is used to create executable JVM application and to package the application. Here is the list of some popular Gradle plugins https://github.com/ksoichiro/awesome-gradle.

The example we are going to see is to compile a Java program, run it and create a jar.

Java Class

Create gradle-project folder on your computer. Create sub directories are add below class in the gradle-project/src/main/java folder.

Hello.java
import org.joda.time.LocalTime;

public class Hello
{  
    public static void main(String args[])
    {
        LocalTime currentTime = new LocalTime();
        System.out.println("Hello World. The time now is " + currentTime);    
    }
}
Note that this class has a dependency on joda-time jar.

Gradle Build

Put the below build file in the root folder gradle-project.

build.gradle :
apply plugin : "application"

group 'org.example'
version '1.0.0'

repositories {
    // Define where the dependencies are to be looked for
    mavenCentral()
}

dependencies {
    //Jars the application is dependent on
    implementation "joda-time:joda-time:2.2"
}

application {
    // Define the main class for the application.
    mainClass = 'Hello'
}

  • "application" plugin is going to be used to compile and package the application
  • repositories section identifies that Maven central repository will be used to resolve the dependencies.
  • dependencies section lists the dependencies. Here are different option for purpose of dependencies.
  • compileOnly — for dependencies that are necessary to compile your production code but shouldn’t be part of the runtime classpath
  • implementation (supersedes compile) — used for compilation and runtime
  • runtimeOnly (supersedes runtime) — only used at runtime, not for compilation
  • testCompileOnly — same as compileOnly except it’s for the tests
  • testImplementation — test equivalent of implementation
  • testRuntimeOnly — test equivalent of runtimeOnly
  • application plugin requires main class to be defined.
Open command line on the root folder "gradle-project" and run command "gradle tasks". It will list all the tasks available to run. It includes all the inbuilt gradle tasks and the tasks provided by the plugin
C:\Users\mail2\Downloads\gradle-project>gradle tasks
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project 'gradle-project'
------------------------------------------------------------

Application tasks
-----------------
run - Runs this project as a JVM application

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle-project'.
dependencies - Displays all dependencies declared in root project 'gradle-project'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle-project'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
outgoingVariants - Displays the outgoing variants of root project 'gradle-project'.
projects - Displays the sub-projects of root project 'gradle-project'.
properties - Displays the properties of root project 'gradle-project'.
resolvableConfigurations - Displays the configurations that can be resolved in root project 'gradle-project'.
tasks - Displays the tasks runnable from root project 'gradle-project'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the test suite.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL in 5s
1 actionable task: 1 executed
Now run "gradle run" command. It will compile the java classes and will run the application.

C:\Users\mail2\Downloads\gradle-project>gradle run
> Task :run
Hello World. The time now is 13:40:42.730

BUILD SUCCESSFUL in 2s
2 actionable tasks: 2 executed
A build folder also gets created in the root having the generated files.

Running the "gradle build" command will also generate the jar for the application.

Source Code:
https://github.com/it-code-lab/gradle-sample-project

    Leave a Comment


  • captcha text