maven本質上是一個插件框架,幾乎所有的功能都是通過各種各樣的插件來實現的。maven默認會依據項目類型自動把構建時的各階段(Lifecycle和phase)自動綁定(Lifecycle Mapping)到特定插件(plugin)提供的功能點(goals)上。例如java項目編譯階段(compile),實際上是調用了maven-compiler-plugin插件提供的compile功能點(goal)來實現的。
一、調用插件提供的功能(goal)
方式一:通過生命周期映射的方式,將插件的goal綁定到生命周期中的phase上,然后調用phase。例如:maven-jar-plugin插件提供了一個叫jar的goal,默認會綁定到生命周期的package階段(phase)。調用mvn package就會自動調用maven-jar-plugin:jar。生命周期中所有前置的phase會先自動執行。
package <==> maven-jar-plugin:jar
方式二:直接調用插件的某個功能(goal)。如mvn maven-jar-plugin:jar。maven有一個約定,如果插件的名字叫maven-xxxx-plugin或xxxx-maven-plugin的話。可以直接用mvn xxxx:goal的方式調用其提供的功能。所以前面這個命令就可以簡寫成:mvn jar:jar。這種方式只會執行指定的goal。
調用goal完整的命令格式為:
mvn <plugin-prefix>:<goal> mvn [<plugin-group-id>:]<plugin-artifact-id>[:<plugin-version>]:<goal>
二、常用插件和常用配置
maven-resources-plugin 文件資源配置
<plugin> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> <!--配置資源文件編碼--> </configuration> </plugin>
關於資源的配置,還可以參考:http://www.cnblogs.com/pixy/p/4798089.html。
maven-compiler-plugin 編譯配置
默認綁定到comile phase。當前版本的maven默認使用jdk1.5,使用更新的java版本必須手動配置。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>utf-8</encoding> <compilerArgument>-Xlint:none</compilerArgument> <compilerArguments> <extdirs>libs</extdirs> <!--使用項目中的jar包--> </compilerArguments> </configuration> </plugin>
maven-surefire-plugin 單元測試
默認綁定到test階段。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> <!--測試有失敗用例時,是否繼續構建--> <skipTests>true</skipTests> <!--是否跳過測試階段,方式1--> <skip>true</skip> <!--是否跳過測試階段,方式2--> </configuration> </plugin>
運行時指定:
mvn package -DskipTests mvn package -Dmaven.test.skip=true
mvn package -Dmaven.test.failure.ignore=true
更多的配置可以參考另一篇文件:http://www.cnblogs.com/pixy/p/4718176.html
maven-jar-plugin 打jar包
這個是普通java項目(非java web項目和其他特殊類型的java項目)package階段默認綁定的插件,能夠將編譯好的class和資源打成jar包。
- 常用配置1:打出可以運行的有主類的jar包
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <excludes> <!--打包時要排除的文件--> <exclude>agent.properties</exclude> </excludes> <archive> <manifest> <addClasspath>true</addClasspath> <!-- <classpathPrefix>lib/</classpathPrefix> --> <mainClass>com.demo.HelloWorld</mainClass> </manifest> </archive> </configuration> </plugin>
maven-assembly-plugin 打包含依賴的全包
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.defonds.RsaEncryptor</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin>
運行mvn assemlby:assembly在target下生成xxxx-with-dependencies.jar
assembly 插件的一個 bug:http://jira.codehaus.org/browse/MASSEMBLY-360,它在對第三方打包時,對於 META-INF 下的 spring.handlers,spring.schemas 等多個同名文件進行了覆蓋,遺漏掉了一些版本的 xsd 本地映射。
maven-shade-plugin 打包含依賴的全包且可以配置主類
Über在德語中是"above,over"的意思。Uber-Jar就是包含所有依賴的全包、完整包。
這個插件只有shade:shade一個唯一的goal,綁定到package pahse。
<plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <configuration> <transformers> <transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.xun.pf.sayHello.HelloWorld</mainClass> </transformer> </transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
</configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin>
這是由於一些包重復引用,打包后的 META-INF 目錄多出了一些 *.SF 等文件所致。 解決方法是在configuration節點下加:
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
ResourceTransformer
如修改配置文件,包含/排除特定文件等
http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#DontIncludeResourceTransformer
maven-shade-plugin插件有個配置屬性:createDependencyReducedPom,默認值為true.
注意這個屬性,如果你用這個插件來deploy,或者發布到中央倉庫
這個屬性會縮減你的pom文件,會把你依賴的<dependency>干掉
正確的做法是把這個值改成false
- <configuration>
- <createDependencyReducedPom>false</createDependencyReducedPom>
- </configuration>
maven-war-plugin 打war包
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <warName>${project.artifactId}</warName> <webResources> <resource> <!--將額外的jar依賴打入war包--> <directory>libs/</directory> <targetPath>WEB-INF/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </webResources>
<packagingExcludes>css/**,html/**</packagingExcludes>
</configuration> </plugin>
maven-source-plugin 打包源碼
生成的源碼包名為xxxx-sources.jar。
<plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.2</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin>
maven-exec-plugin 執行程序
直接調用java goal,執行java程序。
mvn exec:java -Dexec.mainClass="com.demo.HelloWorld"
在pom文件中配置
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>com.demo.config.DocMapper</mainClass> <arguments> <argument>${project.build.outputDirectory}\doc-path-map.txt</argument> <argument>${basedir}\src</argument> <argument>**/resource/*.java</argument> </arguments> </configuration> </plugin>
maven-dependency-plugin 依賴分析
mvn dependency:copy-dependencies -DoutputDirectory=lib #導出所有依賴庫
mvn dependency:list
mvn dependency:tree
mvn dependency:analyze
<plugin> <artifactId>maven-dependency-plugin</artifactId> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <excludeTransitive>false</excludeTransitive> <!--是否排除間接依賴的包--> <stripVersion>true</stripVersion> <!--復制的jar文件是否去掉版本信息--> </configuration> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> </execution> </executions> </plugin>
maven-install-plugin 產出安裝到maven本地庫
默認綁定到install階段。將生成的構建產出安裝到本地庫。
goals:
-
- install-file 將本地jar包安裝到本地倉庫
mvn install:install-file -Dfile=classes12_g.jar -DgroupId=com.oracle -DartifactId=oracle -Dversion=10.2.0.2.0 -Dpackaging=jar -DgeneratePom=true
maven-release-plugin 整合svn創建tag、更新版本
暫無
參考文檔:http://juvenshun.iteye.com/blog/376422
cobertura 測試覆蓋率計算
基於jcoverage,原理是對class文件插樁,然后執行測試並生成覆蓋率報告。參考資料。
Pom.xml文件需要加入配置:
<reporting>
<outputDirectory>target/site</outputDirectory> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> </plugin> </plugins> </reporting>
運行 mvn cobertura:cobertura 將會插樁class文件、測試、生成覆蓋率報告。
cobertura支持的goal:
- check Check the Last Instrumentation Results.
- clean Clean up rogue files that cobertura maven plugin is tracking.
- dump-datafile Cobertura Datafile Dump Mojo.
- instrument Instrument the compiled classes.
- cobertura Instruments, Tests, and Generates a Cobertura Report.
findbugs 靜態java代碼檢查
基於規則匹配靜態檢查java代碼中存在的問題。參考資料
pom.xml中配置:
<reporting> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plugin</artifactId> <version>2.3.1</version> </plugin> </plugins> </reporting>
運行 mvn findbugs:findbugs 將開始執行檢查,並生成bugs報告(默認在target\site\findbugs目錄)。 findbugs:findbugs綁定到compile pahse即在編譯時自動檢查。
findbugs插件支持的goal:
- check fail the build if there were any FindBugs violations in the source code. An XML report is put out by defualt in the target directory with the errors
- findbugs Generates a FindBugs Report when the site plugin is run. The HTML report is generated for site commands only.
- gui Launch the Findbugs GUI. It will use all the parameters in the POM fle.
- help Display help information on findbugs-maven-plugin. mvn findbugs:help -Ddetail=true -Dgoal=<goal-name>
maven-eclipse-plugin 生成Eclispe工程文件
生成.classpath和.project文件,並且配置Eclispe將Maven作為External工具。Goals,Properties。
運行:mvn eclipse:eclipse 生成.classpath和.project文件,
maven-idea-plugin 生成IntelliJ IDEA工程文件
IDEA工程文件擴展名為.ipr和.iws
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-idea-plugin</artifactId> <version>2.2</version> <configuration> <jdkName>1.6</jdkName> <jdkLevel>6.0</jdkLevel> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> <dependenciesAsLibraries>true</dependenciesAsLibraries> <useFullNames>false</useFullNames> <deploymentDescriptorFile>src/main/webapp/WEB-INF/web.xml</deploymentDescriptorFile> </configuration> </plugin>
maven-jetty-plugin jetty服務器
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.6</version> <configuration> <contextPath>/</contextPath> <scanIntervalSeconds>3</scanIntervalSeconds> <scanTargetPatterns> <scanTargetPattern> <directory>src/main/webapp/WEB-INF</directory> <excludes> <exclude>**/*.jsp</exclude> </excludes> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </scanTargetPattern> </scanTargetPatterns> <requestLog implementation="org.mortbay.jetty.NCSARequestLog"> <filename>target/yyyy_mm_dd.request.log</filename> <retainDays>90</retainDays> <append>true</append> <extended>false</extended> <logTimeZone>GMT</logTimeZone> </requestLog> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>80</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin>
maven-javadoc-plugin 生成java文檔
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.4</version> <executions> <execution> <goals> <goal>javadoc</goal> </goals> <phase>compile</phase> </execution> </executions> <configuration> <encoding>UTF-8</encoding> <verbose>false</verbose> <show>public</show> <subpackages>com.pwrd.mobileqa.assist.resource</subpackages> <doclet>com.sun.jersey.wadl.resourcedoc.ResourceDoclet</doclet> <docletPath>${path.separator}${project.build.outputDirectory}</docletPath> <docletArtifacts> <!--解析項目生成javadoc需要的依賴--> <docletArtifact> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.6.1</version> </docletArtifact> ...... </docletArtifacts> <!-- the following option is required as a work around for version 2.5 of the javadoc plugin which will be used by a maven version > 2.0.9--> <useStandardDocletOptions>false</useStandardDocletOptions> <additionalparam>-output ${project.build.outputDirectory}/resourcedoc.xml</additionalparam> </configuration> </plugin>
mybatis-generator-maven-plugin 生成mybatis映射
默認會在根目錄查找並使用名為mybatis-config.xml的配置文件。
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.0</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>