Introduction to Gradle

Here’s Lab 4 with a twist. Instead of writing your own code, why not pull it from GitHub, and then build it with Gradle, just like you would do if you were a DevOps engineer out in the field.

Part 1-3

Create a folder under C:\workspace\labs\gradle and in that directory clone a GitHub repo:

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/gradle/
$ git clone https://github.com/cameronmcnz/MyProject.git

You may need to then move into the MyProject directory

cd My*

Compile Java Code with javac

From the MyProject directory, look at the BadJava.java file. Then compile it with the Java compiler:

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/gradle/MyProject
$ javac BadJava.java

Then run the bytecode

$ java BadJava

Gradle Tasks

See what Gradle tasks are available:

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/gradle/MyProject
$ gradle tasks

Then tell Gradle that you’re all about Java:

apply plugin: 'java'

Now run the tasks again and note the difference.

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/gradle/MyProject
$ gradle tasks
Build vs Merge with Maven Gradle and Git
Just because it will merge doesn't mean it will compile.

Compile with Gradle

Compile your code with Gradle:

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/gradle/MyProject (part7)
$ gradle classes

Now clean up your workspace:

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/gradle/MyProject (part7)
$ gradle clean

Now build a JAR file:

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/gradle/MyProject (part7)
$ gradle build

And clean up again:

$ gradle clean

Switch Branches

Add and commit your changes:

git add .
git commit -m "done for now"

Then switch branches:

git checkout part5

Part 5

Update your gradle file according to the lab guide.

Commands to issue include:

gradle build
gradle -q run

Switch branches

To switch branches, add and commit again. Then do the checkout:

git add .
git commit -m "done again"

git checkout part7

Run Tests

Look at the code added under the test folders. Follow section 7 in the book and then run the test command. Note that it will fail.

gradle test

Fix the test files so the test will pass. Then run your tests again.

gradle test

That just about covers it!

Java Features
Gradle Lifecycle.
Java Features
Maven Cheat Sheet.