Gradle is used very often in the industry and I felt it will be good to cover the basics of Gradle in this article so that it can be used efficiently😄 What is Gradle Gradle is a Build Automation tool and is also Open source. This Article will cover the following topics in Gradle. Creating a Java Application by using Gradle as the build tool Running Unit tests for the Java Application Gradle Build Script and Tasks Installing Gradle You can Install Gradle from https://docs.gradle.org/current/userguide/installation.html Also ensure Gradle is set in the PATH so that gradle commands work. you can verify if gradle is installed and can be accessed using the command gradle -v Also ensure is set. JAVA_HOME Creating a Java Application by Using Gradle as Build Tool Create a folder called as ‘simple-gradle-java-app’ and enter into the folder in command prompt. This will be our project folder. mkdir simple-gradle-java-app simple-gradle-java-app cd Use the following Command to Initialize Gradle for this project gradle init This will ask you to select the type of project to generate. Choose . This is because we are creating an application ‘application’ Next you will be asked to select Implementation Language. Choose . This is because we are creating a Java Application. ‘Java’ Next you will be asked to Choose Build Script DSL. Choose . In this Demo we will be using Groovy as the build script language. ‘Groovy’ Next you will be asked to Select a Test Framework. Choose . ‘Junit Jupiter’ For now you can leave the project name and source package as default. Folder Structure Once the project is created. It would have the following Folder Structure Some of the notable items created in this folder structure are : This is the folder which has all the Java Code in the Application src/main/java : This is folder which has all the test cases for the Above Java code src/test/java : This is the gradle wrapper which is used the run various gradle tasks and Commands. works in Linux and MacOS and is used for Windows. gradlew and gradlew.bat gradlew gradlew.bat : This file indicates which projects to include in the build. In our case we have only one project which is ‘simple-gradle-java-app’. This is set in settings.gradle as rootProject.name = 'simple-gradle-java-app' settings.gradle : This is the file in which the Build Script is written. We will be discussing more on this later in this Blog. build.gradle : This is the folder which has all the Gradle Wrapper files gradle Modifying the Java Class and Its Test Class In The Init Script has automatically Created a Java Class called as . src/main/java App.java This is the Java class which is Automatically generated for us. simple.gradle.java.app; { { ; } { System.out.println( App().getGreeting()); } } package public class App String public getGreeting () return "Hello world." public static void main (String[] args) new Replace the Above Java class with the following Class simple.gradle.java.app; { { (num1 + num2); } { System.out.println( + getSum( , )); } } package public class App public static int getSum ( num1, num2) int int return public static void main (String[] args) "Sum is " 4 5 We have a function here called as which returns sum of 2 numbers. The function calls this function. getSum main In the Init Script has automatically created a Java Test class for us Called as src/test/java AppTest.java The class this is generated is given below simple.gradle.java.app; org.junit.jupiter.api.Test; org.junit.jupiter.api.Assertions.*; { { App classUnderTest = App(); assertNotNull(classUnderTest.getGreeting(), ); } } package import import static class AppTest @Test void appHasAGreeting () new "app should have a greeting" Replace the Above java class with the following simple.gradle.java.app; org.junit.jupiter.api.Test; org.junit.jupiter.api.Assertions.*; { { result = App.getSum( , ); expected = ; assertEquals(expected,result); } { App.main( String[]{}); } } package import import static class AppTest @Test void getSum () int 10 12 int 22 @Test void main () new Here we have written a unit test to check if function is working as expected. getSum Running The Java Application First let us build the project using Gradle. This can be done using the following command Windows gradlew.bat build Linux and MacOS ./gradlew build Once the build is done, a folder called as is created which will have the built project and also it would contain information like the test reports. build Then we can run the Application using the following Command Windows gradlew.bat run Linux and MacOS ./gradlew run This will Run the application and Print the output as Shown Below > Task: run Sum is 9 If needed we can clean the build using the below Command Windows gradlew.bat clean Linux and MacOS ./gradlew clean This will delete the folder. Running The Unit Tests build When we Built the project using the command, it would automatically run the Test classes as well and Generate the Test reports build The Test reports can be found in . Open the index.html file using a browser to see the test results. build/reports/tests/test/index.html Now if you want to run only the tests, then you can use the following command. Windows gradlew.bat test Linux and MacOS ./gradlew test This will also generate the file. The Test report will look as shown below index.html Now Let us make an Error on Purpose In the code Replace with the following App.java simple.gradle.java.app; { { num1; } { System.out.println( + getSum( , )); } } package public class App public static int getSum ( num1, num2) int int return public static void main (String[] args) "Sum is " 4 5 As you can see here, we are always returning num1 instead of num1 + num2 in function which is wrong. getSum Let’s Run the tests now Windows gradlew.bat test Linux and MacOS ./gradlew test You would notice that now it says that the . Let’s check the report in now to see what it is showing Task: Test failed index.html The Error Clearly shows that it Expected the sum to be 22, But then our App logic is returning 10. Hence the Test has Failed. Now modify getSum Function in App.java to return ( num1 + num2 ) and re-run the test and it will pass Gradle Build Script and Tasks has the build script. In this demo we are using Groovy for the build script. build.gradle The build.gradle which was generated has the following snippets. All of these are already Automatically generated for us. So here I will be explaining all the snippets generated Plugins plugins { id id } // Apply the java plugin to add support for Java 'java' // Apply the application plugin to add support for building a CLI application. 'application' help to add additional functionality to Groovy. Here we have a java plugin to Support Java Classes. We also have an Application Plugin so that We can build Applications using Groovy Plugins Dependencies dependencies { implementation testImplementation testRuntimeOnly } // This dependency is used by the application. 'com.google.guava:guava:28.1-jre' // Use JUnit Jupiter API for testing. 'org.junit.jupiter:junit-jupiter-api:5.5.2' // Use JUnit Jupiter Engine for testing. 'org.junit.jupiter:junit-jupiter-engine:5.5.2' is where we add additional libraries need in the project. For Example here we are mentioning that we need dependency for Unit Testing. If you wanted to make a Database connection, then you will need to use a Library like JDBC for it. In that case JDBC will be added as a dependency. Dependencies junit jupiter Repositories repositories { jcenter() } // Use jcenter for resolving dependencies. // You can declare any Maven/Ivy/file repository here. is the location from where Groovy will fetch and download the dependencies. Here we have mentioned that the repository we are using is Jcenter. You can read more about Jcenter . Another Commonly used Repository is Maven Central. You can read more about Maven Central Repositories here here Application application { mainClassName = } // Define the main class for the application. 'simple.gradle.java.app.App' Since we have created a Java application, we need to mention the Java class containing the main class in the build script. In this case it is App.Java. Tasks A Task is a piece of work that the build needs to do. In currently we do not see any tasks defined. But a lot of default tasks already exist. You can see them by typing the following command build.gradle Windows gradlew.bat tasks Linux and MacOS ./gradlew tasks This will give the following output Application tasks ----------------- run - Runs this project as a JVM application Build tasks ----------- assemble - Assembles the outputs of this project. build - Assembles and tests this project. clean - Deletes the build directory. jar - Assembles a jar archive containing the main classes. testClasses - Assembles classes. Documentation tasks ------------------- javadoc - Generates Javadoc API documentation the main code. Help tasks ---------- tasks - Displays the tasks runnable from root project . Verification tasks ------------------ - Runs the unit tests. test for source 'simple-gradle-java-app' test We have already used some of these tasks like , , and . build clean run test The Flexibility of groovy is that we can add Custom Tasks as well. Let’s add a Custom Task and then Run the Task. Add the following code snippet in build.gradle task projectupper { group = description = doLast { String projectName = project.name println println } } 'Custom Tasks' 'Task to get Project Name and Convert it to upper case' "Project Name: $projectName" "Upper case: ${projectName.toUpperCase()}" We have written a simple task here which takes the project name , Coverts the name to upper case and then prints the Name in Upper case. is name of the Task projectupper and are very important since it will list this task in the tasks report whenever we run gradlew.bat tasks group description is where we define the task action. We first get the project Name using , then we convert it to upper case and print it. doLast project.name In order to run the task use the following command Windows gradlew.bat projectupper Linux and MacOS ./gradlew projectupper This will generate the following output > Task :projectupper Project Name: simple-gradle-java-app Upper : SIMPLE-GRADLE-JAVA-APP case This task is a very simple task. We can create much more complex tasks in groovy as well and I will cover those in future articles. Code The entire code discussed here is available in this repo https://github.com/aditya-sridhar/simple-gradle-java-app References Gradle Offical Guide: https://gradle.org/ Gradle Tasks : https://docs.gradle.org/current/userguide/tutorial_using_tasks.html Building Java Projects with Gradle: https://docs.gradle.org/current/userguide/building_java_projects.html Congrats 😄 You know how to use Gradle now. This Article covered some of the basic Concepts of Gradle. Please go over the documentation of Gradle to know more about Gradle. Happy Coding 😄 Feel free to connect with me in or follow me in This Article was originally published in LinkedIn Twitter adityasridhar.com