我正在尝试使用子模块来测试项目并正确生成报告,但遇到了一些问题。
我有以下项目结构:
parent
|-test1
|-test2
parent
的 pom.xml 看起来像这样:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>parent</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>test1</module>
<module>test2</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.16</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<configuration>
<aggregate>true</aggregate>
<reportsDirectories>
<reportsDirectory>${basedir}/target/surefire-reports</reportsDirectory>
</reportsDirectories>
</configuration>
<reportSets>
<reportSet>
<inherited>false</inherited>
<reports>
<report>report-only</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
我使用
mvn clean install site
来构建项目,运行测试并生成一个站点(它使用maven站点插件,该插件又使用maven Surefire报告插件来生成测试报告)。
问题是父pom.xml在子模块的pom.xml执行之前执行所有阶段(清理,安装和站点),因此没有来自
test1
和test2
的测试报告聚合在parent
中.
那么有没有办法在一行中执行
mvn clean install site
,或者我绝对必须先执行mvn clean install
然后执行mvn site
?
感谢您的任何建议。
显示的配置使用相同的 POM 作为父级和聚合器。文档还将讨论继承与多模块。父 POM 将值传递给其子项,而聚合器 POM 管理一组子项目或模块。 Sonatype Maven Book 的第 3.6.2 节介绍了更多细节。
根据我的经验,当父 POM 和聚合器 POM 分离时,Maven 构建工作得更好。
原因与依赖关系、Maven 如何确定构建顺序以及 Maven 如何运行命令有关。如果您的示例结构是这样的,具有单独的父级和聚合器,其中 test1 和 test2 继承自父级:
project (aggregator POM)
|- parent
|- test1
|- test2
然后 Maven 构建顺序将如下所示(当然假设依赖关系定义正确):
像
mvn clean install site
这样的命令将从父项目开始,清理和构建,安装到本地工件存储库,然后生成项目站点。它按顺序为显示的每个项目重复此命令序列。由于聚合器是独立的,因此它可能具有单独的配置来执行测试、覆盖率和 javadoc 的报告聚合。聚合器最后运行,因此模块的覆盖率数据库和中间 javadoc 文件已经存在,可以适当组合。
当父级和聚合器与您拥有的 POM 相同时,构建顺序为:
当您现在运行
mvn clean install site
时,Maven 必须构建父 POM(此处为 ParentAggregator),以便它具有依赖于它的子模块所需的信息。 ParentAggregator 也是聚合器,因此 Maven 会愉快地运行任何聚合报告作为 site
的一部分。当然,子模块尚未构建,因此没有可聚合的覆盖数据库或中间 javadoc 文件。或者,可能更糟糕的是,旧文件存在,因此聚合器对这些文件进行操作。父聚合器构建后,子模块构建,然后命令退出。构建结束时没有最终聚合步骤。总体结果可能不是你想要的。
这种现象可能会在多模块项目发布构建期间导致问题,这非常痛苦,以至于开发人员编写了一个 versions-maven-plugin mojo 来帮助纠正这些问题。问题:
如果您有一个多模块构建,其中聚合器 pom(即包装了 pom 和模块部分的聚合器 pom)也是其子模块引用的父模块,并且聚合器版本与父部分中指定的版本不匹配对于子模块,Maven 不会让您构建项目。
我需要在 Maven 多模块项目中配置有关单元测试结果的聚合可靠报告。 我尝试使用此解决方案,但在父报告中仅包含一个模块,对于其他结果,我必须从每个模块打开它们。 这是我的 pom 配置:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
<reporting>
<outputDirectory>${basedir}/target/site</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.2</version>
<reportSets>
<reportSet>
<reports>
</reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.1.2</version>
<reportSets>
<reportSet>
<!-- defines aggregate unit test report -->
<id>unit-tests-aggregate</id>
<inherited>false</inherited>
<reports>
<report>report</report>
</reports>
<configuration>
<aggregate>true</aggregate>
</configuration>
</reportSet>
<reportSet>
<!-- defines aggregate integration test report -->
<id>integration-tests-aggregate</id>
<inherited>false</inherited>
<reports>
<report>failsafe-report-only</report>
</reports>
<configuration>
<aggregate>true</aggregate>
</configuration>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</reporting>
您能帮助我了解这里可能存在什么问题吗?