Categories: Gradle

Building Java Projects with Gradle

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">This guide walks you through using Gradle to build a simple Java project&period; You’ll create a simple app and then build it using Gradle&period;<&sol;p>&NewLine;<p><b>What you’ll need<&sol;b><&sol;p>&NewLine;<p>About your 15 minutes<br &sol;>&NewLine;Gradle 2&period;0<br &sol;>&NewLine;A favorite text editor or IDE<br &sol;>&NewLine;JDK 6 or later<&sol;p>&NewLine;<p><b>Step 1&colon; Setup the Gradle to Machine<&sol;b><br &sol;>&NewLine;For this <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2015&sol;01&sol;how-to-install-gradle&period;html" target&equals;"&lowbar;blank">click here how setup<&sol;a><&sol;p>&NewLine;<p><b>Step 2&colon; Set up the project<&sol;b><&sol;p>&NewLine;<p>First you set up a Java project for Gradle to build&period; To keep the focus on Gradle&comma; make the project as simple as possible for now&period;<&sol;p>&NewLine;<p><b>Create the directory structure<&sol;b><&sol;p>&NewLine;<p>In a project directory of your choosing&comma; create the following subdirectory structure&semi; for example&comma; with <b><i>mkdir -p src&sol;main&sol;java&sol;hello <&sol;i><&sol;b><&sol;p>&NewLine;<p>&boxur;&HorizontalLine;&HorizontalLine; src<br &sol;>&NewLine;&nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi;&boxur;&HorizontalLine;&HorizontalLine; main<br &sol;>&NewLine;&nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi;&boxur;&HorizontalLine;&HorizontalLine; java<br &sol;>&NewLine;&nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &nbsp&semi; &boxur;&HorizontalLine;&HorizontalLine; hello<&sol;p>&NewLine;<p>Within the src&sol;main&sol;java&sol;hello directory&comma; you can create any Java classes you want&period; For simplicity’s sake and for consistency with the rest of this guide&comma; Spring recommends that you create two classes&colon; HelloWorld&period;java and Greeter&period;java&period;<&sol;p>&NewLine;<p><b>src&sol;main&sol;java&sol;hello&sol;HelloWorld&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package hello&semi;&NewLine;&NewLine;public class HelloWorld &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; Greeter greeter &equals; new Greeter&lpar;&rpar;&semi;&NewLine; System&period;out&period;println&lpar;greeter&period;sayHello&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p><b>src&sol;main&sol;java&sol;hello&sol;Greeter&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package hello&semi;&NewLine;&NewLine;public class Greeter &lbrace;&NewLine; public String sayHello&lpar;&rpar; &lbrace;&NewLine; return "Hello world&excl;"&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Step 3&colon; Build Java code<&sol;b><&sol;p>&NewLine;<p>Starting simple&comma; create a very basic <i><b>build&period;gradle<&sol;b><&sol;i> file that has only one line in it&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">apply plugin&colon; 'java'&NewLine;<&sol;pre>&NewLine;<p>This single line in the build configuration brings a significant amount of power&period; Run gradle tasks again&comma; and you see new tasks added to the list&comma; including tasks for building the project&comma; creating JavaDoc&comma; and running tests&period;<&sol;p>&NewLine;<p>You’ll use the gradle build task frequently&period; This task compiles&comma; tests&comma; and assembles the code into a JAR file&period; You can run it like this&colon;<&sol;p>&NewLine;<p><i>gradle build<&sol;i><&sol;p>&NewLine;<p>After a few seconds&comma; &&num;8220&semi;BUILD SUCCESSFUL&&num;8221&semi; indicates that the build has completed&period;<&sol;p>&NewLine;<p>To see the results of the build effort&comma; take a look in the build folder&period; Therein you’ll find several directories&comma; including these three notable folders&colon;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>classes&period; The project’s compiled &period;class files&period;<&sol;li>&NewLine;<li>reports&period; Reports produced by the build &lpar;such as test reports&rpar;&period;<&sol;li>&NewLine;<li>libs&period; Assembled project libraries &lpar;usually JAR and&sol;or WAR files&rpar;&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&NewLine;The classes folder has &period;class files that are generated from compiling the Java code&period; Specifically&comma; you should find <i><b>HelloWorld&period;class<&sol;b><&sol;i> and <i><b>Greeter&period;class<&sol;b><&sol;i>&period;<&sol;p>&NewLine;<p>At this point&comma; the project doesn’t have any library dependencies&comma; so there’s nothing in the <b>dependency&lowbar;cache<&sol;b> folder&period;<&sol;p>&NewLine;<p>Declare dependencies<&sol;p>&NewLine;<p>The simple Hello World sample is completely self-contained and does not depend on any additional libraries&period; Most applications&comma; however&comma; depend on external libraries to handle common and&sol;or complex functionality&period;<&sol;p>&NewLine;<p>For example&comma; suppose that in addition to saying &&num;8220&semi;Hello World&excl;&&num;8221&semi;&comma; you want the application to print the current date and time&period; You could use the date and time facilities in the native Java libraries&comma; but you can make things more interesting by using the Joda Time libraries&period;<&sol;p>&NewLine;<p>First&comma; change <b>HelloWorld&period;java <&sol;b>to look like this&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package mail&period;java&period;hello&semi;&NewLine;&NewLine;import org&period;joda&period;time&period;LocalTime&semi;&NewLine;&NewLine;public class HelloWorld &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; LocalTime currentTime &equals; new LocalTime&lpar;&rpar;&semi;&NewLine; System&period;out&period;println&lpar;"The current local time is&colon; " &plus; currentTime&rpar;&semi;&NewLine;&NewLine; Greeter greeter &equals; new Greeter&lpar;&rpar;&semi;&NewLine; System&period;out&period;println&lpar;greeter&period;sayHello&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;Here HelloWorld uses Joda Time’s LocalTime class to get and print the current time&period;<&sol;p>&NewLine;<p>If you ran gradle build to build the project now&comma; the build would fail because you have not declared Joda Time as a compile dependency in the build&period;<&sol;p>&NewLine;<p>build&period;gradle<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">apply plugin&colon; 'java'&NewLine;apply plugin&colon; 'eclipse'&NewLine;apply plugin&colon; 'application'&NewLine;&NewLine;mainClassName &equals; 'hello&period;HelloWorld'&NewLine;&NewLine;&sol;&sol; tag&colon;&colon;repositories&lbrack;&rsqb;&NewLine;repositories &lbrace;&NewLine; mavenLocal&lpar;&rpar;&NewLine; mavenCentral&lpar;&rpar;&NewLine;&rcub;&NewLine;&sol;&sol; end&colon;&colon;repositories&lbrack;&rsqb;&NewLine;&NewLine;&sol;&sol; tag&colon;&colon;jar&lbrack;&rsqb;&NewLine;jar &lbrace;&NewLine; baseName &equals; 'gs-gradle'&NewLine; version &equals; '0&period;1&period;0'&NewLine;&rcub;&NewLine;&sol;&sol; end&colon;&colon;jar&lbrack;&rsqb;&NewLine;&NewLine;&sol;&sol; tag&colon;&colon;dependencies&lbrack;&rsqb;&NewLine;dependencies &lbrace;&NewLine; compile "joda-time&colon;joda-time&colon;2&period;2"&NewLine;&rcub;&NewLine;&sol;&sol; end&colon;&colon;dependencies&lbrack;&rsqb;&NewLine;&NewLine;&sol;&sol; tag&colon;&colon;wrapper&lbrack;&rsqb;&NewLine;task wrapper&lpar;type&colon; Wrapper&rpar; &lbrace;&NewLine; gradleVersion &equals; '1&period;11'&NewLine;&rcub;&NewLine;&sol;&sol; end&colon;&colon;wrapper&lbrack;&rsqb;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-to-install-gradle&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '169'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago