一、修改pom.xml文件
需要修改的地方有:
1、打包方式,jar或者war
2、忽略内嵌tomcat
3、引入tomcat依赖
4、定义打包后的项目名
5、如果打包时报错(No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?)则需要添加插件指定jdk
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>
<!-- 1、修改打包方式 -->
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>SpringBootDemo</groupId>
<artifactId>test-book</artifactId>
<version>1.0</version>
<name>test-book</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 2、忽略内嵌tomcat,打包部署到tomcat。注*本地运行的时候要把这一段忽略引入个注释掉,
要不然项目启动不了 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 3、引入tomcat依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!-- 4、定义打包后的项目名 -->
<finalName>book</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 5、添加插件指定jdk -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<!-- executable 可指定也可不用 -->
<executable>C:\Program Files\Java\jdk1.8.0_191\bin\javac</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、注意修改配置文件中数据库的配置信息,确保正确
三、启动类继承SpringBootServletInitializer并重写configure方法,这是为了打包springboot项目用的。
@SpringBootApplication
public class TestBookApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(TestBookApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(TestBookApplication.class);
}
}
四、在windows的cmd窗口中打包项目
进入项目目录后输入 mvn clean package -Dmaven.test.skip=true命令
打包成功,结果如下:
打包成功.png
网友评论