Gradle Hello World Example Java

What is Gradle?

Gradle is a very popular build tool today in the ecosystem of Java based languages. Although it can be used for other languages as well. Gradle is based on the principal of convention over configuration which is also been popular with Maven. Unlike XML in Maven, Gradle uses Groovy for its language which makes it a very powerful tool. One can do almost anything using Gradle. It has very good plugin system with various ready-made plugins available which can be used for different situations / environments / purposes.

More importantly, I must mention Gradle is very popular for it’s features like automatic dependency management, multi project build, deployment to artifact repository etc.

A simple Hello World example with Gradle.

Before starting, make sure you have installed latest JDK and Gradle from their respective website and set their binary available through PATH.

  1. Create a folder say D:/GradleSample
  2. Create a build.gradle file under the GradleSample folder with just following content:

    apply plugin: 'java'
    
  3. Create src/main/java folder under the GradleSample folder. Note that this is the default (convention) folder for placing the source code of Java project.

  4. Create a simple Java file named HelloWorld.java with following content:

    public class HelloWorld {
    public static void main(String[] argv) {
        System.out.println("Hello gradle world");
    }
    }
    

Make sure your folder structure looks like following:

GradleSample
  |
  |-build.gradle
  |-src
     |-main
        |-java
           |-HelloWorld.java

Now open a command prompt and go to GradleSample folder. You can run gradle tasks command to see what are the tasks available. It should print like following:

:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

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. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

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

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle-hello-world-java'.
components - Displays the components produced by root project 'gradle-hello-world-java'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradle-hello-world-java'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle-hello-world-java'.
dependentComponents - Displays the dependent components of components in root project 'gradle-hello-world-java'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'gradle-hello-world-java'. [incubating]
projects - Displays the sub-projects of root project 'gradle-hello-world-java'.
properties - Displays the properties of root project 'gradle-hello-world-java'.
tasks - Displays the tasks runnable from root project 'gradle-hello-world-java'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to 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

Total time: 0.822 secs

In this example, we will use the clean, build and jar tasks. Re-check the description beside those commands in above output. Later you can experiment with other tasks to learn those.

Now let us run the gradle clean build command. This should create build/classes folder under which the classes are generated. Check that folder.

Now run gradle jar command. This should create a jar file named GradleSample.jar under build/libs folder.

Run the jar using following command. This should run the HelloWorld sample and produce output as next.

java -cp build\libs\GradleSample.jar HelloWorld

Output:

Hello gradle world



Gradle dependency management

One of the useful facilities Gradle provides is dependency management. It is very easy to declare what are the dependency for the project


I would explain about more practical examples later. Stay tuned.

Hansaraj avatar
About Hansaraj
Hansaraj is a Software Engineer experienced in Java, Groovy, JavaScript, SQL, C#, C++
comments powered by Disqus