Install Gradle using one of the many methods:
https://gradle.org/install/
- Open a terminal, run gradle –version. If it runs, you are good to go.
Create project folder
- Create a folder somewhere as the root folder for your Java app (e.g
C:\Users\Bob\YourProjectName
) - Open a terminal at the root folder, and run gradle init
It will let your choose a few options. Essentially:
- Type of project:
(2) application
- Language:
(3) Java
(1) No - only one application project
- Build script DSL:
(1) Groovy
- Test framework:
(1) JUnit 4
- Project name:
YourProjectName
- Source package:
YourProjectName
Then the project root folder will be populated by Gradle.
You will need to modify .\YourProjectName\app\build.gradle
:
- Add id ‘jacoco’ under plugins { }
Where your Java files will go:
-
Java application codes:
.\YourProjectName\app\src\main\java\YourProjectName
- YourProjectName.java
-
JUnit test files:
.\YourProjectName\app\src\test\java\YourProjectName
- YourProjectNameTest.java
With your terminal at the project root (.\YourProjectName\
), run the following commands:
Build your app: gradle build
- If it is successful, it means your programme is compiled without error.
Generate Jacoco report: gradle test jacocoTestReport
- Report is generated at: .\YourProjectName\app\build\reports\jacoco\test\html
An issue that I ran into:
- If your code is not shown in the Jacoco report, You might need to add
package YourProjectName;
at the beginning of your Java files and test files.