springboot打包成war,部署到tomcat
1.介绍
springboot默认打包成jar,使用springboot想构建web 项目,默认是使用内置的Tomcat。但是考虑到项目需要集群部署或者优化的时,就需要达成war包部署到外部的Tomcat服务中。
本实例是为了能够将项目发布docker化,使用将达成的war包发布到docker的tomcat中。
2、修改pom.xml将默认的jar方式改为war方式
<groupId>com.liu</groupId>
<artifactId>docker-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--默认为jar方式-->
<!--<packaging>jar</packaging>-->
<!--改为war方式-->
<packaging>war</packaging>
3、排除内置的tomcat容器(两种方式)
3.1 方式一 排除spring-boot-starter-web中的tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
3.2 方式二 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。
相当于compile,但是打包阶段做了exclude操作-->
<scope>provided</scope>
</dependency>
4 修改启动类
继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer,实现configure方法:
为什么继承该类,SpringBootServletInitializer源码注释:
Note that a WebApplicationInitializer is only needed if you are building a war file and deploying it.
If you prefer to run an embedded web server then you won’t need this at all.
注意,如果您正在构建WAR文件并部署它,则需要WebApplicationInitializer。
如果你喜欢运行一个嵌入式Web服务器,那么你根本不需要这个。
启动类代码:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4.1 方式一,启动类继承SpringBootServletInitializer实现configure:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
4.2 方式二,新增加一个类继承SpringBootServletInitializer实现configure:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//此处的Application.class为带有@SpringBootApplication注解的启动类
return builder.sources(Application.class);
}
}
注意事项:
使用外部Tomcat部署访问的时候,application.properties(或者application.yml)中配置的
server.port=
server.servlet.context-path=
将失效,请使用tomcat的端口,tomcat,webapps下项目名进行访问。
为了防止应用上下文所导致的项目访问资源加载不到的问题,
建议pom.xml文件中标签下添加标签:
<build>
<!-- 应与application.properties(或application.yml)中context-path保持一致 -->
<finalName>war包名称</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5 打包
使用mvn命令行打包,运行:
clean是清除之前的包,-Dmaven.test.skip=true是忽略测试代码
jar 方式打包,使用内置Tomcat:mvn clean install -Dmaven.test.skip=true
运行:java -jar 包名.jar
war方式打包,使用外置Tomcat:mvn clean package -Dmaven.test.skip=true
运行:${Tomcat_home}/bin/目录下执行startup.bat(windows)或者startup.sh(linux)
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\Administrator>cd E:\maven_workspaces\docker
C:\Users\Administrator>e:
E:\maven_workspaces\docker>mvn clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building docker 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-clean-plugin/3.1.0/maven-clean-plu
gin-3.1.0.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-clean-plugin/3.1.0/maven-clean-plug
in-3.1.0.pom (5.2 kB at 7.0 kB/s)
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ tomcat-test ---
[INFO] Deleting E:\maven_workspaces\docker\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ tomcat-test ---
[m
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ tomcat-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to E:\maven_workspaces\docker\target\classes
[INFO]
;1m ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ tomcat-test -
--
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ tomcat-test ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:3.2.2:war (default-war) @ tomcat-test ---
[INFO] Packaging webapp
[INFO] Assembling webapp [tomcat-test] in [E:\maven_workspaces\docker\target\tomcat-test-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [224 msecs]
[INFO] Building war: E:\maven_workspaces\docker\target\tomcat-test-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.4.RELEASE:repackage (repackage) @ tomcat-test --
-
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.008 s
[INFO] Finished at: 2019-04-18T10:05:51+08:00
[INFO] Final Memory: 36M/412M
[INFO] ------------------------------------------------------------------------
E:\maven_workspaces\docker>
打包完成:
[外链图片转存失败(img-z2h2ONUG-1565938724628)(C:\Users\Administrator\Desktop\java\learning\images\docker\1555560071932.png)]