Maven 实战 06 插件

来源:互联网 发布:日经225指数数据分析 编辑:程序博客网 时间:2024/06/01 08:30
Maven官方有两个插件列表,
第一个列表的GroupId为org.apache.maven.plugins,这里的插件最为成熟,
具体地址为:http://maven.apache.org/plugins/index.html。
第二个列表的GroupId为org.codehaus.mojo,这里的插件没有那么核心,但也有不少十分有用,
其地址为:http://mojo.codehaus.org/plugins.html。

maven-surefire-plugin 
执行测试。http://maven.apache.org/surefire/maven-surefire-plugin/
默认是执行测试目录下的 以Test结尾、以TestCase结尾、或者以Test开头的类

mvn test -Dtest=FooTest 这样一条命令的效果是仅运行FooTest测试类,参数可以更加灵活
mvn test -Dtest=Foo*Test,SSTest
mvn package -DskipTests   跳过测试

测试覆盖率报告,添加cobertura-maven-plugin插件
            <plugin>  
                <groupId>org.codehaus.mojo</groupId>  
                <artifactId>cobertura-maven-plugin</artifactId>  
                <version>2.5.1</version>  
            </plugin>
首先运行“mvn cobertura:help”, 如果不能运行,请添加以下仓库
    <pluginRepositories>  
        <pluginRepository>  
            <id>Codehaus repository</id>  
            <url>http://repository.codehaus.org/</url>  
        </pluginRepository>  
    </pluginRepositories> 

mvn cobertura:help          查看cobertura插件的帮助  
mvn cobertura:clean         清空cobertura插件运行结果  
mvn cobertura:check         运行cobertura的检查任务  
mvn cobertura:cobertura     运行cobertura的检查任务并生成报表,报表生成在target/site/cobertura目录下  
cobertura:dump-datafile     Cobertura Datafile Dump Mojo  
mvn cobertura:instrument    Instrument the compiled classes

代码检查 maven-checkstyle-plugin
<properties>  
        <checkstyle.config.location>config/maven_checks.xml</checkstyle.config.location>  
    </properties>  
    ...  
    <reporting>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-checkstyle-plugin</artifactId>  
                <version>2.9.1</version>  
            </plugin>  
  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-jxr-plugin</artifactId>  
                <version>2.3</version>  
            </plugin>  
        </plugins>  
    </reporting>  
其中可以修改使用的检查规则文件路径,插件默认提供了四个规则文件可以直接使用
* config/sun_checks.xml - Sun Microsystems Definition (default).  
* config/maven_checks.xml - Maven Development Definitions.  
* config/turbine_checks.xml - Turbine Development Definitions.  
* config/avalon_checks.xml - Avalon Development Definitions. 

也可以使用自定义的规则文件,比如自定义一个文件名为my_checks.xml,并放在工程根目录下,然后修改配置为如下:
    <properties>  
        <checkstyle.config.location>my_checks.xml</checkstyle.config.location>  
    </properties>  

另外,这里也添加了jxr插件,用来在生成的结果中可以通过link找到代码对应的行。

checkstyle插件的可执行任务如下:
mvn checkstyle:help           查看checkstyle-plugin的帮助:   
mvn checkstyle:check          检查工程是否满足checkstyle的检查,如果没有满足,检查会失败,可以通过target/site/checkstyle.html查看。  
mvn checkstyle:checkstyle     检查工程是否满足checkstyle的检查,如果没有满足,检查不会失败,可以通过target/site/checkstyle.html查看。  
mvn checkstyle:checkstyle-aggregate     检查工程是否满足checkstyle的检查,如果没有满足,检查不会失败,可以通过target/site/checkstyle.html查看。 

在运行完“mvn checkstyle:checkstyle”命令后,可以运行"mvn jxr:jxr"来使checkstyle的结果可以直接跳转到代码行位置。