多个模块项目的Maven报告和站点生成 [英] Maven reporting and site generation for multiple module project

查看:122
本文介绍了多个模块项目的Maven报告和站点生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用子模块来测试和生成报告,但是遇到了一些问题。

I am trying to get project with submodules to test and generate reports correctly, but have got some problems.

我有以下项目结构:

parent
  |-test1
  |-test2

parent 的pom.xml如下所示:

and the pom.xml for parent looks like this:

<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报告插件生成测试报告)。

And I use mvn clean install site to build the project, run the tests and generate a site (which uses maven site plugin, which in turn uses maven surefire report plugin, to generate test report).

问题是在执行子模块的pom.xml之前,所有阶段(清理,安装和站点)执行父pom.xml,因此没有来自 test1的测试报告 test2 汇总在

The trouble is the parent pom.xml is executed with all phases (clean, install and site) before submodules' pom.xml are executed, hence no test report from test1 and test2 are aggregated in parent.

那么有没有办法在一行中执行 mvn clean install site ,或者我必须执行 mvn clean install 首先 mvn网站

So is there a way to execute mvn clean install site in a single line, or do I absolutely have to execute mvn clean install first then mvn site?

感谢您的任何建议。

推荐答案

显示的配置使用与父和聚合器相同的POM。文档还将讨论继承与多模块。父POM将值传递给其子节点,而聚合器POM管理一组子项目或模块。 Sonatype Maven Book的第3.6.2节详细介绍了。

The config shown uses the same POM as both the parent and aggregator. Documentation will also discuss inheritance vs. multi-module. A parent POM passes values to its children, while an aggregator POM manages a group of subprojects or modules. Section 3.6.2 of the Sonatype Maven Book goes into a lot more detail.

根据我的经验,当父级和聚合器POM 分开时,Maven构建工作会更好。

In my experience, Maven builds work better when the parent and aggregator POMs are separate.

原因与依赖关系,Maven如何确定构建顺序以及Maven如何运行命令有关。如果您的样本结构是这样的,使用单独的父和聚合器,其中test1和test2继承自父级:

The reason has to do with dependencies, how Maven determines build order, and how Maven runs commands. If your sample structure was like this, with a separate parent and aggregator, where test1 and test2 inherited from parent:

project (aggregator POM)
  |- parent
  |- test1
  |- test2

然后Maven构建顺序看起来像这样(假设当然正确定义了依赖关系):

then the Maven build order will look like this (assuming dependencies are defined correctly of course):


  • parent

  • test1

  • test2

  • 聚合器

  • parent
  • test1
  • test2
  • aggregator

mvn clean install site 这样的命令将从父项目开始,清理和构建,安装到本地工件库,然后生成项目站点。它会重复此命令序列,以便显示每个项目。由于聚合器是独立的,因此它可能具有单独的配置来执行测试,覆盖和javadoc的报告聚合。聚合器最后运行,因此模块的coverage数据库和中间javadoc文件已经存在,可以正确组合。

A command like mvn clean install site will start with the parent project, cleaning and building, installing to the local artifact repository, then generating the project site. It repeats this command sequence in order for each project shown. Because the aggregator is separate, it may have separate configuration to perform report aggregation for tests, coverage and javadoc. The aggregator runs last, so the coverage databases and intermediate javadoc files for the modules already exist, ready to be appropriately combined.

当父和聚合器是同一个POM时你拥有它,构建顺序是:

When the parent and aggregator are the same POM as you have it, the build order is:


  • parentAggregator

  • test1

  • test2

现在运行 mvn clean install site ,Maven必须构建父POM(这里是parentAggregator),因此它具有子模块所需的信息,具体取决于它。 parentAggregator也是聚合器,因此Maven很乐意将任何聚合报告作为 site 的一部分运行。当然,尚未构建子模块,因此没有要聚合的覆盖数据库或中间javadoc文件。或者,可能更糟糕的是,存在旧文件,因此聚合器对这些文件进行操作。在构建父聚合器之后,构建子模块,然后命令退出。构建结束时没有最终聚合步骤。整体结果可能不是你想要的。

When you run mvn clean install site now, Maven must build the parent POM (here, parentAggregator) so it has the information required for the child modules depending on it. parentAggregator is also the aggregator, so Maven will happily run any aggregating reports as part of site. Of course, the child modules haven't been built yet, so there are no coverage databases or intermediate javadoc files to aggregate. Or, possibly worse, old files exist so the aggregator operates on those files. After the parent-aggregator builds, the child modules build, and then the command exits. There is no final aggregation step at the end of the build. The overall results are probably not what you want.

这种现象可能会导致多模块项目发布版本出现问题,开发人员写了一个 versions-maven-plugin mojo 以帮助纠正它们。问题:

This phenomenon can cause problems during multi-module project release builds, painful enough that developers wrote a versions-maven-plugin mojo to help correct them. The issue:


如果你有一个多模块构建聚合器pom(即包含pom和模块部分的那个)也是其子模块引用的父级,并且聚合器版本与子级模块的父节中指定的版本不匹配,Maven将不允许您构建项目。

If you have a multi-module build where the aggregator pom (i.e. the one with packaging of pom and the modules section) is also the parent referenced by its child modules, and the aggregator version does not match the version specified in the parent section of the child modules, Maven will not let you build the project.

这篇关于多个模块项目的Maven报告和站点生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