<div dir="ltr" style="text-align: justify;" trbidi="on">This guide walks you through using Gradle to build a simple Java project. You’ll create a simple app and then build it using Gradle.</p>
<p><b>What you’ll need</b></p>
<p>About your 15 minutes<br />
Gradle 2.0<br />
A favorite text editor or IDE<br />
JDK 6 or later</p>
<p><b>Step 1: Setup the Gradle to Machine</b><br />
For this <a href="https://dineshonjava.com/2015/01/how-to-install-gradle.html" target="_blank">click here how setup</a></p>
<p><b>Step 2: Set up the project</b></p>
<p>First you set up a Java project for Gradle to build. To keep the focus on Gradle, make the project as simple as possible for now.</p>
<p><b>Create the directory structure</b></p>
<p>In a project directory of your choosing, create the following subdirectory structure; for example, with <b><i>mkdir -p src/main/java/hello </i></b></p>
<p>└── src<br />
 ;  ;  ;  ;  ;└── main<br />
 ;  ;  ;  ;  ;  ;  ;  ;  ;└── java<br />
 ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ; └── hello</p>
<p>Within the src/main/java/hello directory, you can create any Java classes you want. For simplicity’s sake and for consistency with the rest of this guide, Spring recommends that you create two classes: HelloWorld.java and Greeter.java.</p>
<p><b>src/main/java/hello/HelloWorld.java</b></p>
<pre class="highlight" name="code">package hello;

public class HelloWorld {
 public static void main(String[] args) {
 Greeter greeter = new Greeter();
 System.out.println(greeter.sayHello());
 }
}
</pre>
<div align='center' id="ads-id"></div>
<p><b>src/main/java/hello/Greeter.java</b></p>
<pre class="highlight" name="code">package hello;

public class Greeter {
 public String sayHello() {
 return "Hello world!";
 }
}
</pre>
<p>
<b>Step 3: Build Java code</b></p>
<p>Starting simple, create a very basic <i><b>build.gradle</b></i> file that has only one line in it:</p>
<pre class="highlight" name="code">apply plugin: 'java'
</pre>
<p>This single line in the build configuration brings a significant amount of power. Run gradle tasks again, and you see new tasks added to the list, including tasks for building the project, creating JavaDoc, and running tests.</p>
<p>You’ll use the gradle build task frequently. This task compiles, tests, and assembles the code into a JAR file. You can run it like this:</p>
<p><i>gradle build</i></p>
<p>After a few seconds, &#8220;BUILD SUCCESSFUL&#8221; indicates that the build has completed.</p>
<p>To see the results of the build effort, take a look in the build folder. Therein you’ll find several directories, including these three notable folders:</p>
<ul style="text-align: left;">
<li>classes. The project’s compiled .class files.</li>
<li>reports. Reports produced by the build (such as test reports).</li>
<li>libs. Assembled project libraries (usually JAR and/or WAR files).</li>
</ul>
<p>
The classes folder has .class files that are generated from compiling the Java code. Specifically, you should find <i><b>HelloWorld.class</b></i> and <i><b>Greeter.class</b></i>.</p>
<p>At this point, the project doesn’t have any library dependencies, so there’s nothing in the <b>dependency_cache</b> folder.</p>
<p>Declare dependencies</p>
<p>The simple Hello World sample is completely self-contained and does not depend on any additional libraries. Most applications, however, depend on external libraries to handle common and/or complex functionality.</p>
<p>For example, suppose that in addition to saying &#8220;Hello World!&#8221;, you want the application to print the current date and time. You could use the date and time facilities in the native Java libraries, but you can make things more interesting by using the Joda Time libraries.</p>
<p>First, change <b>HelloWorld.java </b>to look like this:</p>
<pre class="highlight" name="code">package mail.java.hello;

import org.joda.time.LocalTime;

public class HelloWorld {
 public static void main(String[] args) {
 LocalTime currentTime = new LocalTime();
 System.out.println("The current local time is: " + currentTime);

 Greeter greeter = new Greeter();
 System.out.println(greeter.sayHello());
 }
}

</pre>
<p>
Here HelloWorld uses Joda Time’s LocalTime class to get and print the current time.</p>
<p>If you ran gradle build to build the project now, the build would fail because you have not declared Joda Time as a compile dependency in the build.</p>
<p>build.gradle</p>
<pre class="highlight" name="code">apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = 'hello.HelloWorld'

// tag::repositories[]
repositories {
 mavenLocal()
 mavenCentral()
}
// end::repositories[]

// tag::jar[]
jar {
 baseName = 'gs-gradle'
 version = '0.1.0'
}
// end::jar[]

// tag::dependencies[]
dependencies {
 compile "joda-time:joda-time:2.2"
}
// end::dependencies[]

// tag::wrapper[]
task wrapper(type: Wrapper) {
 gradleVersion = '1.11'
}
// end::wrapper[]
</pre>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/how-to-install-gradle/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '169'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…