What you’ll need
About your 15 minutes
Gradle 2.0
A favorite text editor or IDE
JDK 6 or later
Step 1: Setup the Gradle to Machine
For this click here how setup
Step 2: Set up the project
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.
Create the directory structure
In a project directory of your choosing, create the following subdirectory structure; for example, with mkdir -p src/main/java/hello
└── src
└── main
└── java
└── hello
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.
src/main/java/hello/HelloWorld.java
package hello; public class HelloWorld { public static void main(String[] args) { Greeter greeter = new Greeter(); System.out.println(greeter.sayHello()); } }
src/main/java/hello/Greeter.java
package hello; public class Greeter { public String sayHello() { return "Hello world!"; } }
Step 3: Build Java code
Starting simple, create a very basic build.gradle file that has only one line in it:
apply plugin: 'java'
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.
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:
gradle build
After a few seconds, “BUILD SUCCESSFUL” indicates that the build has completed.
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:
- classes. The project’s compiled .class files.
- reports. Reports produced by the build (such as test reports).
- libs. Assembled project libraries (usually JAR and/or WAR files).
The classes folder has .class files that are generated from compiling the Java code. Specifically, you should find HelloWorld.class and Greeter.class.
At this point, the project doesn’t have any library dependencies, so there’s nothing in the dependency_cache folder.
Declare dependencies
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.
For example, suppose that in addition to saying “Hello World!”, 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.
First, change HelloWorld.java to look like this:
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()); } }
Here HelloWorld uses Joda Time’s LocalTime class to get and print the current time.
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.
build.gradle
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[]
Hi. after running "grade build", in the build folder there is only two "libs" and "tmp" folders. What's the problem?
HashMap is synchronized while it is being looked up. what "looked up" means??
It is worth noting that that @RequestBody is still needed on method parameters that you want unmarshaled from JSON.
It would be nice if you can also provide us interceptor example along with RestController.