一歹叮、修改pom.xml文件
需要修改的地方有:
1跑杭、打包方式咆耿,jar或者war
2、忽略內(nèi)嵌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手报、忽略內(nèi)嵌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>
二啃匿、注意修改配置文件中數(shù)據(jù)庫的配置信息蛆楞,確保正確
三、啟動類繼承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命令
打包成功逻住,結(jié)果如下: