Skip to content

Commit c3020e9

Browse files
committed
Add a single how to for creating a deployable war file
This commit updates the documentation to describe the three steps involved in producing a deployable war file in a single place. Closes gh-2185
1 parent b2a059a commit c3020e9

File tree

1 file changed

+83
-11
lines changed

1 file changed

+83
-11
lines changed

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

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,17 +1889,89 @@ after which you can run the application with
18891889

18901890
[[howto-create-a-deployable-war-file]]
18911891
=== Create a deployable war file
1892-
Use the `SpringBootServletInitializer` base class, which is picked up by Spring's
1893-
Servlet 3.0 support on deployment. Add an extension of that to your project and build a
1894-
war file as normal. For more detail, see the
1895-
http://spring.io/guides/gs/convert-jar-to-war['`Converting a jar Project to a war`'] guide
1896-
on the spring.io website and the sample below.
1897-
1898-
The war file can also be executable if you use the Spring Boot build tools. In that case
1899-
the embedded container classes (to launch Tomcat for instance) have to be added to the
1900-
war in a `lib-provided` directory. The tools will take care of that as long as the
1901-
dependencies are marked as '`provided`' in Maven or Gradle. Here's a Maven example
1902-
{github-code}/spring-boot-samples/spring-boot-sample-traditional/pom.xml[in the Boot Samples].
1892+
1893+
The first step in producing a deployable war file is to provide a
1894+
`SpringBootServletInitializer` subclass and override its `configure` method. This makes
1895+
use of Spring Framework's Servlet 3.0 support and allows you to configure your
1896+
application when it's launched by the servlet container. Typically, you update your
1897+
application's main class to extend `SpringBootServletInitializer`:
1898+
1899+
[source,java,indent=0,subs="verbatim,quotes,attributes"]
1900+
----
1901+
@Configuration
1902+
@EnableAutoConfiguration
1903+
@ComponentScan
1904+
public class Application extends SpringBootServletInitializer {
1905+
1906+
@Override
1907+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
1908+
return application.sources(Application.class);
1909+
}
1910+
1911+
public static void main(String[] args) throws Exception {
1912+
SpringApplication.run(Application.class, args);
1913+
}
1914+
1915+
}
1916+
----
1917+
1918+
The next step is to update your build configuration so that your project produces a war file
1919+
rather than a jar file. If you're using Maven and using `spring-boot-starter-parent` (which
1920+
configures Maven's war plugin for you) all you need to do is modify `pom.xml` to change the
1921+
packaging to war:
1922+
1923+
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
1924+
----
1925+
<packaging>war</packaging>
1926+
----
1927+
1928+
If you're using Gradle, you need to modify `build.gradle` to apply the war plugin to the
1929+
project:
1930+
1931+
[source,groovy,indent=0,subs="verbatim,quotes,attributes"]
1932+
----
1933+
apply plugin: 'war'
1934+
----
1935+
1936+
The final step in the process is to ensure that the embedded servlet container doesn't
1937+
interfere with the servlet container to which the war file will be deployed. To do so, you
1938+
need to mark the embedded servlet container dependency as provided.
1939+
1940+
If you're using Maven:
1941+
1942+
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
1943+
----
1944+
<dependencies>
1945+
<!-- … -->
1946+
<dependency>
1947+
<groupId>org.springframework.boot</groupId>
1948+
<artifactId>spring-boot-starter-tomcat</artifactId>
1949+
<scope>provided</scope>
1950+
</dependency>
1951+
<!-- … -->
1952+
</dependencies>
1953+
----
1954+
1955+
And if you're using Gradle:
1956+
1957+
[source,groovy,indent=0,subs="verbatim,quotes,attributes"]
1958+
----
1959+
dependencies {
1960+
// …
1961+
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
1962+
// …
1963+
}
1964+
----
1965+
1966+
If you're using the <<build-tool-plugins.adoc#build-tool-plugins, Spring Boot build tools>>,
1967+
marking the embedded servlet container dependency as provided will produce an executable war
1968+
file with the provided dependencies packaged in a `lib-provided` directory. This means
1969+
that, in addition to being deployable to a servlet container, you can also run your
1970+
application using `java -jar` on the command line.
1971+
1972+
TIP: Take a look at Spring Boot's sample applications for a
1973+
{github-code}/spring-boot-samples/spring-boot-sample-traditional/pom.xml[Maven-based example]
1974+
of the above-described configuration.
19031975

19041976

19051977

0 commit comments

Comments
 (0)