Skip to content

Commit ba2ea30

Browse files
committed
Merge branch 'gh-1117'
2 parents ffc5d56 + ed57adb commit ba2ea30

File tree

25 files changed

+907
-39
lines changed

25 files changed

+907
-39
lines changed

spring-boot-docs/src/main/asciidoc/build-tool-plugins.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,18 @@ want the other Boot features but not this one)
383383
|`customConfiguration`
384384
|The name of the custom configuration which is used to populate the nested lib directory
385385
(without specifying this you get all compile and runtime dependencies).
386+
387+
|`executable`
388+
|Boolean flag to indicate if jar files are fully executable on Unix like operating
389+
systems. Defaults to `true`.
390+
391+
|`embeddedLaunchScript`
392+
|The embedded launch script to prepend to the front of the jar if it is fully executable.
393+
If not specified the 'Spring Boot' default script will be used.
394+
395+
|`embeddedLaunchScriptProperties`
396+
|Additional properties that to be expanded in the launch script. The default script
397+
supports a `mode` property which can contain the values `auto`, `service` or `run`.
386398
|===
387399

388400

spring-boot-docs/src/main/asciidoc/cloud-deployment.adoc renamed to spring-boot-docs/src/main/asciidoc/deployment.adoc

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
[[cloud-deployment]]
2-
= Deploying to the cloud
1+
[[deployment]]
2+
== Deploying Spring Boot applications
33

