|
|
@@ -30,9 +30,10 @@ What you'll need
|
|
30
|
30
|
- About 15 minutes
|
|
31
|
31
|
- A favorite text editor or IDE
|
|
32
|
32
|
- [JDK 6][jdk] or later
|
|
33
|
|
- - [Maven 3.0][mvn] or later
|
|
|
33
|
+ - [Gradle 1.7+][gradle] or [Maven 3.0+][mvn]
|
|
34
|
34
|
|
|
35
|
35
|
[jdk]: http://www.oracle.com/technetwork/java/javase/downloads/index.html
|
|
|
36
|
+[gradle]: http://www.gradle.org/
|
|
36
|
37
|
[mvn]: http://maven.apache.org/download.cgi
|
|
37
|
38
|
|
|
38
|
39
|
|
|
|
@@ -59,7 +60,7 @@ To **skip the basics**, do the following:
|
|
59
|
60
|
Set up the project
|
|
60
|
61
|
------------------
|
|
61
|
62
|
|
|
62
|
|
-First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with [Maven](https://maven.apache.org) and [Gradle](http://gradle.org) is included here. If you're not familiar with either, refer to [Building Java Projects with Maven](/guides/gs/maven) or [Building Java Projects with Gradle](/guides/gs/gradle/).
|
|
|
63
|
+First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with [Gradle](http://gradle.org) and [Maven](https://maven.apache.org) is included here. If you're not familiar with either, refer to [Building Java Projects with Gradle](/guides/gs/gradle/) or [Building Java Projects with Maven](/guides/gs/maven).
|
|
63
|
64
|
|
|
64
|
65
|
### Create the directory structure
|
|
65
|
66
|
|
|
|
@@ -70,66 +71,44 @@ In a project directory of your choosing, create the following subdirectory struc
|
|
70
|
71
|
└── java
|
|
71
|
72
|
└── hello
|
|
72
|
73
|
|
|
73
|
|
-### Create a Maven POM
|
|
74
|
|
-
|
|
75
|
|
-`pom.xml`
|
|
76
|
|
-```xml
|
|
77
|
|
-<?xml version="1.0" encoding="UTF-8"?>
|
|
78
|
|
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
79
|
|
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
80
|
|
- <modelVersion>4.0.0</modelVersion>
|
|
81
|
|
-
|
|
82
|
|
- <groupId>org.springframework</groupId>
|
|
83
|
|
- <artifactId>gs-rest-service</artifactId>
|
|
84
|
|
- <version>0.1.0</version>
|
|
85
|
|
-
|
|
86
|
|
- <parent>
|
|
87
|
|
- <groupId>org.springframework.boot</groupId>
|
|
88
|
|
- <artifactId>spring-boot-starter-parent</artifactId>
|
|
89
|
|
- <version>0.5.0.BUILD-SNAPSHOT</version>
|
|
90
|
|
- </parent>
|
|
91
|
|
-
|
|
92
|
|
- <dependencies>
|
|
93
|
|
- <dependency>
|
|
94
|
|
- <groupId>org.springframework.boot</groupId>
|
|
95
|
|
- <artifactId>spring-boot-starter-web</artifactId>
|
|
96
|
|
- </dependency>
|
|
97
|
|
- <dependency>
|
|
98
|
|
- <groupId>com.fasterxml.jackson.core</groupId>
|
|
99
|
|
- <artifactId>jackson-databind</artifactId>
|
|
100
|
|
- </dependency>
|
|
101
|
|
- </dependencies>
|
|
102
|
|
-
|
|
103
|
|
- <build>
|
|
104
|
|
- <plugins>
|
|
105
|
|
- <plugin>
|
|
106
|
|
- <artifactId>maven-compiler-plugin</artifactId>
|
|
107
|
|
- <version>2.3.2</version>
|
|
108
|
|
- </plugin>
|
|
109
|
|
- </plugins>
|
|
110
|
|
- </build>
|
|
111
|
|
-
|
|
112
|
|
- <repositories>
|
|
113
|
|
- <repository>
|
|
114
|
|
- <id>spring-snapshots</id>
|
|
115
|
|
- <url>http://repo.springsource.org/libs-snapshot</url>
|
|
116
|
|
- <snapshots><enabled>true</enabled></snapshots>
|
|
117
|
|
- </repository>
|
|
118
|
|
- </repositories>
|
|
119
|
|
- <pluginRepositories>
|
|
120
|
|
- <pluginRepository>
|
|
121
|
|
- <id>spring-snapshots</id>
|
|
122
|
|
- <url>http://repo.springsource.org/libs-snapshot</url>
|
|
123
|
|
- <snapshots><enabled>true</enabled></snapshots>
|
|
124
|
|
- </pluginRepository>
|
|
125
|
|
- </pluginRepositories>
|
|
126
|
|
-</project>
|
|
|
74
|
+### Create a Gradle build file
|
|
|
75
|
+
|
|
|
76
|
+`build.gradle`
|
|
|
77
|
+```gradle
|
|
|
78
|
+buildscript {
|
|
|
79
|
+ repositories {
|
|
|
80
|
+ maven { url "http://repo.springsource.org/libs-snapshot" }
|
|
|
81
|
+ mavenLocal()
|
|
|
82
|
+ }
|
|
|
83
|
+}
|
|
|
84
|
+
|
|
|
85
|
+apply plugin: 'java'
|
|
|
86
|
+apply plugin: 'eclipse'
|
|
|
87
|
+apply plugin: 'idea'
|
|
|
88
|
+
|
|
|
89
|
+jar {
|
|
|
90
|
+ baseName = 'gs-rest-service'
|
|
|
91
|
+ version = '0.1.0'
|
|
|
92
|
+}
|
|
|
93
|
+
|
|
|
94
|
+repositories {
|
|
|
95
|
+ mavenCentral()
|
|
|
96
|
+ maven { url "http://repo.springsource.org/libs-snapshot" }
|
|
|
97
|
+}
|
|
|
98
|
+
|
|
|
99
|
+dependencies {
|
|
|
100
|
+ compile("org.springframework.boot:spring-boot-starter-web:0.5.0.BUILD-SNAPSHOT")
|
|
|
101
|
+ compile("com.fasterxml.jackson.core:jackson-databind")
|
|
|
102
|
+ testCompile("junit:junit:4.11")
|
|
|
103
|
+}
|
|
|
104
|
+
|
|
|
105
|
+task wrapper(type: Wrapper) {
|
|
|
106
|
+ gradleVersion = '1.7'
|
|
|
107
|
+}
|
|
127
|
108
|
```
|
|
128
|
109
|
|
|
129
|
110
|
This guide is using [Spring Boot's starter POMs](/guides/gs/spring-boot/).
|
|
130
|
111
|
|
|
131
|
|
-Note to experienced Maven users who are unaccustomed to using an external parent project: you can take it out later, it's just there to reduce the amount of code you have to write to get started.
|
|
132
|
|
-
|
|
133
|
112
|
|
|
134
|
113
|
<a name="initial"></a>
|
|
135
|
114
|
Create a resource representation class
|
|
|
@@ -263,44 +242,45 @@ The [`@EnableAutoConfiguration`][] annotation switches on reasonable default beh
|
|
263
|
242
|
|
|
264
|
243
|
Now that your `Application` class is ready, you simply instruct the build system to create a single, executable jar containing everything. This makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.
|
|
265
|
244
|
|
|
266
|
|
-Add the following configuration to your existing Maven POM:
|
|
267
|
|
-
|
|
268
|
|
-`pom.xml`
|
|
269
|
|
-```xml
|
|
270
|
|
- <properties>
|
|
271
|
|
- <start-class>hello.Application</start-class>
|
|
272
|
|
- </properties>
|
|
273
|
|
-
|
|
274
|
|
- <build>
|
|
275
|
|
- <plugins>
|
|
276
|
|
- <plugin>
|
|
277
|
|
- <groupId>org.springframework.boot</groupId>
|
|
278
|
|
- <artifactId>spring-boot-maven-plugin</artifactId>
|
|
279
|
|
- </plugin>
|
|
280
|
|
- </plugins>
|
|
281
|
|
- </build>
|
|
282
|
|
-```
|
|
|
245
|
+Add the following configuration to your existing Gradle build file:
|
|
283
|
246
|
|
|
284
|
|
-The `start-class` property tells Maven to create a `META-INF/MANIFEST.MF` file with a `Main-Class: hello.Application` entry. This entry enables you to run it with `mvn spring-boot:run` (or simply run the jar itself with `java -jar`).
|
|
|
247
|
+`build.gradle`
|
|
|
248
|
+```groovy
|
|
|
249
|
+buildscript {
|
|
|
250
|
+ ...
|
|
|
251
|
+ dependencies {
|
|
|
252
|
+ classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.BUILD-SNAPSHOT")
|
|
|
253
|
+ }
|
|
|
254
|
+}
|
|
285
|
255
|
|
|
286
|
|
-The [Spring Boot maven plugin][spring-boot-maven-plugin] collects all the jars on the classpath and builds a single "über-jar", which makes it more convenient to execute and transport your service.
|
|
|
256
|
+apply plugin: 'spring-boot'
|
|
|
257
|
+```
|
|
|
258
|
+
|
|
|
259
|
+The [Spring Boot gradle plugin][spring-boot-gradle-plugin] collects all the jars on the classpath and builds a single "über-jar", which makes it more convenient to execute and transport your service.
|
|
|
260
|
+It also searches for the `public static void main()` method to flag as a runnable class.
|
|
287
|
261
|
|
|
288
|
262
|
Now run the following command to produce a single executable JAR file containing all necessary dependency classes and resources:
|
|
289
|
263
|
|
|
290
|
264
|
```sh
|
|
291
|
|
-$ mvn package
|
|
|
265
|
+$ ./gradlew build
|
|
|
266
|
+```
|
|
|
267
|
+
|
|
|
268
|
+Now you can run the JAR by typing:
|
|
|
269
|
+
|
|
|
270
|
+```sh
|
|
|
271
|
+$ java -jar build/libs/gs-rest-service-0.1.0.jar
|
|
292
|
272
|
```
|
|
293
|
273
|
|
|
294
|
|
-[spring-boot-maven-plugin]: https://github.com/SpringSource/spring-boot/tree/master/spring-boot-tools/spring-boot-maven-plugin
|
|
|
274
|
+[spring-boot-gradle-plugin]: https://github.com/SpringSource/spring-boot/tree/master/spring-boot-tools/spring-boot-gradle-plugin
|
|
295
|
275
|
|
|
296
|
276
|
> **Note:** The procedure above will create a runnable JAR. You can also opt to [build a classic WAR file](/guides/gs/convert-jar-to-war/) instead.
|
|
297
|
277
|
|
|
298
|
278
|
Run the service
|
|
299
|
279
|
-------------------
|
|
300
|
|
-Run your service using the spring-boot plugin at the command line:
|
|
|
280
|
+Run your service at the command line:
|
|
301
|
281
|
|
|
302
|
282
|
```sh
|
|
303
|
|
-$ mvn spring-boot:run
|
|
|
283
|
+$ ./gradlew clean build && java -jar build/libs/gs-rest-service-0.1.0.jar
|
|
304
|
284
|
```
|
|
305
|
285
|
|
|
306
|
286
|
|