Open the project you built in the Creating your first Gradle project lesson. Use a command-line, text editor, or an IDE as you prefer.
Adding the Java code
-
add a directory src/main/java for the main application code
-
create directories for the Java package com/tomgregory/languageapp
-
create a new file
SayHello.java
and open it for editing -
paste in the following Java code:
package com.tomgregory.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 and delete the comment.
-
insert this plugins configuration:
plugins { id '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.
-
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.
-
compile the Java classes:
./gradlew compileJava (Linux/Mac)
gradlew.bat compileJava (Windows)
-
look for output in build/classes/java/main
-
process resources:
./gradlew processResources (Linux/Mac)
gradlew.bat processResources (Windows)
-
look for output in build/resources/main
-
generate a jar file:
./gradlew jar (Linux/Mac)
gradlew.bat jar (Windows)
-
look for the jar file in build/libs
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:
jar { manifest { attributes('Main-Class': 'com.tomgregory.languageapp.SayHello') } }
-
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 text Hello! output.
-
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 text Hola! output.
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 directories com/tomgregory/languageapp for the same package structure
-
add a new file SayHelloTest.java and open it for editing
-
paste in the following Java code:
package com.tomgregory.languageapp; import org.junit.jupiter.api.Test; 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, after the plugins:
dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.6.3' }
-
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 that 0 tests have been run.
-
add this test configuration block below the dependencies block:
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.
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 just built, run, and tested your first Java project with Gradle.