Port of schuchert.wikispaces.com


Creating a Java project from scratch using Gradle

Creating a Java project from scratch using Gradle

What follows is first a summary of the steps required, then the prerequisites that make all of this magic possible, more details steps with example output, and some closing notes.

It seems that copying all of the commands in one fell swoop and pasting them into a terminal stops after the gradle init ... step. If so, copy the rest and try again. If you know how to fix that, please let me know.

Summary of Steps

Create a directory to hold the project

vagrant@vagrant-ubuntu16:~/src$mkdir smoke

Switch to that directory

vagrant@vagrant-ubuntu16:~/src$cd smoke

Initialize the project using gradle

vagrant@vagrant-ubuntu16:~/src/smoke$gradle init --type java-application

BUILD SUCCESSFUL in 0s
2 actionable tasks: 1 executed, 1 up-to-date

Gradle created several files, have a look

vagrant@vagrant-ubuntu16:~/src/smoke$ls
build.gradle  gradle  gradlew  gradlew.bat  settings.gradle  src

Try running tests

The first run of your freshly-created application may take longer due to the need to download Java library files

vagrant@vagrant-ubuntu16:~/src/smoke$gradle test

BUILD SUCCESSFUL in 4s
3 actionable tasks: 3 executed

Re-run the tests

If they failed before, e.g., you needed to configure a proxy, then they will run this time. If they passed last time, then they will not re-run.

vagrant@vagrant-ubuntu16:~/src/smoke$gradle test

BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 up-to-date

Force re-execution

After a task (tests in this case) has passed, it will not re-run if there are no relevant changes. You can force re-execution with the --retun-tasks parameter to Gradle.

Notice “executed” below versus “up-to-date” above:

vagrant@vagrant-ubuntu16:~/src/smoke$gradle test --rerun-tasks

BUILD SUCCESSFUL in 3s
3 actionable tasks: 3 executed

Create the application jar

The use of --rerun-tasks is not necessary here but doing this should provide more consistent results between this example and what you see on your screen.

vagrant@vagrant-ubuntu16:~/src/smoke$gradle build --rerun-tasks

BUILD SUCCESSFUL in 4s
7 actionable tasks: 7 executed

Finally, run the application from the command line

vagrant@vagrant-ubuntu16:~/src/smoke$java -cp build/libs/smoke.jar App
Hello world.

Prerequisites

A working terminal

These steps assume a bash-like terminal from which you can run commands. Possibilities include:

System Terminal Option
Windows git bash
Mac OS Terminal - built in
Unix: Terminal - built in

Java Development Kit installed and working at the command line:

vagrant@vagran-ubuntu16:~/src$ javac -version
javac 1.8.0_181

Gradle installed and runs at the command line:

vagrant@vagran-ubuntu16:~/src$ gradle -version

------------------------------------------------------------
Gradle 4.10
------------------------------------------------------------

Build time:   2018-08-27 18:35:06 UTC
Revision:     ee3751ed9f2034effc1f0072c2b2ee74b5dce67d

Kotlin DSL:   1.0-rc-3
Kotlin:       1.2.60
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_181 (Oracle Corporation 25.181-b13)
OS:           Linux 4.15.0-33-generic amd64

Internet access

The terminal needs access to the internet. If these commands fail due to needing to configure the proxy, update settings.gradle:

// ... snip
rootProject.name = 'smoke'
systemProp.https.proxyHost=<proxy url or ip>
systemProp.https.proxyPort=<port number>
systemProp.https.nonProxyHosts=localhost|120.0.01|<proxy url or ip>

Step by Step Instructions with Example Output

Create a directory to hold the project

vagrant@vagrant-ubuntu16:~/src$mkdir smoke

Switch to that directory

vagrant@vagrant-ubuntu16:~/src$cd smoke

Initialize the project using gradle

vagrant@vagrant-ubuntu16:~/src/smoke$gradle init --type java-application

BUILD SUCCESSFUL in 0s
2 actionable tasks: 1 executed, 1 up-to-date

Gradle created several files, have a look

vagrant@vagrant-ubuntu16:~/src/smoke$ls
build.gradle  gradle  gradlew  gradlew.bat  settings.gradle  src

Try running tests

The first run of your freshly-created application may take longer due to the need to download Java library files

vagrant@vagrant-ubuntu16:~/src/smoke$gradle test

BUILD SUCCESSFUL in 4s
3 actionable tasks: 3 executed

Re-run the tests

If they failed before, e.g., you needed to configure a proxy, then they will run this time. If they passed last time, then they will not re-run.

vagrant@vagrant-ubuntu16:~/src/smoke$gradle test

BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 up-to-date

Force re-execution

After a task (tests in this case) has passed, it will not re-run if there are no relevant changes. You can force re-execution with the --retun-tasks parameter to Gradle.

Notice “executed” below versus “up-to-date” above:

vagrant@vagrant-ubuntu16:~/src/smoke$gradle test --rerun-tasks

BUILD SUCCESSFUL in 3s
3 actionable tasks: 3 executed

Create the application jar

The use of --rerun-tasks is not necessary here but doing this should provide more consistent results between this example and what you see on your screen.

vagrant@vagrant-ubuntu16:~/src/smoke$gradle build --rerun-tasks

BUILD SUCCESSFUL in 4s
7 actionable tasks: 7 executed

Finally, run the application from the command line

vagrant@vagrant-ubuntu16:~/src/smoke$java -cp build/libs/smoke.jar App
Hello world.

Published

05 September 2018

Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.