Engineering Full Stack Apps with Java and JavaScript
You need to install, configure and get familiar with gradle as outlined in the previous note.
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.
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.
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.
You can find the documentation on standard Gradle features @ docs/dsl/index.html within the gradle installation.
Examples in this note has been tested against Gradle versions 1.11 and 2.1.