Building First Java Program with Gradle from Command Line

Prerequisites

  1. You need to install, configure and get familiar with gradle as outlined in the previous note

  2. Gradle 1.x requires a Java JDK 5 or higher to be installed and the location needs to be added to an environment variable JAVA_HOME. 

  3. Gradle 2.x requires a Java JDK 6 or higher to be installed and the location needs to be added to an environment variable JAVA_HOME. 

 

Hello World Java Program

We will create a simple hello world program and use the default conventions for gradle java plugin with minimal code changes.

 

Gradle default folder structure for source code

Gradle has many default convensions so that we don't have to explicitly tell many things. We will use those default conventions here.

Assuming that our classes are defined with a package structure com.javajee, we should follow below folder structure.

  • HelloWorld.java should be in a folder structure corresponding to the package defined (e.g. com\javajee). If you are not clear about java packages and how to define package structure, please understand and learn it first (without Gradle) @ Packaging in Java.

  • This folder structure (e.g. com\javajee) should be within a folder structure according to gradle conventions: src\main\java\<class-package>. So the final folder structure for your java source file (according to above package structure) will be src\main\java\com\javajee.

 

HelloWorld.java

Create a hello world java program in a file HelloWorld.java:

package com.javjee;

public class HelloWorld {

  public static void main(String[] args)

  {

    System.out.println("Hello World");

  }

}

Refer to ‘Beginning Java Book’ to download and install java, and start writing java programs.

 

Gradle build file

Next, create the build file (build.gradle) as:

apply plugin: 'java'

 

Compiling the code using gradle

Finally go to parent folder of the src folder and run gradle build command:

gradle build

This will print:

:compileJava

:processResources UP-TO-DATE

:classes

:jar

:assemble

:compileTestJava UP-TO-DATE

:processTestResources UP-TO-DATE

:testClasses UP-TO-DATE

:test UP-TO-DATE

:check UP-TO-DATE

:build

BUILD SUCCESSFUL

Total time: 4.767 secs

This will create below folders and files within them:

.gradle\1.11\taskArtifacts contains:

  • cache.properties

  • cache.properties.lock

  • fileHashes.bin

  • fileSnapshots.bin

  • askArtifacts.bin

  • outputFileStates.bin

  • taskArtifacts.bin

Other folders and files created are:

  • build\classes\main\com\javjee\HelloWorld.class

  • build\libs\GRADLE.jar that contains ‘com\javjee\ HelloWorld.class’. Here jar file has been created using the project directory name as the filename.

  • build\dependency-cache (This example doesn’t have any library dependencies, so there’s nothing in this folder now.)

  • build\tmp\jar

 

Executing the class

You can run the above class file using the java command from command line as:

java -cp build\classes\main com.javjee.HelloWorld

You can also use the JavaExec task in Gradle to execute it, which we will see in detail later.

 

Summary of steps

Note that gradle compiled the source, created class files and even packaged it into a jar using its conventions without we telling gradle to do any of these things.

By applying the Java plug-in, the build script automatically inherits a set of tasks and you never directly see its code. You can however modify the behavior of these tasks using the task configuration, doFirst(), and doLast() methods. We will see all these in detail in coming tutorials. Gradle also has a few built-in tasks, like tasks and properties which aren’t provided by any plug-in or any code you write, but are just a standard part of the Gradle DSL.

 

Documentation

You can find the documentation on standard Gradle features @ docs/dsl/index.html within the gradle installation.

 

Software Versions Tested Against

Examples in this note has been tested against Gradle versions 1.11 and 2.1.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)