44
[partintro]
55
--
6+
Spring Boot's flexible packaging options provide a great deal of choice when it comes to
7+
deploying your application. You can easily deploy Spring Boot applications to a variety
8+
of cloud platforms, to a container images (such as Docker) or to virtual/real machines.
9+
10+
This section covers some of the more common deployment scenarios.
11+
--
12+
13+
14+
15+
[[cloud-deployment]]
16+
== Deploying to the cloud
17+
618
Spring Boot's executable jars are ready-made for most popular cloud PaaS
719
(platform-as-a-service) providers. These providers tend to require that you
820
"`bring your own container`"; they manage application processes (not Java applications
@@ -23,12 +35,11 @@ to run packaged within it.
2335
In this section we'll look at what it takes to get the
2436
<<getting-started.adoc#getting-started-first-application, simple application that we
2537
developed>> in the "`Getting Started`" section up and running in the Cloud.
26-
--
2738

2839

2940

3041
[[cloud-deployment-cloud-foundry]]
31-
== Cloud Foundry
42+
=== Cloud Foundry
3243
Cloud Foundry provides default buildpacks that come into play if no other buildpack is
3344
specified. The Cloud Foundry https://github.com/cloudfoundry/java-buildpack[Java buildpack]
3445
has excellent support for Spring applications, including Spring Boot. You can deploy
@@ -102,7 +113,7 @@ able to hit the application at the URI given, in this case
102113

103114

104115
[[cloud-deployment-cloud-foundry-services]]
105-
=== Binding to services
116+
==== Binding to services
106117
By default, metadata about the running application as well as service connection
107118
information is exposed to the application as environment variables (for example:
108119
`$VCAP_SERVICES`). This architecture decision is due to Cloud Foundry's polyglot
@@ -142,7 +153,7 @@ auto-configuration support and a `spring-boot-starter-cloud-connectors` starter
142153

143154

144155
[[cloud-deployment-heroku]]
145-
== Heroku
156+
=== Heroku
146157
Heroku is another popular PaaS platform. To customize Heroku builds, you provide a
147158
`Procfile`, which provides the incantation required to deploy an application. Heroku
148159
assigns a `port` for the Java application to use and then ensures that routing to the
@@ -225,7 +236,7 @@ Your application should now be up and running on Heroku.
225236

226237

227238
[[cloud-deployment-openshift]]
228-
== Openshift
239+
=== Openshift
229240
https://www.openshift.com/[Openshift] is the RedHat public (and enterprise) PaaS solution.
230241
Like Heroku, it works by running scripts triggered by git commits, so you can script
231242
the launching of a Spring Boot application in pretty much any way you like as long as the
@@ -288,14 +299,85 @@ run the app.
288299

289300

290301
[[cloud-deployment-gae]]
291-
== Google App Engine
302+
=== Google App Engine
292303
Google App Engine is tied to the Servlet 2.5 API, so you can't deploy a Spring Application
293304
there without some modifications. See the <<howto.adoc#howto-servlet-2-5, Servlet 2.5 section>>
294305
of this guide.
295306

296307

308+
[[deployment-service]]
309+
== Installing Spring Boot applications
310+
In additional to running Spring Boot applications using `java -jar` it is also possible
311+
to execute applications directly on Unix systems (Linux, OSX, FreeBSD etc). This makes it
312+
very easy to install and manage Spring Boot applications in common production
313+
environments. As long as you are generating '`fully executable`' jars from your build, and
314+
you are not using a custom `embeddedLaunchScript`, the following techniques can be used.
315+
316+
317+
318+
=== Unix/Linux services
319+
Spring Boot application can be easily started as Unix/Linux services using either `init.d`
320+
or `systemd`.
321+
322+
323+
324+
==== Installation as a init.d (system v) service
325+
The default executable script that is embedded into Spring Boot executable jars will act
326+
as an `init.d` script when it is symlinked to `/etc/init.d`. The standard `start`, `stop`,
327+
`restart` and `status` commands can be used. The script supports the following features:
328+
329+
* Starts the services as the user that owns the jar file
330+
* Tracks application PIDs using `/var/run/<appname>.pid`
331+
* Writes console logs to `/var/log/<appname>.log`
332+
333+
Assuming that you have a Spring Boot application installed in `/var/myapp`, to install a
334+
Spring Boot application as an `init.d` service simply create a symlink:
335+
336+
[indent=0,subs="verbatim,quotes,attributes"]
337+
----
338+
$ sudo link -s /var/myapp/myapp.jar /etc/init.d/myapp
339+
----
340+
341+
TIP: It is advisable to create a specific user account to run you application. Ensure
342+
that you have set the owner of the jar file using `chown` before installing your service.
297343

298-
[[cloud-deployment-whats-next]]
344+
Once installed, you can start and stop the service in the usual way. You can also flag the
345+
application to start automatically using your standard operating system tools. For example,
346+
if you use Debian:
347+
348+
[indent=0,subs="verbatim,quotes,attributes"]
349+
----
350+
$ update-rc.d myapp defaults <priority>
351+
----
352+
353+
354+
355+
==== Installation as a systemd service
356+
Systemd is the successor to `init.d` scripts, and now being used by many many modern Linux
357+
distributions. Although you can continue to use `init.d` script with `systemd`, it is also
358+
possible to launch Spring Boot applications using `systemd` '`service`' scripts.
359+
360+
For example, to run a Spring Boot application installed in `var/myapp` you can add the
361+
following script in `/etc/systemd/system/myapp.service`:
362+
363+
[indent=0]
364+
----
365+
[Unit]
366+
Description=myapp
367+
After=syslog.target
368+
369+
[Service]
370+
ExecStart=/var/myapp/myapp.jar
371+
372+
[Install]
373+
WantedBy=multi-user.target
374+
----
375+
376+
TIP: Remember to change the `Description` and `ExecStart` fields for your application.
377+
378+
379+
380+
[[deployment-whats-next]]
299381
== What to read next
300382
Check out the http://www.cloudfoundry.com/[Cloud Foundry], https://www.heroku.com/[Heroku]
301383
and https://www.openshift.com[Openshift] web sites for more information about the kinds of
@@ -307,6 +389,3 @@ The next section goes on to cover the _<<spring-boot-cli.adoc#cli, Spring Boot C
307389
or you can jump ahead to read about
308390
_<<build-tool-plugins.adoc#build-tool-plugins, build tool plugins>>_.
309391

310-
311-
312-

spring-boot-docs/src/main/asciidoc/documentation-overview.adoc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,9 @@ When you're ready to push your Spring Boot application to production, we've got
136136
== Advanced topics
137137
Lastly, we have a few topics for the more advanced user.
138138

139-
* *Deploy to the cloud:*
140-
<<cloud-deployment.adoc#cloud-deployment-cloud-foundry, Cloud Foundry>> |
141-
<<cloud-deployment.adoc#cloud-deployment-heroku, Heroku>> |
142-
<<cloud-deployment.adoc#cloud-deployment-cloudbees, CloudBees>>
139+
* *Deploy Spring Boot Applications:*
140+
<<deployment.adoc#cloud-deployment, Cloud Deployment>> |
141+
<<deployment.adoc#deployment-service, OS Service>>
143142
* *Build tool plugins:*
144143
<<build-tool-plugins.adoc#build-tool-plugins-maven-plugin, Maven>> |
145144
<<build-tool-plugins.adoc#build-tool-plugins-gradle-plugin, Gradle>>

spring-boot-docs/src/main/asciidoc/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ include::getting-started.adoc[]
4545
include::using-spring-boot.adoc[]
4646
include::spring-boot-features.adoc[]
4747
include::production-ready-features.adoc[]
48-
include::cloud-deployment.adoc[]
48+
include::deployment.adoc[]
4949
include::spring-boot-cli.adoc[]
5050
include::build-tool-plugins.adoc[]
5151
include::howto.adoc[]

spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ If you want to explore some of the concepts discussed in this chapter, you can t
10431043
look at the actuator {github-code}/spring-boot-samples[sample applications]. You also
10441044
might want to read about graphing tools such as http://graphite.wikidot.com/[Graphite].
10451045

1046-
Otherwise, you can continue on, to read about <<cloud-deployment.adoc#cloud-deployment,
1047-
'`cloud deployment options`'>> or jump ahead
1046+
Otherwise, you can continue on, to read about <<deployment.adoc#deployment,
1047+
'`deployment options`'>> or jump ahead
10481048
for some in-depth information about Spring Boot's
10491049
_<<build-tool-plugins.adoc#build-tool-plugins, build tool plugins>>_.

spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
[partintro]
66
--
77
This section goes into more detail about how you should use Spring Boot. It covers topics
8-
such as build systems, auto-configuration and run/deployment options. We also cover some
9-
Spring Boot best practices. Although there is nothing particularly special about
8+
such as build systems, auto-configuration and how to run your applications. We also cover
9+
some Spring Boot best practices. Although there is nothing particularly special about
1010
Spring Boot (it is just another library that you can consume), there are a few
1111
recommendations that, when followed, will make your development process just a
1212
little easier.

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/SpringBootPluginExtension.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.gradle
1818

19+
import java.io.File;
20+
import java.util.Map;
21+
1922
import org.springframework.boot.loader.tools.Layout
2023
import org.springframework.boot.loader.tools.Layouts
2124

@@ -130,4 +133,21 @@ public class SpringBootPluginExtension {
130133
*/
131134
boolean applyExcludeRules = true;
132135

136+
/**
137+
* If a fully executable jar (for *nix machines) should be generated by prepending a
138+
* launch script to the jar.
139+
*/
140+
boolean executable = true;
141+
142+
/**
143+
* The embedded launch script to prepend to the front of the jar if it is fully
144+
* executable. If not specified the 'Spring Boot' default script will be used.
145+
*/
146+
File embeddedLaunchScript;
147+
148+
/**
149+
* Properties that should be expanded in the embedded launch script.
150+
*/
151+
Map<String,String> embeddedLaunchScriptProperties;
152+
133153
}

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.gradle.api.tasks.TaskAction;
2929
import org.gradle.api.tasks.bundling.Jar;
3030
import org.springframework.boot.gradle.SpringBootPluginExtension;
31+
import org.springframework.boot.loader.tools.DefaultLaunchScript;
32+
import org.springframework.boot.loader.tools.LaunchScript;
3133
import org.springframework.boot.loader.tools.Repackager;
3234
import org.springframework.util.FileCopyUtils;
3335

@@ -80,6 +82,10 @@ public void setClassifier(String classifier) {
8082
this.classifier = classifier;
8183
}
8284

85+
void setOutputFile(File file) {
86+
this.outputFile = file;
87+
}
88+
8389
@TaskAction
8490
public void repackage() {
8591
Project project = getProject();
@@ -170,7 +176,8 @@ private void repackage(File file) {
170176
}
171177
repackager.setBackupSource(this.extension.isBackupSource());
172178
try {
173-
repackager.repackage(file, this.libraries);
179+
LaunchScript launchScript = getLaunchScript();
180+
repackager.repackage(file, this.libraries, launchScript);
174181
}
175182
catch (IOException ex) {
176183
throw new IllegalStateException(ex.getMessage(), ex);
@@ -201,6 +208,15 @@ else if (getProject().getTasks().getByName("run").hasProperty("main")) {
201208
getLogger().info("Setting mainClass: " + mainClass);
202209
repackager.setMainClass(mainClass);
203210
}
211+
212+
private LaunchScript getLaunchScript() throws IOException {
213+
if (this.extension.isExecutable()) {
214+
return new DefaultLaunchScript(this.extension.getEmbeddedLaunchScript(),
215+
this.extension.getEmbeddedLaunchScriptProperties());
216+
}
217+
return null;
218+
}
219+
204220
}
205221

206222
/**
@@ -228,10 +244,7 @@ protected String findMainMethod(java.util.jar.JarFile source) throws IOException
228244
}
229245
}
230246
}
231-
}
232247

233-
void setOutputFile(File file) {
234-
this.outputFile = file;
235248
}
236249

237250
}

0 commit comments

Comments
 (0)