Setting up your Gradle project

Open the project you built in the Creating your first Gradle project chapter. Use a command-line, text editor, or IDE as you prefer.

Adding the Java code

Add a directory src/main/java for the main application code.

Within the new directory, create directories for the Java package com/gradlehero/languageapp.

Within the new package, create a file SayHello.java and open it for editing.

Paste in the following Java code:

package com.gradlehero.languageapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class SayHello {
    public static void main(String[] args) throws IOException {
        String language = args[0];

        InputStream resourceStream = SayHello.class.getClassLoader().getResourceAsStream(language + ".txt");
        assert resourceStream != null;
        BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(resourceStream, StandardCharsets.UTF_8));

        System.out.println(bufferedInputStream.readLine());
    }
}

Adding the resources

Create a new directory under src/main called resources.

Add a file en.txt with contents Hello!.

Add a file es.txt with contents Hola!.

Building the application

Open build.gradle.kts and delete the comment.

Insert this plugin configuration:

plugins {
    java
}

See what Gradle tasks we have available:

./gradlew tasks (Linux/Mac)

gradlew.bat tasks (Windows)

All the tasks listed under Build tasks have been added to the project by the Java plugin.

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 classes of the 'main' feature.
testClasses - Assembles test classes.

To see all Gradle tasks, including compileJava and processResources run:

./gradlew tasks --all (Linux/Mac)

gradlew.bat tasks --all (Windows)

These tasks appear under Other tasks.

Other tasks
-----------
compileJava - Compiles main Java source.
compileTestJava - Compiles test Java source.
components - Displays the components produced by root project 'get-going-with-gradle-practical'. [deprecated]
dependentComponents - Displays the dependent components of components in root project 'get-going-with-gradle-practical'. [deprecated]
model - Displays the configuration model of root project 'get-going-with-gradle-practical'. [deprecated]
prepareKotlinBuildScriptModel
processResources - Processes main resources.
processTestResources - Processes test resources.

Compile the Java classes:

./gradlew compileJava (Linux/Mac)

gradlew.bat compileJava (Windows)

Look for output in build/classes/java/main.

$ ls build/classes/java/main/com/gradlehero/languageapp/
SayHello.class

Process resources:

./gradlew processResources (Linux/Mac)

gradlew.bat processResources (Windows)

Look for output in build/resources/main.

$ ls build/resources/main/
en.txt  es.tst

Generate a jar file:

./gradlew jar (Linux/Mac)

gradlew.bat jar (Windows)

Look for the jar file in build/libs.

$ ls build/libs/
get-going-with-gradle-practical.jar

Running the application

Run the following Java command:

java -jar build/libs/get-going-with-gradle.jar en (Linux/Mac/Windows)

You’ll see an error which says no main manifest attribute.

Add this jar configuration to the end of build.gradle.kts:

tasks.named<Jar>("jar") {
  manifest {
    attributes["Main-Class"] = "com.gradlehero.languageapp.SayHello"
  }
}

This configuration sets up a Main-Class attribute in the jar manifest, which tells Java which class to run when the jar is executed.

Build the jar file again:

./gradlew jar (Linux/Mac)

gradlew.bat jar (Windows)

Rerun the Java command:

java -jar build/libs/get-going-with-gradle.jar en (Linux/Mac/Windows)

You should see the output text Hello!.

Run the same Java command but replace en with es for Spanish

java -jar build/libs/get-going-with-gradle.jar es (Linux/Mac/Windows)

You should see the output text Hola!.

Awesome! You’ve just built your first Java application with Gradle.

Testing the application

Tests live in src/test/java, so under src create a new directory test/java.

Within this directory create the same package structure by creating directories com/gradlehero/languageapp.

Add a new file SayHelloTest.java and open it for editing.

Paste in the following Java code:

package com.gradlehero.languageapp;

import org.junit.jupiter.api.Test;

import com.gradlehero.languageapp.SayHello;

import java.io.IOException;

public class SayHelloTest {
    @Test
    public void testSayHello() throws IOException {
        SayHello.main(new String[]{"en"});
    }
}

Try running the tests:

./gradlew test (Linux/Mac)

gradlew.bat test (Windows)

You’ll get an error here saying that it can’t find the org.junit.jupiter.api package.

Add this dependency configuration block to build.gradle.kts, after the plugins:

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}

Add this repositories configuration block, putting it just before the dependencies:

repositories {
    mavenCentral()
}

Rerun the test command:

./gradlew test (Linux/Mac)

gradlew.bat test (Windows)

The build should be successful.

Open the test report in a browser, located in build/reports/tests/test/index.html.

You’ll see 0 tests have been run.

Test Summary
0
tests

0
failures

0
ignored

-
duration

-
successful

Add this test configuration block to build.gradle.kts, below the dependencies block:

tasks.named<Test>("test") {
    useJUnitPlatform()
}

This configures tests to use the Junit 5 platform.

Run the tests again:

./gradlew test (Linux/Mac)

gradlew.bat test (Windows)

Open the test report again in a browser, located in build/reports/tests/test/index.html.

You should now see that 1 test was run, with a 100% success rate.

Test Summary
1
tests

0
failures

0
ignored

0.019s
duration

100%
successful

Commit your changes

Get all the files ready to be committed:

git add .

See what files are staged for commit:

git status

Commit everything:

git commit -m "Create Java project for language app."

Good work! You’ve just built, run, and tested your first Java project with Gradle!