萬丈高樓平地起
spring-boot-maven-plugin這個(gè)插件是針對(duì)springboot項(xiàng)目運(yùn)行打包用的,公司項(xiàng)目有用到這些maven插件。于是自己來試下能不能使用這種方式部署。
添加spring-boot-maven-plugin 插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
然后看看idea的maven控制面板,多了一個(gè)spring-boot的插件選項(xiàng)

點(diǎn)開看看,包含了6個(gè)操作

使用help操作,有具體的用法提示如下
spring-boot:build-info
Generate a build-info.properties file based the content of the
current
MavenProject.
spring-boot:help
Display help information on spring-boot-maven-plugin.
Call mvn spring-boot:help -Ddetail=true -Dgoal=<goal-name> to display
parameter details.
spring-boot:repackage
Repackages existing JAR and WAR archives so that they can be executed from the
command line using java -jar. With layout=NONE can also be used simply to
package a JAR with nested dependencies (and no main class, so not executable).
spring-boot:run
Run an executable archive application.
spring-boot:start
Start a spring application. Contrary to the run goal, this does not block and
allows other goal to operate on the application. This goal is typically used
in integration test scenario where the application is started before a test
suite and stopped after.
spring-boot:stop
Stop a spring application that has been started by the 'start' goal. Typically
invoked once a test suite has completed.
run 和 start、stop 就是運(yùn)行和停止項(xiàng)目咯,但是一般不會(huì)使用這種方式來運(yùn)行和停止啊。
最多用上spring-boot:repackage命令來打包項(xiàng)目了,使用一下看看。結(jié)果報(bào)這個(gè)錯(cuò)。。
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.4.RELEASE:repackage (default-cli) on project demo1: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.2.4.RELEASE:repackage failed: Source file must be provided
這個(gè)插件對(duì)入口類有一定要求,不想做過多的配置(就是懶)此處不留爺,自有留爺處。換個(gè)插件走起。使用maven自帶的 maven-surefire-plugin豈不美哉?

- 雖然說不使用spring-boot-maven-plugin了,但是還是要將這個(gè)插件的依賴加上,不然打出來的jar包沒有包含maven依賴。
- maven-compiler-plugin這個(gè)插件 配置了java的開發(fā)環(huán)境和運(yùn)行環(huán)境。還有文件編碼
- maven-surefire-plugin 要注意需要跳過單元測(cè)試,如果工程里存在對(duì)數(shù)據(jù)庫進(jìn)行操作的單元測(cè)試必須要使用以下配置將之跳過!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--不加上這個(gè)springboot打出來的jar包將不包含依賴-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 跳過單元測(cè)試 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
運(yùn)行完畢,查看taget輸出目錄,已經(jīng)有jar包了

打開jar包,里面的依賴確認(rèn)是完整的

使用 java -jar demo1-0.0.1-SNAPSHOT.jar 執(zhí)行下看看

沒問題~