将spring boot项目部署到外置tomcat
- 首先将内置tomcat删除,在pom文件中加入这段代码即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
再加入这段代码
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.css</include>
<include>**/*.js</include>
<include>**/*.html</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
- 改造启动类
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}
}
继承SpringBootServletInitializer重写configure方法
- 打包成war文件
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
在pom文件中加入如上图所示的war
然后在右边Maven面板(没有的在View 的 Tool-Windows的下拉列表中找到Maven)然后
然后
最后在项目列表中可以看到打包成功的war文件
最后到你项目文件中找到打包好的war文件,复制
- 部署到外置的tomcat服务器
将复制的war文件粘贴到你安装tomcat目录下的wepapps目录下
最后到bin目录下找到startup.bat启动tomcat
启动后用 127.0.1:8080/打包后的war名 就可以访问了
- 访问后可能出现的错误访问接口时报404错误
在tomcat安装目录下的conf下的server.xml中添加
这段,将path=“”,和将docBase=“打包后的war包名” 即可
<Context path ="" docBase="打包后的war包名" reloadable="true"/>
<Host name="网站域名" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<!-- war包名 -->
<Context path ="" docBase="war包名" reloadable="true"/>
</Host>
- 如果不想加war包名登录,同上