Spring Boot打包方式
程序员文章站
2022-05-24 15:29:49
...
1 部署到外部Tomcat中
<?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>
<groupId>com.user</groupId>
<artifactId>zjsz-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 移除之后会报错,加入下面的依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>user</warName> <!--外部访问到项目名 http://localhost:8080/user/.....-->
</configuration>
</plugin>
</plugins>
<!--将xml打包进去-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
然后在修改一下spring boot入口,继承重新SpringBootServletInitializer 里面打configure方法
@SpringBootApplication
public class ZjszUserApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ZjszUserApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(ZjszUserApplication.class, args);
}
}
都添加好后,打一个war出来,然后放到Tomcat中去。启动完成。。
2 已jar包方式启动
<packaging>jar</packaging>
.......................
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- maven打包跳过测试类-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
<!--将xml打包进去-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
最下面打resources最好都加一下,因为有的时候打出来都jar包并不包含resources这个文件夹,会导致启动找不到对应都配置信息。
打包完成后,java -jar xxxxx.jar 启动。效果如下
推荐阅读
-
java-使用war将spring-boot和angular 7应用程序部署到tomcat 8.5中
-
(转)Spring boot 切换配置文件到yaml
-
将Spring Boot JAR应用程序转换为WAR
-
将Spring Boot应用程序绑定到Cloud Foundry中的服务的方法
-
Spring Boot应用程序创建可部署的war文件到tomcat
-
将Spring Boot应用程序注册成为系统服务
-
将Spring Boot程序打包成docker镜像-超简版
-
spring Boot环境下dubbo+zookeeper的一个基础讲解与示例
-
Spring Boot使用redis做数据缓存
-
Spring Boot 实战 入门