1,帶Tomcat的打包方式
1.1, 在pom.xml文件添加以下配置(目的:自定main入口和跳過Junit代碼)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
< build > < plugins > <!--打包為jar時指定main入口--> < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > <!--這里寫上main方法所在類的路徑--> < configuration > < mainClass >com.sh.tool.YnToolApplication</ mainClass > </ configuration > < executions > < execution > < goals > < goal >repackage</ goal > </ goals > </ execution > </ executions > </ plugin > <!--忽略Junit代碼--> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-surefire-plugin</ artifactId > < version >2.4.2</ version > < configuration > < skipTests >true</ skipTests > </ configuration > </ plugin > </ plugins > </ build > |
1.2,在命令行窗口輸入命令 :
1
|
mvn clean package |
1.3,結果會在項目的target路徑下生成一個.jar文件,將.jar文件復制到任意路徑,打開命令窗口進入.jar所在路徑,執行以下命令
1
|
java -jar <.jar文件名> |
執行成功之后就可以訪問controller接口了。
2,去除Tomcat的打包方式
2.1,配置pom.xml文件,加上如下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< packaging >war</ packaging > <!--將packaging指定為war--> <!--移除內嵌的Tomcat--> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-tomcat</ artifactId > < scope >provided</ scope > </ dependency > < build > < plugins > <!--忽略Junit代碼--> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-surefire-plugin</ artifactId > < version >2.4.2</ version > < configuration > < skipTests >true</ skipTests > </ configuration > </ plugin > </ plugins > </ build > |
PS:網上有些說要加以下配置,但是我測試不加也是可以的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
< build > < plugins > <!-- maven打包的時候告訴maven不需要web.xml,否剛會報找不到web.xml錯誤 --> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-war-plugin</ artifactId > < version >2.6</ version > < configuration > < failOnMissingWebXml >false</ failOnMissingWebXml > </ configuration > </ plugin > <!--指定jdk版本--> < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-compiler-plugin</ artifactId > < version >3.1</ version > < configuration > < source >1.7</ source > < target >1.7</ target > </ configuration > </ plugin > </ plugins > </ build > |
2.2,修改啟動類為如下
1
2
3
4
5
6
7
8
9
10
11
12
|
@SpringBootApplication public class YnToolApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(YnToolApplication. class , args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(YnToolApplication. class ); } } |
2.3,執行以下命令打包,結果將在target目錄下生成.war文件
1
|
mvn clean package |
2.4,將war包放到Tomcat的webapps路徑下,啟動Tomcat,然后訪問就可以了。PS:war包部署的默認訪問方式和jar包的有點不同,war包訪問URL需要加上項目名,例如:http://localhost:8989/yn-tool-0.0.1-SNAPSHOT/index
到此這篇關于詳解springboot項目帶Tomcat和不帶Tomcat的兩種打包方式的文章就介紹到這了,更多相關springboot Tomcat打包內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/Tc11331144/article/details/85625115