この記事では、SpringブートWARファイルをTomcatサーブレットコンテナにデプロイする方法を説明します。
SpringブートWARデプロイメントでは、次の3つの手順を実行する必要があります。
-
SpringBootServletInitializerを拡張する
-
提供されたサーブレットコンテナにマークを付けました.
-
パッケージングを戦争に更新する
テスト済み
-
春のブート1.4.2.RELEASE
-
Tomcat 8.5.9
-
Maven 3
1. SpringBootServletInitializerを拡張する
既存の
@ SpringBootApplication`クラスが
SpringBootServletInitializer`を拡張するようにします。
1.1従来のSpringブートJARデプロイメント。 (このファイルを更新してWARデプロイメントをサポートする)
SpringBootWebApplication.java
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[]args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
1.2 SpringブートWARデプロイメント。
SpringBootWebApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
}
public static void main(String[]args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
/** @SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[]args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}** /....
デプロイメントのために追加の `SpringBootWebApplication`クラスを作成する場合は、Springブートにどのメインクラスを起動するかを伝えてください:
pom.xml
<properties>
<start-class>com.mkyong.NewSpringBootWebApplicationForWAR</start-class>
</properties>
これを読んでください:リンク://spring-boot/spring-boot-which-main-class-to-start/[SpringBoot - 起動するメインクラス] === 2.提供されたサーブレットコンテナにマークを付けました. pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- marked the embedded servlet container as provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
=== 3.パッケージを戦争に更新する pom.xml
<packaging>war</packaging>
`mvn package`を実行し、` $ project/target/xxx.war`をTomcatにコピーして展開します。 === 4.完全な例 - SpringブートWAR + Tomcatデプロイメント 4.1 spring-boot/spring-boot-hello-world-example-thymeleaf/[Spring Boot Thymeleafの例]を更新し、Tomcatに手動で展開してください。 4.2既存の `SpringBootWebApplication`を更新し、` SpringBootServletInitializer`を拡張するようにします。 pom.xml
package com.mkyong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
}
public static void main(String[]args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
4.3パッケージを `war`に更新し、提供された` spring-boot-starter-tomcat`にマークを付けます。 pom.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0″
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd”>
;
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-web-thymeleaf</artifactId> <packaging>war</packaging> <name>Spring Boot Web Thymeleaf Example</name> <description>Spring Boot Web Thymeleaf Example</description> <url>/</url> <version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- marked the embedded servlet container as provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- hot swapping, disable cache for template, enable live reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- Optional, for bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.4これはオプションで、後でデモンストレーションのために `contextPath`を`/mkyong`に更新します。完了しました。 WARデプロイメントの準備ができました。 application.properties
welcome.message: Hello Mkyong
server.contextPath=/mkyong
4.5 Tomcatを入手し、WARファイルを展開します。 Mac OS X:ターミナル