You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何确保Maven构建阶段在报告阶段前执行?

问题

需要在pre-integration-test阶段生成的API文档,同时用于package阶段创建的fake javadoc和站点(site)。已知Maven的build与site生命周期相互独立,虽然可以每次执行mvn pre-integration-test site替代mvn site,但希望将所有构建逻辑整合到项目配置中,实现类似maven-surefire-report-plugin的自动关联效果。

现有pom.xml相关配置如下:

Site插件与Fake Javadoc配置

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.10.0</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.4.2</version>
            <executions>
                <execution>
                    <id>fake-javadoc</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classifier>javadoc</classifier>
                        <classesDirectory>${project.build.directory}/site</classesDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

API文档生成配置

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.5.1</version>
                <executions>
                    <execution>
                        <id>generate-apidoc</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <workingDirectory>${basedir}</workingDirectory>
                            <executable>tools/makeSourceDoc</executable>
                        </configuration>
                    </execution>

附加配置

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
        </plugin>
    </plugins>
</reporting>

解决方案

核心思路是让API文档生成步骤同时绑定到build和site生命周期的前置阶段,确保无论执行build流程还是site流程,都会自动生成所需文档。

调整API文档生成的阶段绑定

修改exec-maven-plugin的配置,新增一个执行实例绑定到pre-site阶段(site生命周期的前置阶段),同时保留原有的pre-integration-test绑定,这样两种流程都会触发文档生成:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.5.1</version>
    <executions>
        <!-- 绑定到build生命周期的pre-integration-test,满足fake javadoc打包需求 -->
        <execution>
            <id>generate-apidoc-build</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <workingDirectory>${basedir}</workingDirectory>
                <executable>tools/makeSourceDoc</executable>
            </configuration>
        </execution>
        <!-- 绑定到site生命周期的pre-site,满足站点生成需求 -->
        <execution>
            <id>generate-apidoc-site</id>
            <phase>pre-site</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <workingDirectory>${basedir}</workingDirectory>
                <executable>tools/makeSourceDoc</executable>
            </configuration>
        </execution>
    </executions>
</plugin>

优化说明

  • 执行mvn install时,pre-integration-test阶段会生成API文档,后续post-integration-test阶段的fake javadoc打包可以直接使用该文档。
  • 执行mvn site时,pre-site阶段会自动触发API文档生成,确保站点使用最新的文档内容。
  • 无需手动执行多阶段命令,所有逻辑都整合在pom配置中,符合Maven生命周期的自动执行逻辑。

如果担心重复执行文档生成命令(比如同时执行build和site),可以通过配置Maven的skip属性或自定义条件来避免,但通常文档生成命令的开销较低,重复执行不会有明显影响。


内容的提问来源于stack exchange,提问作者Árpád Magosányi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 16:35:57