{"id":556,"date":"2025-01-21T14:19:34","date_gmt":"2025-01-21T14:19:34","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=556"},"modified":"2025-01-21T14:19:34","modified_gmt":"2025-01-21T14:19:34","slug":"build-java-application-using-gradle","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=556","title":{"rendered":"How to Build Java Application Using Gradle?"},"content":{"rendered":"<p>In this blog, you will learn to build Java Application using Gradle.<\/p>\n<p>In this step-by-step blog, we will cover everything you need to know about building a Java Application using Gradle.<\/p>\n<h2 id=\"what-is-gradle\">What is Gradle?<\/h2>\n<p><a href=\"https:\/\/docs.gradle.org\/current\/userguide\/userguide.html?ref=devopscube.com\" rel=\"noreferrer noopener\">Gradle<\/a> is an open-source build tool that automates the process of compiling, testing, and packaging the code, and its configuration files are written in Groovy.<\/p>\n<p>It supports almost every language but is mostly used for projects using languages like Java, Kotlin, C\/C++, etc.<\/p>\n<p>Gradle is highly customizable and supports a wide range of IDEs. IDE stands for an <code>integrated development environment<\/code> which is software that combines the capability of compiling, testing and packaging applications.<\/p>\n<h2 id=\"why-should-we-use-gradle\">Why should we use Gradle?<\/h2>\n<p>Gradle is faster, highly scalable, and can be used to build large projects. For example, Netflix and Android are primarily built on Java, and they use Gradle as their building tool.<\/p>\n<p>Gradle was created to combine the advantages of Maven and Ant and also to reduce the issues while using them.<\/p>\n<p>For example, Maven and Ant build files are XML-based, which is a bit difficult to understand, but Gradle build files are based on Groovy or Kotlin, which is easily understandable.<\/p>\n<p>Maven rebuilds the whole project again for code changes, but Gradle only rebuilds the part where the code is changed. This is one of the main advantages of Gradle, which decreases build time.<\/p>\n<p>Gradle supports multi-project builds, which means it can split large projects into subprojects with its own build files. An example multi-project structure is given below:<\/p>\n<pre><code>ecommerce\/\n\u251c\u2500\u2500 settings.gradle\n\u251c\u2500\u2500 build.gradle\n\u251c\u2500\u2500 web\/\n\u2502   \u2514\u2500\u2500 build.gradle\n\u251c\u2500\u2500 service\/\n\u2502   \u2514\u2500\u2500 build.gradle\n\u2514\u2500\u2500 data\/\n    \u2514\u2500\u2500 build.gradle<\/code><\/pre>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<p>To build a Java application with Gradle, you need the following installed on your system.<\/p>\n<ol>\n<li><a href=\"https:\/\/devopscube.com\/install-maven-guide\/\">Java JDK<\/a><\/li>\n<li><a href=\"https:\/\/gradle.org\/install\/?ref=devopscube.com\">Gradle<\/a><\/li>\n<\/ol>\n<h2 id=\"how-to-build-java-application-using-gradle\">How to Build Java Application Using Gradle?<\/h2>\n<p>To build a simple Java application, clone the following repository to your workspace.<\/p>\n<pre><code>git clone https:\/\/github.com\/techiescamp\/java-app.git<\/code><\/pre>\n<p>The structure of the repository is given below.<\/p>\n<pre><code>.\n\u251c\u2500\u2500 README.md\n\u251c\u2500\u2500 .github\n\u251c\u2500\u2500 build.gradle\n\u251c\u2500\u2500 pom.xml\n\u251c\u2500\u2500 settings.gradle\n\u2514\u2500\u2500 src\n    \u251c\u2500\u2500 main\n    \u2502   \u2514\u2500\u2500 java\n    \u2502       \u2514\u2500\u2500 springboot\n    \u2502           \u2514\u2500\u2500 Application.java\n    \u2514\u2500\u2500 test\n        \u2514\u2500\u2500 java\n            \u2514\u2500\u2500 springboot\n                \u2514\u2500\u2500 ApplicationTest.java<\/code><\/pre>\n<p>The <code>build.gradle<\/code> file contains all the requirements for the application, like plugins, dependencies, and the repository where the dependencies are downloaded from.<\/p>\n<pre><code>plugins {\n    id 'org.springframework.boot' version '3.4.1'\n    id 'io.spring.dependency-management' version '1.1.7'\n    id 'java'\n}\n\ngroup = 'springboot'\nversion = '1.0.0-SNAPSHOT'\n\nrepositories {\n    mavenCentral()\n}\n\ndependencies {\n    implementation 'org.springframework.boot:spring-boot-starter-web'\n    testImplementation 'org.springframework.boot:spring-boot-starter-test'\n}\n\njava {\n    toolchain {\n        languageVersion = JavaLanguageVersion.of(21)\n    }\n}\n\ntest {\n    useJUnitPlatform()\n}<\/code><\/pre>\n<ol>\n<li>The <code>plugin<\/code> block configures the springframework, dependency-management, and Java plugins to the application for springboot and dependency management.<\/li>\n<li>In the <code>group<\/code> and <code>version<\/code>, we specify the project ID and version.<\/li>\n<li>The <code>repositories<\/code> block specifies the repository from which the dependencies will be downloaded, in this case it <code>mavenCentral<\/code> repository.<\/li>\n<li>In the <code>dependencies<\/code> block, we specify the dependencies needed, this downloads the <code>springboot starter web<\/code> dependency for web applications and the <code>springboot starter test<\/code> for testing.<\/li>\n<li>In the <code>Java toolchain<\/code> block, we specify the required Java version.<\/li>\n<li>In the final <code>test<\/code> block, we specify the <code>JUnitPlatform<\/code> for running the test case.<\/li>\n<\/ol>\n<p>The <code>settings.gradle<\/code> file is where the root project name is specified.<\/p>\n<pre><code>rootProject.name = 'spring-boot'<\/code><\/pre>\n<p>The <strong>src<\/strong> folder contains two subfolders, <strong>main<\/strong> and <strong>test<\/strong>. The <strong>main\/java\/springboot<\/strong> folder contains the application&#8217;s source code.<\/p>\n<pre><code>package springboot;\n\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@SpringBootApplication\n@RestController\npublic class Application {\n\n    public static void main(String[] args) {\n        SpringApplication.run(Application.class, args);\n    }\n\n    @GetMapping(\"\/\")\n    public String index() {\n        return \"Hi from Spring Boot\";\n    }\n}<\/code><\/pre>\n<p>It is a simple spring boot application that says &#8220;Hi from Spring Boot&#8221; while we run.<\/p>\n<p>The <strong>test\/java\/springboot<\/strong> folder contains the test case for source code.<\/p>\n<pre><code>package springboot;\n\nimport org.junit.jupiter.api.Test;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.test.web.servlet.MockMvc;\n\nimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;\n\n@SpringBootTest\n@AutoConfigureMockMvc\npublic class ApplicationTest {\n\n    @Autowired\n    private MockMvc mockMvc;\n\n    @Test\n    public void indexEndpointReturnsExpectedMessage() throws Exception {\n        mockMvc.perform(get(\"\/\"))\n                .andExpect(status().isOk())\n                .andExpect(content().string(\"Hi from Spring Boot\"));\n    }\n}<\/code><\/pre>\n<p>This test checks if the endpoint returns HTTP 200 status with the expected message &#8220;Hi from Spring Boot.&#8221;<\/p>\n<p>The <code>pom.xml<\/code> file is used when you <a href=\"https:\/\/devopscube.com\/build-java-application-using-maven\/\" rel=\"noreferrer noopener\">build the Java application using Maven<\/a>, not when building with Gradle.<\/p>\n<p><code>pom.xml<\/code> is a similar file like <code>build.gradle<\/code>, it contains all the requirements for the application, like plugins, dependencies, etc&#8230;<\/p>\n<p><code>pom.xml<\/code> is for building Java applications using Maven and <code>build.gradle<\/code> is for building Java applications using Gradle.<\/p>\n<p>Let&#8217;s start building the Java application using Gradle, go to the root directory using the following command.<\/p>\n<pre><code>cd java-app<\/code><\/pre>\n<p>Before building the application, you can check if the application is working properly by running the following command from the root directory, which is<strong> java-app<\/strong>.<\/p>\n<pre><code>gradle bootRun<\/code><\/pre>\n<p>This runs your application, and you can access it on the web using the URL <a href=\"http:\/\/localhost:8080\/?ref=devopscube.com\" rel=\"noreferrer noopener\">http:\/\/localhost:8080<\/a>.<\/p>\n<p>Now, run the following command from the root directory to build the application using Gradle.<\/p>\n<pre><code>gradle build<\/code><\/pre>\n<p>Once the build is completed, you can find the JAR file inside the <strong>build\/libs\/<\/strong> directory as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-20-14.png\" class=\"kg-image\" alt=\"jar file directory\" loading=\"lazy\" width=\"856\" height=\"628\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-20-14.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-20-14.png 856w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>To run the JAR file, run the following command.<\/p>\n<pre><code>java -jar build\/libs\/spring-boot-1.0.0-SNAPSHOT.jar<\/code><\/pre>\n<p>You can add the symbol &amp; at the end of the above command to run the application in the background.<\/p>\n<p>Once, it starts running you can access the application on the web using the URL <a href=\"http:\/\/localhost:8080\/?ref=devopscube.com\" rel=\"noreferrer noopener\">http:\/\/localhost:8080<\/a>, as shown below<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-21-6.png\" class=\"kg-image\" alt=\"accessing the application on web\" loading=\"lazy\" width=\"330\" height=\"147\"><\/figure>\n<p>You can use this JAR file to create a <a href=\"https:\/\/devopscube.com\/dockerize-java-application\/\" rel=\"noreferrer noopener\">dockerized<\/a> image of the Java application and deploy the Java application on <a href=\"https:\/\/devopscube.com\/kubernetes-tutorials-beginners\/\" rel=\"noreferrer noopener\">Kubernetes<\/a>.<\/p>\n<h2 id=\"gradle-commands\">Gradle Commands<\/h2>\n<p>Let&#8217;s look at other important Gradle commands that are not used in the example.<\/p>\n<h3 id=\"init-command\">Init Command<\/h3>\n<p>The init command will create a basic project structure with every configuration file based on the project type you select.<\/p>\n<p>Run the following command to create the project structure.<\/p>\n<pre><code>gradle init<\/code><\/pre>\n<p>This will ask a couple of questions to create the structure as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-23-9.png\" class=\"kg-image\" alt=\"questions asked for creating project structure by gradle init command\" loading=\"lazy\" width=\"2000\" height=\"1963\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-23-9.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-23-9.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1600\/2025\/03\/image-23-9.png 1600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w2400\/2025\/03\/image-23-9.png 2400w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Once you answer all the questions, your new project structure will be created based on your answers.<\/p>\n<pre><code>.\n\u251c\u2500\u2500 app\n\u2502   \u251c\u2500\u2500 build.gradle\n\u2502   \u2514\u2500\u2500 src\n\u2502       \u251c\u2500\u2500 main\n\u2502       \u2502   \u251c\u2500\u2500 java\n\u2502       \u2502   \u2502   \u2514\u2500\u2500 org\n\u2502       \u2502   \u2502       \u2514\u2500\u2500 example\n\u2502       \u2502   \u2502           \u2514\u2500\u2500 App.java\n\u2502       \u2502   \u2514\u2500\u2500 resources\n\u2502       \u2514\u2500\u2500 test\n\u2502           \u251c\u2500\u2500 java\n\u2502           \u2502   \u2514\u2500\u2500 org\n\u2502           \u2502       \u2514\u2500\u2500 example\n\u2502           \u2502           \u2514\u2500\u2500 AppTest.java\n\u2502           \u2514\u2500\u2500 resources\n\u251c\u2500\u2500 gradle\n\u2502   \u251c\u2500\u2500 libs.versions.toml\n\u2502   \u2514\u2500\u2500 wrapper\n\u2502       \u251c\u2500\u2500 gradle-wrapper.jar\n\u2502       \u2514\u2500\u2500 gradle-wrapper.properties\n\u251c\u2500\u2500 gradle.properties\n\u251c\u2500\u2500 gradlew\n\u251c\u2500\u2500 gradlew.bat\n\u2514\u2500\u2500 settings.gradle<\/code><\/pre>\n<h3 id=\"gradle-scan-command\">Gradle Scan Command<\/h3>\n<p>The Gradle scan command records the details of your build, such as execution time, plugins and dependencies used, etc&#8230; and shows it on the web.<\/p>\n<p>To use Gradle scan, you have to use the <code>--scan<\/code> flag with the build command as given below.<\/p>\n<pre><code>gradle build --scan<\/code><\/pre>\n<p>Once the build is completed, it will ask a question whether your want to see the details of your build in the web, if you say yes, it will generate a link to access the page.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-25-9.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"955\" height=\"431\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-25-9.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-25-9.png 955w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Then you have to give your Email ID on the page that appears when you press the above link, it will send the web page link to the specified ID.<\/p>\n<p>Then, you can use the link from the Email to view the below page.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-28-9.png\" class=\"kg-image\" alt=\"web report from gradle scan command\" loading=\"lazy\" width=\"993\" height=\"673\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-28-9.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-28-9.png 993w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h3 id=\"gradle-profile-command\">Gradle Profile Command<\/h3>\n<p>The profile command created a detailed report of the build in an HTML file, but not as detailed as the scan command.<\/p>\n<p>This command is helpful in generating build reports on CI\/CD workflows.<\/p>\n<p>To use the Gradle profile, you have to use the <code>--profile<\/code> flag with the build command as given below.<\/p>\n<pre><code>gradle build --profile<\/code><\/pre>\n<p>You can find the HTML file inside the <code>build\/reports\/profile<\/code> directory, as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-29-11.png\" class=\"kg-image\" alt=\"directory structure of html report file\" loading=\"lazy\" width=\"906\" height=\"612\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-29-11.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-29-11.png 906w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>If you run the HTML file, you can see the build details in the browser as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-30-8.png\" class=\"kg-image\" alt=\"report generated by gradle profile command\" loading=\"lazy\" width=\"991\" height=\"572\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-30-8.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-30-8.png 991w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"what-are-the-differences-between-gradle-and-maven\">What are the differences between Gradle and Maven?<\/h2>\n<p>The key differences between Gradle and Maven are listed below:<\/p>\n<p><!--kg-card-begin: html--><\/p>\n<table class=\"has-fixed-layout\">\n<tbody>\n<tr>\n<td>Gradle<\/td>\n<td>Maven<\/td>\n<\/tr>\n<tr>\n<td>Uses Groovy or Kotlin DSL based configuration file<\/td>\n<td>Uses XML based configuration file<\/td>\n<\/tr>\n<tr>\n<td>It does not natively support caching<\/td>\n<td>It does not natively supports caching<\/td>\n<\/tr>\n<tr>\n<td>Gradle build files are highly customizable and readable<\/td>\n<td>Mavens build files are less customizable and complex<\/td>\n<\/tr>\n<tr>\n<td>Building project fater than Maven and only rebuilds new changes<\/td>\n<td>Build speed is slow, and rebuild the whole project if any changes are made<\/td>\n<\/tr>\n<tr>\n<td>Supports multi-project builds natively<\/td>\n<td>Also supports multi-project builds but needs additional configurations, which are complex<\/td>\n<\/tr>\n<tr>\n<td>The directory structure can be customized<\/td>\n<td>Fixed directory sturcture. For example, src\/main\/java.<\/td>\n<\/tr>\n<tr>\n<td>Supports languages like Java, C\/C++, Groovy, etc\u2026<\/td>\n<td>Supports languages like Java, Scala, C#, Ruby, etc\u2026<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--kg-card-end: html--><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this guide, we have learned about the build tool Gradle, its configurations file, built a Java application using it, and additional important Gradle commands.<\/p>\n<p>Not only that, we have also checked the application build by running the JAR file and accessing it on the web.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/build-java-application-using-gradle\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Build Java Application Using Gradle? \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/build-java-application-using-gradle\/<\/p>\n","protected":false},"author":1,"featured_media":557,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-556","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=556"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/556\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/557"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}