idea spring boot 使用maven-plugin外掛打包
首頁需要需改pom.xml檔案的packaging :
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
新增外部執行的tomcat依賴:
<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> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
編碼問題:在package週期中其中在test環節種會遇到專案傳送的sql是中文亂碼,問題根源是新增不必要的測試依賴,這個時候應該最好把所有的測試依賴包都清楚,應該在專案執行時是不需要測試包的。而其中spring boot 會把所有的編碼配置 成 UTF-8,無需再自己手動修改編碼,給自己造成不要的BUG。
如果想去除版本資訊,再build標籤新增finalName新增自己想要的名字的即可
<build> <finalName>輸出專案名稱</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
啟動類:需繼承SpringBootServletInitializer類,它實現了servlet3的載入機制,找到相對應的路線,從而能夠順利打包
@SpringBootApplication
public class AppSpringBoot extends SpringBootServletInitializer {
//打包
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(AppSpringBoot.class);
}
public static void main(String[] args) {
SpringApplication.run(AppSpringBoot.class,args);
}
}
看到如下資訊,則打包成功:
[INFO] Building war: F:\ideaworkspace\test\target\test.war
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ test ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.772 s
[INFO] Finished at: 2018-09-17T10:28:20+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0