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:
1apply plugin: 'java'
  1. 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.

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

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

Make sure your folder structure looks like following:

1GradleSample
2  |
3  |-build.gradle
4  |-src
5     |-main
6        |-java
7           |-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:

 1:tasks
 2
 3------------------------------------------------------------
 4All tasks runnable from root project
 5------------------------------------------------------------
 6
 7Build tasks
 8-----------
 9assemble - Assembles the outputs of this project.
10build - Assembles and tests this project.
11buildDependents - Assembles and tests this project and all projects that depend on it.
12buildNeeded - Assembles and tests this project and all projects it depends on.
13classes - Assembles main classes.
14clean - Deletes the build directory.
15jar - Assembles a jar archive containing the main classes.
16testClasses - Assembles test classes.
17
18Build Setup tasks
19-----------------
20init - Initializes a new Gradle build. [incubating]
21wrapper - Generates Gradle wrapper files. [incubating]
22
23Documentation tasks
24-------------------
25javadoc - Generates Javadoc API documentation for the main source code.
26
27Help tasks
28----------
29buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle-hello-world-java'.
30components - Displays the components produced by root project 'gradle-hello-world-java'. [incubating]
31dependencies - Displays all dependencies declared in root project 'gradle-hello-world-java'.
32dependencyInsight - Displays the insight into a specific dependency in root project 'gradle-hello-world-java'.
33dependentComponents - Displays the dependent components of components in root project 'gradle-hello-world-java'. [incubating]
34help - Displays a help message.
35model - Displays the configuration model of root project 'gradle-hello-world-java'. [incubating]
36projects - Displays the sub-projects of root project 'gradle-hello-world-java'.
37properties - Displays the properties of root project 'gradle-hello-world-java'.
38tasks - Displays the tasks runnable from root project 'gradle-hello-world-java'.
39
40Verification tasks
41------------------
42check - Runs all checks.
43test - Runs the unit tests.
44
45Rules
46-----
47Pattern: clean<TaskName>: Cleans the output files of a task.
48Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
49Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
50
51To see all tasks and more detail, run gradle tasks --all
52
53To see more detail about a task, run gradle help --task <task>
54
55BUILD SUCCESSFUL
56
57Total time: 0.822 secs
58

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.

1java -cp build\libs\GradleSample.jar HelloWorld

Output:

1Hello 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.

comments powered by Disqus