1.spring-boot-maven-plugin
我們直接使用 maven package (maven自帶的package打包功能),打包Jar包的時候,不會將該項目所依賴的Jar包一起打進去,在使用java -jar命令啟動項目時會報錯,項目無法正常啟動。這個時候,我們就可以考慮引用spring-boot-maven-plugin
插件來為項目打Jar包。
<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring.boot.version}</version><executions><execution><goals><!-- 將引入的 jar 打入其中 --><goal>repackage</goal></goals></execution></executions>
</plugin>
2.flatten-maven-plugin
在使用Maven多模塊結構工程時,版本管理是一件很繁瑣且容易出錯的事情。每次升級版本號都要手動調整或者通過mvn versions:set -DnewVerion=1.2.0-SNAPSHOT
命令去更改每一個子模塊的版本號,非常的不方便,Maven官方文檔說:自 Maven 3.5.0-beta-1 開始,可以使用 ${revision}, ${sha1} and/or ${changelist} 這樣的變量作為版本占位符。即在maven多模塊項目中,可配合插件flatten-maven-plugin
及${revision}
屬性來實現全局版本統一管理。
<!-- 統一 revision 版本 -->
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>flatten-maven-plugin</artifactId><version>1.5.0</version><configuration><!-- 避免IDE將 .flattened-pom.xml 自動識別為功能模塊 --><flattenMode>resolveCiFriendliesOnly</flattenMode><updatePomFile>true</updatePomFile></configuration><executions><execution><goals><goal>flatten</goal></goals><id>flatten</id><phase>process-resources</phase></execution><execution><goals><goal>clean</goal></goals><id>flatten.clean</id><phase>clean</phase></execution></executions>
</plugin>
不可混合使用${revision}和明確字符串版本號,若出現父子模塊版本號混合使用${revision}和明確字符串形式如1.2.0-SNAPSHOT,在mvn package會出現類似如下錯誤
3.maven-clean-plugin
maven-clean-plugin
插件對應的命令是 mvn clean,執行 mvn clean 命令會刪除構建輸出目錄 target。mvn clean 命令其實是調用 maven-clean-plugin 插件執行 clean 操作的。maven-clean-plugin 插件是默認安裝好的插件,并不需要我們在 pom.xml 文件中進行配。
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-clean-plugin</artifactId><version>3.2.0</version><configuration><!-- 忽略錯誤 --><failOnError>false</failOnError></configuration><!-- mvn package和mvn clean package等價配置 --><executions><execution><id>auto-clean</id><phase>initialize</phase><goals><goal>clean</goal></goals></execution></executions>
</plugin>
4.maven-compiler-plugin
maven-compiler-plugin
插件是一個 Maven 插件,用來編譯項目代碼;自從3.0開始默認的編譯器是 javax.tools.JavaCompiler,用來編譯 Java 源碼;如果你想強制插件使用 javac 編譯器,你必須配置插件的屬性 forceJavacCompilerUse;還要注意,當前默認源(source)設置為 1.6,默認目標(target)設置為 1.6。獨立運行 Maven 和 JDK,可以通過 source 和 target 選項更改他們的默認值;
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><!-- 源代碼使用的JDK版本 --><source>1.8</source> <!-- 需要生成的目標class文件的編譯版本 --><target>1.8</target> <!-- 字符集編碼 --><encoding>UTF-8</encoding><!-- 跳過測試 --><skipTests>true</skipTests><!-- maven-compiler-plugin 插件,解決 Lombok + MapStruct 組合 --><annotationProcessorPaths><path><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.7.18</version></path><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></path><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>1.5.5.Final</version></path></annotationProcessorPaths></configuration>
</plugin>
5.maven-resources-plugin
maven-resources-plugin
插件來將src/main/resources目錄中的資源文件單獨剝離出來,生成一個獨立的資源文件包,用于處理項目資源文件并拷貝到輸出目錄。
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>3.2.0</version><executions><execution><id>copy-resources</id><phase>package</phase><goals><goal>copy-resources</goal></goals><configuration><outputDirectory>${project.build.directory}/resources</outputDirectory><resources><resource><directory>src/main/resources</directory><includes><include>**/*</include></includes></resource></resources></configuration></execution></executions></plugin>
上述配置將在Maven的package階段執行copy-resources目標,并將src/main/resources目錄中的所有文件復制到${project.build.directory}/resources目錄下。執行mvn package命令后,將生成一個獨立的資源文件包,其中包含src/main/resources目錄中的所有文件。可以在${project.build.directory}/resources目錄中找到這個資源文件包。
6.maven-source-plugin
maven-source-plugin
提供項目自動將源碼打包并發布的功能,在需要發布源碼項目的pom.xml文件中添加如下代碼即可執行 mvn install,maven會自動將source install到repository 。執行 mvn deploy,maven會自動將source deploy到remote-repository 。執行 mvn source:jar,單獨打包源碼。
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>3.3.0</version><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions>
</plugin>
7.maven-war-plugin
maven-war-plugin
WAR 插件負責收集 Web 應用程序的所有依賴項、類和資源,并將它們打包到 WAR 包中,僅包含 scope
為 compile+runtime
的依賴項,默認綁定到 package
階段。
<plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.4.0</version></plugin>
</plugins>
<resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/**</include></includes><filtering>true</filtering></resource>
</resources>
8.maven-antrun-plugin
maven-antrun-plugin
是 Maven 的一個插件,它允許你在 Maven 構建生命周期的某個階段執行 Apache Ant 任務。Apache Ant 是一個基于 Java 的構建工具,常用于 Java 項目的構建和部署。
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>3.1.0</version>
</plugin>
9.maven-surefire-plugin
Maven本身并不是一個單元測試框架,它只是在構建執行到特定生命周期階段的時候,通過插件來執行JUnit或者TestNG的測試用例。這個插件就是maven-surefire-plugin
,也可以稱為測試運行器(Test Runner),它能兼容JUnit]3、JUnit 4以及TestNG。
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M5</version>
</plugin>
10.versions-maven-plugin
versions-maven-plugin
插件可以管理項目版本, 特別是當Maven工程項目中有大量子模塊時,可以批量修改pom版本號,插件會把父模塊更新到指定版本號,然后更新子模塊版本號與父模塊相同,可以避免手工大量修改和遺漏的問題。
<!-- generateBackupPoms為true(默認值),pom.xml.versionsBackup備份文件,否則沒有備份文件無法回退版本號 -->
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.7</version><configuration><generateBackupPoms>true</generateBackupPoms></configuration>
</plugin>
#查看版本號
mvn help:evaluate -Dexpression=project.version -q -DforceStdout
# 修改版本號
mvn -f "pom.xml" versions:set -DoldVersion=* -DnewVersion=1.2.0-SNAPSHOT -DprocessAllModules=true -DallowSnapshots=true -DgenerateBackupPoms=true
# 修改版本號
mvn versions:set -DnewVersion=1.2.0-SNAPSHOT
# 回退版本號
mvn versions:revert
11.pluginManagement
plugins 和 pluginManagement 的區別,和我們前面研究過的 dependencies 和 dependencyManagement 的區別是非常類似的。plugins 下的 plugin 是真實使用的,而 pluginManagement 下的 plugins 下的 plugin 則僅僅是一種聲明,子項目中可以對 pluginManagement 下的 plugin 進行信息的選擇、繼承、覆蓋等。
<build><pluginManagement><plugins><!-- maven-surefire-plugin 插件,用于運行單元測試。 --><!-- 注意,需要使用 3.0.X+,因為要支持 Junit 5 版本 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M5</version></plugin><!-- maven-compiler-plugin 插件,解決 Lombok + MapStruct 組合 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><annotationProcessorPaths><path><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.7.18</version></path><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></path><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>1.5.5.Final</version></path></annotationProcessorPaths></configuration></plugin></plugins></pluginManagement>
</build>