Maven基于M2Eclipse生成站点和报告

前面介绍了基于 MyEclipse+M2Eclipse 完成工程的基本构建,接下来介绍怎样生成相关的文档和报告。

之前用的是 mvn 命令,后面指定插件的坐标(没有自定,mvn 自动找仓库中最新的)告知 mvn 做什么事。那么在 MyEclipse+M2Eclipse 环境下,是通过什么方式告诉 Maven 使用哪些插件做哪些事情呢?

靠的是 pom.xml 骨架文件。要做的就是在 pom.xml 文件中,按它的语法要求指定相关的插件坐标,Maven 在运行的时候,自动调用插件完成对应的任务。

这里要先了解一下常用的插件有哪些,以及怎样确定它们的坐标,这样才好在 pom.xml 中描述,如表 1 所示。

表 1 常用的插件
插件名称 用途 来源
maven-clean-plugin 清理项目 Apache
maven-compile-plugin 编译项目 Apache
maven-deploy-pligin 发布项目 Apache
maven-site-plugin 生成站点 Apache
maven-surefire-plugin 运行测试 Apache
maven-jar-plugin 构建 jar 项目 Apache
maven-javadoc-plugin 生成 javadoc 文件 Apache
maven-surefire-report-plugin 生成测试报告 Apache

接下来在工程里面体验运行测试、生成 javadoc、生成站点和测试报告插件的使用。

运行测试

在 Maven Repository(仓库)中找到 surefire 插件的坐标。

用浏览器打开 http://mvnrepository.com/,在 Search 输入框中输入“surefire-plugin”,单击 Search 按钮,如图 1 所示。

中央仓库查询surefire插件
图 1  中央仓库查询surefire插件

单击 maven-surefire-plugin,查看它的所有版本信息。单击想使用的版本(这里使用 2.19.1),会显示该版本的 groupId、artifactId 等坐标信息,如图 2 所示。

surefire插件的坐标页面
图 2  surefire 插件的坐标页面

在 pom.xml 中添加 surefire 插件描述。

在 MyEclipse 中打开 pom.xml 文件,在 pom.xml 后面添加一个 build 标签,里面添加 surefire 插件的描述信息,具体内容如下所示,被粗体显示的为新添加的内容。
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.mengma.demo</groupId>
  7. <artifactId>HelloWorld</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>HelloWorld</name>
  12. <url>http://maven.apache.org</url>
  13.  
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. </properties>
  17.  
  18. <dependencies>
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>4.7</version>
  23. <scope>test</scope>
  24. </dependency>
  25. </dependencies>
  26. <build>
  27. <plugins>
  28. <plugin>
  29. <groupId>org.apache.maven.plugins</groupId>
  30. <artifactId>maven-surefire-plugin</artifactId>
  31. <version>2.19.1</version>
  32. <configuration>
  33. <!-- 设置包含的测试类 -->
  34. <includes>
  35. <include>****</include>
  36. </includes>
  37. <!-- 设置不进行测试类 -->
  38. <excludes>
  39. <exclude>Test *</exclude>
  40. </excludes>
  41. <!-- 跳过测试阶段,测试类写的有问题也会出错,一般不推荐 -->
  42. <!-- <skip>true</skip> -->
  43. </configuration>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>
上面内容 26-46 行就是 surefire-plugin 的描述。具体的描述方式和说明上面有注释。

启动 Maven,运行 test。

右击“工程”,选择 Run As→Maven test 命令,Maven 会自动启动插件进行编译和测试。如果是第一次运行测试,在控制台会发现如下下载信息,说明 Maven 将用到之前配置的 surefire-plugin 运行测试。

[INFO]---maven-surefire-plugin:2.19.1:test (default-test) @MvnBookTP02 ---
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/
surefire/maven-surefire-common/2.19.1/maven-surefire-common-2.19.1.pom

生成 javadoc API 帮助文档

查找合适版本的坐标。

在 mvnrepository.com 中,类似查找 surefire-plugin 的方式,输入“javadoc-plugin”查询,找到自己需要的版本坐标信息。

将 javadoc-plugin 添加到 pom.xml。

在 pom.xml 的 plugins 标签之间添加如下内容。
  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-javadoc-plugin</artifactId>
  4. <version>2.7</version>
  5. <configuration>
  6. <aggregate>true</aggregate>
  7. </configuration>
  8. <executions>
  9. <execution>
  10. <id>attach-javadocs</id>
  11. <glals>
  12. <gola>jar</gola>
  13. </glals>
  14. <!-- 执行maven test的时候运行插件 -->
  15. <phase>test</phase>
  16. </execution>
  17. </executions>
  18. </plugin>

这里面除了 javadoc-plugin 的坐标信息外,还有其他配置信息。中间被粗体显示的信息的意思是:当选择 Run As→Maven test 命令时,执行 javadoc 插件,生成 doc 帮助文档。

运行 javadoc-plugin 插件,查看 doc ap 文档。

右击“工程”,选择 Run As→Maven test 命令,Maven 会自动调用插件生成 API 文档。在工程的 target 目录下会自动产生一个 apidocs 目录,里面就是生成的 API 文档。

前面介绍了 2 个插件的坐标查找和配置方法,接下来是生成站点和测试报告。

生成站点

  1. <!-- 构建项目站点报告插件 -->
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-site-plugin</artifactId>
  5. <version>3.0-beta-3</version>
  6. <configuration>
  7. <!-- 配置站点国际化 -->
  8. <locales>zh_CN</locales>
  9. <!-- 输出编码 -->
  10. <outputEncoding>GBK</outputEncoding>
  11. </configuration>
  12. </plugin>

测试报告

  1. <!-- 单元测试报告html -->
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-surefire-report-plugin</artifactId>
  5. <version>2.12.2</version>
  6. <configuration>
  7. <showSuccess>true</showSuccess>
  8. </configuration>
  9. <executions>
  10. <execution>
  11. <id> test-report</id>
  12. <phase>test</phase>
  13. </execution>
  14. </executions>
  15. </plugin>
  16. <!--测试覆盖率的报告 -->
  17. <plugin>
  18. <groupId>org.codehaus.mojo</groupId>
  19. <artifactId>cobertura-maven-plugin</artifactId>
  20. <version>2.5.1</version>
  21. <configuration>
  22. <formats>
  23. <format>html</format>
  24. <format>xml</format>
  25. </formats>
  26. </configuration>
  27. <executions>
  28. <execution>
  29. <id>cobertura-report</id>
  30. <goals>
  31. <goal>cobertura</goal>
  32. </goals>
  33. <phase>test</phase>
  34. </execution>
  35. </executions>
  36. </plugin>