Ubuntu, Eclipse, and Gradle Setup


The purpose of this tutorial is to show you how to setup a project using Eclipse, Gradle, jUnit, JaCoCo, and Mockito.

The project creates a Java executable. This is probablywhat you need for HW-1.

At the time of this writing the following software versions are being used:

  • Ubuntu 20.04
  • Eclipse 2021-12
  • Gradle 7.4
  • jUnit 5

Gradle is an open-source build automation tool that is designed to be flexible enough to build almost any type of software. Such a tool is pretty much a must have for any project except the smallest. Gradle, like Maven, allows you to define project dependencies and manage them.

jUnit is a very popular automated unit testing framework you should be familiar with. We're going to use the most recent incarnation of jUnit, 5+.

JaCoCo is a sophisticated, free code coverage library for Java, which has beencreated by the EclEmma team.

Mockito is used for creating mock objects and greatly enhances your ability to do automated unit testing.


A. Prerequisites

We're going to start with a fresh installation of Ubuntul 20.04.3

B. Install Java

Installing Gradle on Linux requires the Java 8 or higher version and Java doesn’t come pre-installed.

Type the following commands in your terminal:

  
$ sudo apt update $ sudo apt install openjdk-17-jdk

Check the version to ensure that it has been installed successfully:

  
$ java -version openjdk version "17.0.1" 2021-10-19 OpenJDK Runtime Environment (build 17.0.1+12-Ubuntu-120.04) OpenJDK 64-Bit Server VM (build 17.0.1+12-Ubuntu-120.04, mixed mode, sharing) $ javac -version javac 17.0.1

C. Install Eclipse

(i) Download the Eclipse installer from the Eclipse website. The link will download the 64-bit version.

NOTE: if you're running Linux as a virtual machine inside one of the new Macs -- equipped with an ARM processor -- then you'll have to download a different installer.

(ii) Uncompress and unarchive the installer.

    
$ cd ~/Downloads $ gunzip eclipse-inst-jre-linux64.tar.gz $ tar xvf eclipse-inst-jre-linux64.tar

(iii) Navigate with your browser to ~/Downloads/eclipse-installer and then double click on the installer (eclipse-inst) to run it. Select "Eclipse IDE for Java Developers" and complete the installation.

Congratulations, you installed the 2021-12 version of Eclipse!

D. Install Gradle

(i) Download the Gradle binary-only zip file in the /tmp directory using the following wget command:

  
$ wget https://services.gradle.org/distributions/gradle-7.4-bin.zip -P /tmp

(ii) Unzip the downloaded file into the destination directory and then create a symbolic link to make it easier to maintain:

  
$ sudo unzip -d /opt/gradle /tmp/gradle-7.4-bin.zip $ sudo ln -s /opt/gradle/gradle-7.4 /opt/gradle/latest

Later on, when upgrading Gradle, unzip the newer version and change the symlink to point to it.

(iv) Set-up Environment Variables

  
$ sudo vi /etc/profile.d/gradle.sh

and add the following two lines to the file:

  export GRADLE_HOME=/opt/gradle/latest
  export PATH=${GRADLE_HOME}/bin:${PATH}

NOTE: you can use any text editor you want, not just vi.

Make the gradle.sh executable:

  
$ sudo chmod +x /etc/profile.d/gradle.sh

Load the environment variables in the current shell session:

  
$ source /etc/profile.d/gradle.sh

(v) Verify the Gradle installation

  
$ gradle -v

E. Create a Gradle Project

(i) Go into your Eclipse workspace:

  
$ cd ~/eclipse-workspace

(ii) Create directory for the project:

  
$ cd ~/eclipse-workspace $ mkdir HW1-gradle $ cd HW1-gradle

(iii) Create/initialize an 'application' project:

  
$ gradle init

NOTE: make following selections when prompted:

  • 2: application
  • 3: Java
  • 1: no - only one application project
  • 1: Groovy
  • no
  • 4: JUnit Jupiter
  • HW1-gradle
  • edu.iit.cs445.spring22

(iv) Open Eclipse and import the project you just created from the command line:

  • File > Import > Gradle > Existing Gradle Project > Next > Next > Browse and select the directory you created > Next > check the 'Override workspace settings' , select 'Local installation directory' and type /opt/gradle/latest in the text box; then select Next and Finish.

The sample project should now be visible in your Eclipse workspace.

NOTE: For some odd reason the app folder appear separate from the HW1-gradle project you created; don't worry, it appears to be just a visual artifact.

(v) While still in Eclipse expand the 'app' folder then 'src/main/java' followed by 'edu.iit.cs445.spring2022' and then make any changes you want to the existing 'App.java' source file. When done with your changes don't forget to right-click on the name of the project (HW1-gradle) and select Gradle > Refresh Gradle project

NOTE: if you change the name of the class where the main() function is located, then you'll have to also edit the build.gradle file to reflect the change, look for the mainClass line.

The unit test corresponding to App.java is named AppTest.java and can be found under 'src/test/java'.

F. Build, run, get unit test coverage within Eclipse

You can run tests by right clicking on the tests file, i.e. AppTest.java and then selecting Run As > JUnit Test.

You can get unit test coverage by right clicking on the project name and then selecting Coverage As > Coverage Configurations; select AppTest followed by Coverage and then switch to the Coverage tab in the lower right pane of Eclipse to get the coverage summary for the project.

You can build the executable for the application by right clicking on the project name file, and then selecting Run As > Run Configurations; select Gradle Task > HW1-gradle-build and then watch the messages in the Console.

NOTE: Running the build task will also run the tests, you can find the results by loading ~/eclipse-workspace/HW1-gradle/app/build/reports/tests/test/classes/edu.iit.cs445.spring22.AppTest.html in the browser.

You can run the application by right clicking on the project name file, and then selecting Run As > Run Configurations; select Gradle Task > HW1-gradle-run and then watch the messages in the Console.

G. Build, run, get unit test coverage from command line

(i) Go back to the terminal; you should still be in ~/eclipse-workspace/HW1-gradle/

(ii) Build the project:

  
$ ./gradlew build

NOTE: Running the build task will also run the tests, you can find the results by loading ~/eclipse-workspace/HW1-gradle/app/build/reports/tests/test/classes/edu.iit.cs445.spring22.AppTest.html in the browser.

(iii) Run the project:

  
$ ./gradlew run

You should see 'Hello World!' printed on the terminal, unless you have changed the content of the App.java class.

You can view all tasks available for execution by running:

  
$ ./gradlew tasks

H. Get detailed unit test coverage with JaCoCo

While in Eclipse edit the gradle.build for the project and add the following in the 'plugins' section:

  id 'jacoco'

NOTE: The new build.gradle file for your project should look like this.

Refresh the project (right click on project name, then select Gradle and Refresh Gradle Project)

  
$ cd ~/eclipse-workspace/HW1-gradle $ ./gradlew clean $ ./gradlew build $ ./gradlew jacocoTestReport

Now use your browser to open the file ~/eclipse-workspace/HW1-gradle/app/build/reports/jacoco/test/html/edu.iit.cs445.spring22/index.html . This is your detailed unit-test coverage report.


Last update: Feb 22, 2022 Virgil Bistriceanu cs445 Computer Science