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

maven-release-plugin与exec-maven-plugin多模块配置及执行时机问题

Maven Release 多模块项目配置问题解答

问题1:脚本执行时机错误,变更未纳入发布提交

你配置的preparationGoals会在版本号更新前执行,但release插件只会自动将pom文件的版本变更纳入后续提交,不会主动检测脚本修改的其他文件。这导致你的tagChangeLog.sh虽然运行了,但修改的liquibase日志文件没被加入发布提交。

解决方法

改用maven-release-plugin的preparationCommitHook配置,这个钩子会在release版本提交前执行,并且会自动将钩子产生的所有文件变更纳入提交。

修改父pom中的release插件配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <configuration>
        <!-- 移除原preparationGoals配置,替换为提交前钩子 -->
        <preparationCommitHook>./tagChangeLog.sh -rv v${releaseVersion}</preparationCommitHook>
    </configuration>
</plugin>

注意:如果脚本不在项目根目录,可调整路径为相对模块的路径,比如./modules/liquibase-module/tagChangeLog.sh。

如果你坚持使用preparationGoals,需要在脚本末尾手动将修改的文件加入git暂存区,比如:

git add src/main/resources/liquibase/changelog.xml

但这种方式不如钩子配置可靠,推荐优先使用preparationCommitHook。


问题2:exec插件被不必要的模块调用,需冗余配置

preparationGoals是在所有模块上执行的Maven目标,当你指定exec:exec@tagLiquibase时,Maven会在每个模块中查找ID为tagLiquibase的exec插件执行配置,找不到就会报错。你之前的冗余配置就是为了规避这个问题,但可以通过以下方式避免:

解决方法1:让exec插件仅在目标模块激活

  1. 在父pom的pluginManagement中,给exec插件添加<inherited>false</inherited>,禁止子模块自动继承:
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <inherited>false</inherited>
    <executions>
        <execution>
            <id>tagLiquibase</id>
            <configuration>
                <executable>/bin/bash</executable>
                <arguments>
                    <argument>./tagChangeLog.sh</argument>
                    <argument>-rv</argument>
                    <argument>v${env.RELEASE_VERSION}</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. 在需要执行脚本的目标模块的build/plugins中,显式引用该插件:
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这样只有显式引用的模块才会加载该exec配置,其他模块不会触发。

解决方法2:用Profile控制插件执行

在父pom中定义仅在目标模块激活的Profile,将exec插件配置放在Profile内:

<profiles>
    <profile>
        <id>run-liquibase-tag</id>
        <activation>
            <property>
                <name>project.artifactId</name>
                <value>你的目标模块artifactId</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>tagLiquibase</id>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <configuration>
                                <executable>/bin/bash</executable>
                                <arguments>
                                    <argument>./tagChangeLog.sh</argument>
                                    <argument>-rv</argument>
                                    <argument>v${env.RELEASE_VERSION}</argument>
                                </arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

然后修改release插件的preparationGoals,激活该Profile:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <configuration>
        <preparationGoals>-P run-liquibase-tag exec:exec@tagLiquibase</preparationGoals>
    </configuration>
</plugin>

提示:如果使用preparationCommitHook的解决方案,就不需要再配置exec插件,自然也不会出现这个问题,这是最简洁的处理方式。


内容的提问来源于stack exchange,提问作者Victor Rodriguez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 14:47:02