Maven

maven常用插件总结

2017-01-10  本文已影响188人  mr_franklin

maven-compiler-plugin

用于编译项目源代码。
compile目标会编译src/main/java目录下的源代码。
testCompile目标会编译src/test/java目录下的测试代码。

maven compile编译源代码。
maven testCompile编译源代码+测试代码。

配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
        <source>1.8</source> <!-- 源代码使用jdk1.8支持的特性 -->
        <target>1.8</target> <!-- 使用jvm1.8编译目标代码 -->
               <compilerArgs> <!-- 传递参数 -->
                    <arg>-parameters</arg>
                    <arg>-Xlint:unchecked</arg>
                    <arg>-Xlint:deprecation </arg>
              </compilerArgs>
    </configuration>
</plugin>

用户可以通过两种方式调用Maven插件目标。第一种方式是将插件目标与生命周期阶段(lifecycle phase)绑定,这样用户在命令行只是输入生命周期阶段而已,例如Maven默认将maven-compiler-plugin的compile目标与compile生命周期阶段绑定,因此命令mvn compile实际上是先定位到compile这一生命周期阶段,然后再根据绑定关系调用maven-compiler-plugin的compile目标。第二种方式是直接在命令行指定要执行的插件目标,例如mvn archetype:generate 就表示调用maven-archetype-plugin的generate目标,这种带冒号的调用方式与生命周期无关。
(转自:http://www.infoq.com/cn/news/2011/04/xxb-maven-7-plugin)

maven-surefire-plugin

用于跑测试用例

此插件可以不在pom.xml里面声明,maven运行命令时,会自动调用该插件。
一些有用的配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skipAfterFailureCount>1</skipAfterFailureCount>  <!-- 只要有一个用例测试失败,就立即停止。默认情况下会跑完所有测试用例 -->
        <rerunFailingTestsCount>2</rerunFailingTestsCount>  <!-- 失败重试次数 -->
        <parallel>methods</parallel>  <!-- 并发执行测试用例 -->
        <threadCount>10</threadCount>  <!-- 并发执行时的线程数量 -->
    </configuration>
</plugin>

一些用法:

// 跳过测试用例
mvn test -Dmaven.test.skip=true
// 或
mvn test -DskipTests=true

// 运行指定的测试用例
mvn test -Dtest=*2Test
mvn test -Dtest=App2Test,AppTest
mvn test -Dtest=???2Test
mvn test -Dtest=*Test#testAdd

参考:
http://www.cnblogs.com/qyf404/p/5013694.html

spring-boot-maven-plugin

spring-boot开发必备插件。它能将spring-boot项目代码及其依赖jar打包成一个完整的可执行的jar包(fat jar)作用和插件maven-shade-plugin差不多。
repackage的goal绑定在生命周期的package阶段。
使用方式:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

如果指定了spring-boot作为parent,可以不指定版本和repackage goal

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.8.RELEASE</version>
    <configuration>
        <mainClass>${start-class}</mainClass>
   </configuration>
</parent>

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

spring-boot查找main文件的流程是:

  1. 首先查看<mainClass>是否有值,如果有,直接拿标签内的类名作为入口。
<properties>
    <start-class>com.example.Application</start-class>
</properties>

  1. 如果没找到<start-class>标签,会遍历所有文件,找到注解了@SpringBootApplication并含有main方法的类,将其作为入口。
    实践证明,如果没有定义<start-class>,查找入口类的方法也是非常快的。
    在实际开发中,推荐手动定义<start-class>。这样在一个项目工程中可以有多个@SpringBootApplication注解的类,修改一下pom里的配置就能灵活切换入口了。

maven 生命周期中及对应插件

maven生命周期是指从初始化,编译,打包,发布的一系列过程。
maven生命周期有三类::clean,default,site。我们经常使用的是clean(负责清理项目)和default(负责构建项目)。
default生命周期包括以下phase(阶段):

validate
initialize
generate-sources
process-sources
generate-resources
process-resources
compile
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources
test-compile
process-test-classes
test
prepare-package
package
pre-integration-test
integration-test
post-integration-test
verify
install
deploy

maven的default生命周期各个phase绑定的插件:

phase 插件:goal
generate-resources plugin:descriptor
process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar and plugin:addPluginArtifactMetadata
install install:install
deploy deploy:deploy

可以通过命令查看插件的详细信息:

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compile-plugin –Ddetail

官方文档:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

上一篇 下一篇

猜你喜欢

热点阅读