Maven 插件配置
1、内置的插件绑定
生命周期阶段 |
插件目标 |
执行任务 |
process-resources |
maven-resources-plugin:resources |
复制主资源文件至主输出目录 |
compile |
maven-compiler-plugin:compile |
编译主代码至主输出目录 |
process-test-resources |
maven-resources-plugin:testResources |
复制测试资源文件至测试输出目录 |
test-compile |
maven-compiler-plugin:testCompile |
编译测试代码至测试输出目录 |
test |
maven-surefire-plugin:test |
执行测试用例 |
package |
maven-jar-plugin:jar |
创建项目jar包 |
install |
maven-install-plugin:install |
将项目输出构件安装到本地仓库 |
deploy |
maven-deploy-plugin:deploy |
将项目输出构件部署到远程仓库 |
2、自定义绑定
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3、 配置插件
命令行: $ mvn help:describe -Dcmd=compiler:compile
插件全局:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
插件任务:
4、常用插件
maven反编译插件:
命令:mvn compile
该插件是默认插件,如果没有配置,Maven将以1.3级别来编译。
5、测试插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
作用:
可以跳过测试
当测试失败仍然执行
默认插件,也可以命令后面加上参数来替代配置:-Dmaven.test.skip=true
跳过测试:
mvn test -DskipTests
mvn test -Dmaven.test.skip=true (同时跳过编译测试)
单独运行某个测试:
mvn test -Dtest=SquareTest
mvn test -Dtest=List*Test
mvn test -Dtest=SquareTest,List*Test
测试代码打包(默认测试代码不会打包):
生成测试代码包: 依赖配置:
jar包生成插件:
命令 :
mvn jar:jar
6、Tomcat插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
……
<!—默认使用8080端口,Context Path为build标签中finalName指定的名称,若没指定,则为artifactId的值,更多配置见下面-->
</configuration>
</plugin>
命令:
mvn tomcat:run
Tomcat插件常用配置:
<configuration>
<!—可选,context路径-->
<path>/</path>
<!—可选,指定端口-->
<port>8080</port>
<!—可选,指定自已的server.xml文件-->
<serverXml></srverXml>
<!—可选,指定自已的context.xml文件-->
<contextFile></contextFile>
<!—可选,改变(自定义)tomcat配置文件目录-->
<configurationDir></configurationDir>
<!—可选,携带一些系统变量,例如jvm参数,是map类型-->
<systemProperties></systemProperties>
<!—可选,指定web.xml文件,默认在WEB-INF/下-->
<tomcatWebXml></tomcatWebXml>
<!—可选,指定URI编码类型-->
<uriEncoding>UTF-8</uriEncoding>//uri编码
</configuration>
PS:这些配置适合在Eclipse中通过Maven启动Tomcat来测试自己的Web项目,如果要完全控制Tomcat,并自动将项目发布到Tomcat中,则还需要添加<server>标签,并在setting.xml中添加Tomcat管理员账号,详情见官方说明:
http://mojo.codehaus.org/tomcat-maven-plugin/
7、Jetty插件
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<!-- 每隔一段时间扫描项目是否更新 -->
<scanIntervalSeconds>0</scanIntervalSeconds>
<connectors>
<connector implementation="org.eclipse.jetty.
server.nio.SelectChannelConnector">
<port>80</port><!--端口-->
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
</configuration>
</plugin>
插件命令:
mvn jetty:run