第一种方式:手动配置
1.修改pom.xml文件
1.添加<packaging>war</packaging>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.joe</groupId>
<artifactId>learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
2.添加额外的tomcat(spring-boot-starter-tomcat)依赖,scope(作用域)设置为provided(该依赖编译和测试时有效,打包时不会被包含进去,在独立运行时由服务器提供)以防与独立运行时的tomcat的版本发生冲突。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
*注意:spring-boot-starter-web中依赖的spring-boot-starter-tomcat排不排除都可以,采用继承<parent>
方式开启的SpringBoot方式,后面显式导入的会自动覆盖隐式依赖的。
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
2.继承SpringBootServletInitializer抽象类并实现configure方法
两种方式:
1.直接在启动类继承SpringBootServletInitializer并实现configure方法
@SpringBootApplication
public class LearnApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(LearnApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(LearnApplication.class, args);
}
}
2.在启动类同包或子包下新建一个类继承SpringBootServletInitializer并实现configure方法
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(LearnApplication.class);
}
}
3.建议pom.xml文件中<build></build>
标签下添加<finalName></finalName>
标签
<finalName></finalName>
作用:定义了编译、打包、部署的项目名称。
源码里面这样描述:
The filename (excluding the extension, and with no path information) that the produced artifact will be called. The default value is ${artifactId}-${version}
;.
该部分引用了原文链接:https://blog.csdn.net/qq_33512843/article/details/80951741
使用外部Tomcat部署访问的时候,application.properties(或者application.yml)中配置的
server.port=
server.servlet.context-path=
将失效,请使用tomcat的端口,tomcat,webapps下项目名进行访问。
为了防止应用上下文所导致的项目访问资源加载不到的问题,
建议pom.xml文件中<build></build>
标签下添加<finalName></finalName>
标签
<build>
<finalName>learn</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
4.如果在resources之外有其他的静态资源包,需要指定打包时的静态资源路径
<build>
<finalName>learn</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory> <!-- 默认的资源路径,如果有其它显式配置,会覆盖默认的,所以需要再配置一下-->
<includes>
<include>**
5.使用mvn命令行打包,运行:
该部分引用了原文链接:https://blog.csdn.net/qq_33512843/article/details/80951741
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)
第二种方式:新建SpringBoot项目时,将打包方式选择为war
当选为war时,SpringBoot默认该项目为web项目,而且要打成war包在独立的服务器上运行,所以SpringBoot会自动帮你完成第一种方式的第1步和第2步。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)