maven是目前java項目中比較流行的構建工具,特別是它提供的插件,如果使用得當,整個項目研發流程都將會受益,從而提高研發、測試和部署的效率。這裏記錄幾個常用插件的用法,既方便自己日後回顧,也便於有興趣的同仁交流指正。各位實踐過程中如果發現有趣的插件或者更好的用法,也歡迎留言分享。
Maven工程標準架構
目錄 | 備註 |
${basedir} | 存放 pom.xml和所有的子目錄 |
${basedir}/src/main/resources | 項目的資源,如spring配置文件,properties資源文件等 |
${basedir}/src/main/webapps | web項目特有 |
${basedir}/src/test/java | 項目的測試類,比如說 JUnit代碼、TestNg代碼 |
${basedir}/src/test/resources | 測試代碼使用的資源目錄 |
插件一 maven-resources-plugin
Maven可以區別對待Java代碼文件和資源文件,默認的主資源文件目錄是src/main/resources,我們可以通過這個插件實現資源文件過濾。資源文件過濾的意思是指我們可以在資源文件裏用使用佔位符${propertyName},然後開啓對資源文件的過濾,pom.xml裏再統一設置所有{propertyName}對應的值,就可以在構建過程中將值替換掉資源文件中對應的${propertyName},實現了代碼配置分離、做到了參數的統一維護。
示例用法
<resources> <resource> <directory>src/main/resources</directory> <includes> <include>properties/*.properties</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>*.xml</include> <include>mapper/**/*.xml</include> <include>mysqlMapper/**/*.xml</include> <include>*.properties</include> </includes> </resource> </resources> …… <properties> <runtime.env>local</runtime.env> </properties>
我們的主應用集成後,會根據實際要求部署到不同的環境中,比如聯調環境、測試環境、壓力環境、預發佈環境、生產環境等,而這些環境上的資源配置信息顯然是不一樣的,針對每套環境,每個具體佔位符${propertyName}都會有不同的值,而這種場景可以使用Maven的profile來支持,每個profile都可以獨立維護一套參數值,在mvn package的時候靈活指定;此外,maven也支持在package的時候指定多個profile,這個特性在執行自動部署的時候特別有用。使用這個插件,我們的項目可以做到多環境支持,參考命令
mvn package -Pnocheck,env-test
示例用法
<profiles> <profile> <id>nocheck</id> <properties> <skipTests>true</skipTests> <checkstyle.skip>true</checkstyle.skip> <license.skip>true</license.skip> <notice.skip>true</notice.skip> <versions.skip>true</versions.skip> </properties> </profile> <profile> <!-- 本地環境,默認是windows --> <id>local</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <runtime.env>local</runtime.env> </properties> </profile> <profile> <id>env-test</id> <properties> <runtime.env>env-test</runtime.env> </properties> </profile> </profiles>
插件二 maven-jar-plugin
當我們將項目模塊化後,有一些通用的資源文件基本上大多數模塊都會用到,比如log4j.properties,jdbc.properties等,模塊中有了這些資源文件,我們才能單獨對該模塊進行開發、調試。默認情況下maven-jar-plugin會將這些資源文件全部package成一個jar包進行發佈,如果這樣的jar包集成到一個主應用中部署,運行,很可能導致主應用的配置不生效,我稱之爲配置混亂,爲了解決這個問題,可以開啓maven-jar-plugin的排除功能,在執行mvn package之前排除指定的資源文件。
示例用法
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <excludes> <exclude>applicationContext.xml</exclude> <exclude>properties/**</exclude> <exclude>log4j.properties</exclude> </excludes> </configuration> </plugin>
插件三 maven-war-plugin
項目如果是web主應用,我們可以使用maven-war-plugin來對webapps下各類文件進行過濾。用法參考maven-resources-plugin
示例用法
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <warName>demo-Rest</warName> <webResources> <resource> <directory>src/main/webapp/WEB-INF</directory> <filtering>true</filtering> <targetPath>WEB-INF</targetPath> <includes> <include>web.xml</include> </includes> </resource> </webResources> </configuration> </plugin>
插件四 properties-maven-plugin
隨着項目的不斷迭代,我們的資源配置項將會變得更多,這個會直接影響到pom.xml的體積膨脹;此外,如果項目目標部署環境比較多,pom.xml將會膨脹得更快,更加難以維護。爲了解決這個問題,我們需要將這些配置信息獨立出來,並按照不同環境進行歸類,使用properties-maven-plugin就會達到這個效果。
示例用法(將每個環境的信息放在不同的目錄下,然後在mvn package切換不同的profile實現去指定目錄讀取配置信息,用讀取到的value去替換資源配置文件的佔位符)
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <files> <file>profiles/${runtime.env}/jdbc.properties</file> <file>profiles/${runtime.env}/redis.properties</file> <file>profiles/${runtime.env}/batch.properties</file> <file>profiles/${runtime.env}/config.properties</file> </files> </configuration> <executions> <execution> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> </execution> </executions> </plugin>
插件五 maven-assembly-plugin
Java項目中有一種類型的主應用,是需要獨立部署在後臺啓動的,比如socket服務程序,比如定時調度程序,比如dubbo服務程序,這些程序理論上只需要執行一個簡單的java命令即可;稍微複雜一些的,我們可以規範一下自己的主應用結構,定義配置文件夾和依賴庫文件夾,再準備啓動的批處理腳本sh或bat文件即可。使用maven-assembly-plugin就可以達到這種效果。
示例用法
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>target/classes/package.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
附package.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>package</id> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>src/main/bin</directory> <includes> <include>*.sh</include> <include>*.bat</include> </includes> <filtered>true</filtered> <outputDirectory></outputDirectory> <fileMode>0755</fileMode> </fileSet> <fileSet> <directory>${basedir}/src/main/config</directory> <includes> <include>*.properties</include> <include>log4j.xml</include> </includes> <outputDirectory>config</outputDirectory> <filtered>true</filtered> <fileMode>0644</fileMode> </fileSet> <fileSet> <directory>${basedir}/src/main/config</directory> <includes> <include>log4j.dtd</include> </includes> <outputDirectory>config</outputDirectory> <fileMode>0644</fileMode> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <scope>runtime</scope> <fileMode>0644</fileMode> </dependencySet> </dependencySets> </assembly>
附示例生成的Java應用結構圖
插件六 maven-shade-plugin
有時候,我們需要將所有配置文件和依賴庫文件全部放在一個jar包中,運維的同事只需要執行java -jar batch.jar即可完成啓動。雖然使用maven-assembly-plugin也可以做到這一點,但是在讀取配置文件的時候有可能會遇到一些問題,這個時候,我們可能需要使用到maven-shade-plugin這個插件,經筆者實踐按照如下示例用法配置確實可用;當然本示例配置了mainClass,直接執行java -jar batch.jar確實沒問題,但如果執行java com.fastjrun.demospring4.BatchInit -classpath batch.jar也是可以的。
示例用法
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <finalName>batch</finalName> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>jar-with-dependencies</shadedClassifierName> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.fastjrun.demospring4.BatchInit</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.tooling</resource> </transformer> </transformers> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin>
插件七 versions-maven-plugin
當項目模塊化後,我們會遇到一個問題,就是項目版本升級的時候,需要同時變更父模塊和所有子模塊中的版本號 ,而這是一個比較瑣碎且容易出錯的事情,還好maven考慮得很周到,提供了這樣一個插件,我們使用命令行就可以達到效果了。我們的項目視圖如下
參考命令如下
mvn versions:set -DnewVersion=1.2-SNAPSHOT
總結
本文彙總了筆者常用的幾個插件及其用法,經實踐,基於eclipse的kepler、luna版本都能很好支持maven-resources-plugin、maven-jar-plugin、maven-war-plugin和properties-maven-plugin使用,同時也支持profile的activeByDefault設置,研發同事在不需要任何調整的情況下就能直接開發、調試代碼,且在開發結束後,可以直接使用mvn命令打包,打出各個環境的部署程序。從開發、調試、測試、驗證到上線的整個過程,所有模塊的pom.xml直到下一個迭代變更版本前都不用修改,直接使用。