Maven本質上是一個插件框架,它的核心並不執行任何具體的構建任務,全部這些任務都交給插件來完成,例如編譯源代碼是由maven-compiler-plugin完成的。進一步說,每一個任務對應了一個插件目標(goal),每一個插件會有一個或者多個目標,例如maven-compiler-plugin的compile目標用來編譯位於src/main/Java/
目錄下的主源碼,testCompile目標用來編譯位於src/test/java/
目錄下的測試源碼。html
用戶能夠經過兩種方式調用Maven插件目標。第一種方式是將插件目標與生命週期階段(lifecycle phase)綁定,這樣用戶在命令行只是輸入生命週期階段而已,例如Maven默認將maven-compiler-plugin的compile目標與 compile生命週期階段綁定,所以命令mvn compile其實是先定位到compile這一輩子命週期階段,而後再根據綁定關係調用maven-compiler-plugin的compile目標。第二種方式是直接在命令行指定要執行的插件目標,例如mvn archetype:generate 就表示調用maven-archetype-plugin的generate目標,這種帶冒號的調用方式與生命週期無關。java
<properties> <project.build.name>tools</project.build.name> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
${basedir} 項目根目錄 ${project.build.directory} 構建目錄,缺省爲target ${project.build.outputDirectory} 構建過程輸出目錄,缺省爲target/classes ${project.build.finalName} 產出物名稱,缺省爲${project.artifactId}-${project.version} ${project.packaging} 打包類型,缺省爲jar ${project.xxx} 當前pom文件的任意節點的內容
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <configuration> <filesets> <fileset> <directory>F:/logs</directory> </fileset> <fileset> <directory>../message-test</directory> <includes> <include>spy.log</include> </includes> </fileset> <fileset> <directory>../message-test/target</directory> </fileset> </filesets> </configuration> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> <configuration> <skip>true</skip> </configuration> </plugin>
skip=true,表示跳過單元測試,至關於-Dmaven.test.skip=true。
可是在執行mvn test -Dtest時,也會被跳過。解決方法是用excludes跳過全部測試,而不是用skipTests。web
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> <configuration> <!-- <skipTests>true</skipTests> --> <excludes> <exclude>**/*.java</exclude> </excludes> </configuration> </plugin>
即跳過全部test。這樣再執行mvn test -Dtest就沒有問題了。redis
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <!-- here the phase you need --> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/test-classes</outputDirectory> <resources> <resource> <directory>${basedir}/src/main/webapp/WEB-INF/config</directory> <filtering>true</filtering> <includes> <include>**/*.txt</include> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> <testResources> <testResource> <directory>src/test/resources</directory> <includes> <include>**/*.txt</include> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </testResource> </testResources> </configuration> </execution> </executions> </plugin>
爲了使項目結構更爲清晰,Maven區別對待Java代碼文件和資源文件,maven-compiler-plugin用來編譯Java代碼,maven-resources-plugin則用來處理資源文件。默認的主資源文件目錄是src/main/resources
,不少用戶會須要添加額外的資源文件目錄,這個時候就能夠經過配置maven-resources-plugin來實現。此外,資源文件過濾也是Maven的一大特性,你能夠在資源文件中使用${propertyName}形式的Maven屬性,而後配置maven-resources-plugin開啓對資源文件的過濾,以後就能夠針對不一樣環境經過命令行或者Profile傳入屬性的值,以實現更爲靈活的構建。spring
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.6</source> <!-- 源代碼使用的開發版本 --> <target>1.6</target> <!-- 須要生成的目標class文件的編譯版本 --> <!-- 通常而言,target與source是保持一致的,可是,有時候爲了讓程序能在其餘版本的jdk中運行(對於低版本目標jdk,源代碼中須要沒有使用低版本jdk中不支持的語法),會存在target不一樣於source的狀況 --> <encoding>UTF-8</encoding> <!-- 這下面的是可選項 --> <meminitial>128m</meminitial> <maxmem>512m</maxmem> <fork>true</fork> <!-- fork is enable,用於明確表示編譯版本配置的可用 --> <compilerVersion>1.3</compilerVersion> </configuration> </plugin>
默認編譯插件apache
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-dependencies</id> <phase>compile</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- ${project.build.directory}爲Maven內置變量,缺省爲target --> <outputDirectory>${project.build.directory}/lib</outputDirectory> <!-- 表示是否不包含間接依賴的包 --> <excludeTransitive>false</excludeTransitive> <!-- 表示複製的jar文件去掉版本信息 --> <stripVersion>true</stripVersion> </configuration> </execution> </executions> </plugin>
maven-dependency-plugin最大的用途是幫助分析項目依賴,dependency:list可以列出項目最終解析到的依賴列表,dependency:tree能進一步的描繪項目依賴樹,dependency:analyze能夠告訴你項目依賴潛在的問題,若是你有直接使用到的卻未聲明的依賴,該目標就會發出警告。maven-dependency-plugin還有不少目標幫助你操做依賴文件,例如dependency:copy-dependencies能將項目依賴從本地Maven倉庫複製到某個特定的文件夾下面。app
打成jar包插件: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <!-- 告知 maven-jar-plugin添加一個 Class-Path元素到 MANIFEST.MF文件,以及在Class-Path元素中包括全部依賴項 --> <addClasspath>true</addClasspath> <!-- 全部的依賴項應該位於 lib文件夾 --> <classpathPrefix>lib/</classpathPrefix> <!-- 當用戶使用 lib命令執行JAR文件時,使用該元素定義將要執行的類名 --> <mainClass>com.*.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> 打成war包插件: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <webResources> <resource> <directory>src/main/webapp</directory> <excludes> <exclude>**/*.jpg</exclude> </excludes> </resource> </webResources> </configuration> </plugin>
默認打包插件框架
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>com.yunzero.App</mainClass> </configuration> </plugin>
若是咱們運行的時候須要提供一些輸入的參數,能夠經過configuration的元素裏添加。這樣後續要執行這個程序時,咱們只須要在命令行執行以下命令:mvn exec:java ,而後程序就能夠運行起來了。運維
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <id>make-zip</id> <!-- 綁定到package生命週期階段上 --> <phase>package</phase> <goals> <!-- 綁定到package生命週期階段上 --> <goal>single</goal> </goals> <configuration> <descriptors> <!--描述文件路徑--> <descriptor>src/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin>
assembly.xml文件webapp
<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>distribution</id> <formats> <format>tar.gz</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}\src\main\resources</directory> <outputDirectory>\</outputDirectory> </fileSet> <fileSet> <directory>${project.basedir}\src\bin</directory> <outputDirectory>\bin</outputDirectory> </fileSet> </fileSets> <dependencySets> <dependencySet> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> <!-- 將scope爲runtime的依賴包打包到lib目錄下。 --> <scope>runtime</scope> </dependencySet> </dependencySets> </assembly>
生成的lib文件夾下放該項目的全部依賴以及該服務jar包,src/main/bin文件夾下咱們通常放start.sh和stop.sh兩個腳本文件用來開啓和關閉該服務,打包後直接放到根目錄下。生成的tar.gz文件的名字爲:maven-assembly-plugin插件中配置的finalName-assembly.xml配置的id(若assembly中沒有指定id,則只有前半部分).
<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>
隨着項目的不斷迭代,咱們的資源配置項將會變得更多,這個會直接影響到pom.xml的體積膨脹;此外,若是項目目標部署環境比較多,pom.xml將會膨脹得更快,更加難以維護。爲了解決這個問題,咱們須要將這些配置信息獨立出來,並按照不一樣環境進行歸類,使用properties-maven-plugin就會達到這個效果。
<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>
有時候,咱們須要將全部配置文件和依賴庫文件所有放在一個jar包中,運維的同事只須要執行java -jar batch.jar便可完成啓動。雖然使用maven-assembly-plugin也能夠作到這一點,可是在讀取配置文件的時候有可能會遇到一些問題,這個時候,咱們可能須要使用到maven-shade-plugin這個插件,經筆者實踐按照以下示例用法配置確實可用;固然本示例配置了mainClass,直接執行java -jar batch.jar確實沒問題,但若是執行java com.fastjrun.demospring4.BatchInit -classpath batch.jar也是能夠的。
更多插件請查看:
http://maven.apache.org/plugins/index.html?spm=a2c4e.11153940.blogcont26377.4.49b44321W1482B