常用maven插件總結(jié)
maven是目前java項(xiàng)目中比較流行的構(gòu)建工具,特別是它提供的插件,如果使用得當(dāng),整個(gè)項(xiàng)目研發(fā)流程都將會(huì)受益,從而提高研發(fā)、測(cè)試和部署的效率。這里記錄幾個(gè)常用插件的用法,既方便自己日后回顧,也便于有興趣的同仁交流指正。各位實(shí)踐過(guò)程中如果發(fā)現(xiàn)有趣的插件或者更好的用法,也歡迎留言分享。
Maven工程標(biāo)準(zhǔn)架構(gòu)
插件一 maven-resources-plugin
Maven可以區(qū)別對(duì)待Java代碼文件和資源文件,默認(rèn)的主資源文件目錄是src/main/resources,我們可以通過(guò)這個(gè)插件實(shí)現(xiàn)資源文件過(guò)濾。資源文件過(guò)濾的意思是指我們可以在資源文件里用使用占位符${propertyName},然后開啟對(duì)資源文件的過(guò)濾,pom.xml里再統(tǒng)一設(shè)置所有{propertyName}對(duì)應(yīng)的值,就可以在構(gòu)建過(guò)程中將值替換掉資源文件中對(duì)應(yīng)的${propertyName},實(shí)現(xiàn)了代碼配置分離、做到了參數(shù)的統(tǒng)一維護(hù)。
示例用法
- <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>
我們的主應(yīng)用集成后,會(huì)根據(jù)實(shí)際要求部署到不同的環(huán)境中,比如聯(lián)調(diào)環(huán)境、測(cè)試環(huán)境、壓力環(huán)境、預(yù)發(fā)布環(huán)境、生產(chǎn)環(huán)境等,而這些環(huán)境上的資源配置信息顯然是不一樣的,針對(duì)每套環(huán)境,每個(gè)具體占位符${propertyName}都會(huì)有不同的值,而這種場(chǎng)景可以使用Maven的profile來(lái)支持,每個(gè)profile都可以獨(dú)立維護(hù)一套參數(shù)值,在mvn package的時(shí)候靈活指定;此外,maven也支持在package的時(shí)候指定多個(gè)profile,這個(gè)特性在執(zhí)行自動(dòng)部署的時(shí)候特別有用。使用這個(gè)插件,我們的項(xiàng)目可以做到多環(huán)境支持,參考命令
- 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>
- <!-- 本地環(huán)境,默認(rèn)是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
當(dāng)我們將項(xiàng)目模塊化后,有一些通用的資源文件基本上大多數(shù)模塊都會(huì)用到,比如log4j.properties,jdbc.properties等,模塊中有了這些資源文件,我們才能單獨(dú)對(duì)該模塊進(jìn)行開發(fā)、調(diào)試。默認(rèn)情況下maven-jar-plugin會(huì)將這些資源文件全部package成一個(gè)jar包進(jìn)行發(fā)布,如果這樣的jar包集成到一個(gè)主應(yīng)用中部署,運(yùn)行,很可能導(dǎo)致主應(yīng)用的配置不生效,我稱之為配置混亂,為了解決這個(gè)問(wèn)題,可以開啟maven-jar-plugin的排除功能,在執(zhí)行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
項(xiàng)目如果是web主應(yīng)用,我們可以使用maven-war-plugin來(lái)對(duì)webapps下各類文件進(jìn)行過(guò)濾。用法參考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
隨著項(xiàng)目的不斷迭代,我們的資源配置項(xiàng)將會(huì)變得更多,這個(gè)會(huì)直接影響到pom.xml的體積膨脹;此外,如果項(xiàng)目目標(biāo)部署環(huán)境比較多,pom.xml將會(huì)膨脹得更快,更加難以維護(hù)。為了解決這個(gè)問(wèn)題,我們需要將這些配置信息獨(dú)立出來(lái),并按照不同環(huán)境進(jìn)行歸類,使用properties-maven-plugin就會(huì)達(dá)到這個(gè)效果。
示例用法(將每個(gè)環(huán)境的信息放在不同的目錄下,然后在mvn package切換不同的profile實(shí)現(xiàn)去指定目錄讀取配置信息,用讀取到的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項(xiàng)目中有一種類型的主應(yīng)用,是需要獨(dú)立部署在后臺(tái)啟動(dòng)的,比如socket服務(wù)程序,比如定時(shí)調(diào)度程序,比如dubbo服務(wù)程序,這些程序理論上只需要執(zhí)行一個(gè)簡(jiǎn)單的java命令即可;稍微復(fù)雜一些的,我們可以規(guī)范一下自己的主應(yīng)用結(jié)構(gòu),定義配置文件夾和依賴庫(kù)文件夾,再準(zhǔn)備啟動(dòng)的批處理腳本sh或bat文件即可。使用maven-assembly-plugin就可以達(dá)到這種效果。
示例用法
- <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應(yīng)用結(jié)構(gòu)圖
插件六 maven-shade-plugin
有時(shí)候,我們需要將所有配置文件和依賴庫(kù)文件全部放在一個(gè)jar包中,運(yùn)維的同事只需要執(zhí)行java -jar batch.jar即可完成啟動(dòng)。雖然使用maven-assembly-plugin也可以做到這一點(diǎn),但是在讀取配置文件的時(shí)候有可能會(huì)遇到一些問(wèn)題,這個(gè)時(shí)候,我們可能需要使用到maven-shade-plugin這個(gè)插件,經(jīng)筆者實(shí)踐按照如下示例用法配置確實(shí)可用;當(dāng)然本示例配置了mainClass,直接執(zhí)行java -jar batch.jar確實(shí)沒(méi)問(wèn)題,但如果執(zhí)行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
當(dāng)項(xiàng)目模塊化后,我們會(huì)遇到一個(gè)問(wèn)題,就是項(xiàng)目版本升級(jí)的時(shí)候,需要同時(shí)變更父模塊和所有子模塊中的版本號(hào) ,而這是一個(gè)比較瑣碎且容易出錯(cuò)的事情,還好maven考慮得很周到,提供了這樣一個(gè)插件,我們使用命令行就可以達(dá)到效果了。我們的項(xiàng)目視圖如下
參考命令如下
- mvn versions:set -DnewVersion=1.2-SNAPSHOT
總結(jié)
本文匯總了筆者常用的幾個(gè)插件及其用法,經(jīng)實(shí)踐,基于eclipse的kepler、luna版本都能很好支持maven-resources-plugin、maven-jar-plugin、maven-war-plugin和properties-maven-plugin使用,同時(shí)也支持profile的activeByDefault設(shè)置,研發(fā)同事在不需要任何調(diào)整的情況下就能直接開發(fā)、調(diào)試代碼,且在開發(fā)結(jié)束后,可以直接使用mvn命令打包,打出各個(gè)環(huán)境的部署程序。從開發(fā)、調(diào)試、測(cè)試、驗(yàn)證到上線的整個(gè)過(guò)程,所有模塊的pom.xml直到下一個(gè)迭代變更版本前都不用修改,直接使用。
【本文為51CTO專欄作者“崔瑩峰”的原創(chuàng)稿件,轉(zhuǎn)載請(qǐng)聯(lián)系原作者】