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

如何在Maven父POM中向所有模块写入含版本号的文件?

实现向Maven父POM所有模块目录写入含父版本号的文件

可以通过maven-antrun-plugin实现需求,在父POM中配置该插件,绑定到verify生命周期阶段,借助Ant的文件操作任务,遍历所有模块目录并写入包含父项目版本的文件。

具体配置步骤

在父POM的<build>节点下添加以下插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>write-version-to-modules</id>
                    <!-- 绑定到verify阶段,执行mvn clean verify时触发 -->
                    <phase>verify</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <!-- 遍历父POM中声明的所有模块 -->
                            <for param="module">
                                <path>
                                    <pathelements>
                                        <!-- 获取所有模块的相对路径 -->
                                        <pom refid="maven.project.modules"/>
                                    </pathelements>
                                </path>
                                <sequential>
                                    <!-- 拼接模块的绝对路径 -->
                                    <property name="module.dir" value="${project.basedir}/@{module}"/>
                                    <!-- 检查模块目录是否存在 -->
                                    <available file="${module.dir}" type="dir" property="module.dir.exists"/>
                                    <if>
                                        <equals arg1="${module.dir.exists}" arg2="true"/>
                                        <then>
                                            <!-- 写入文件,文件名可自定义,这里用version.txt -->
                                            <echo file="${module.dir}/version.txt" append="false" encoding="UTF-8">
                                                ${project.version}
                                            </echo>
                                        </then>
                                    </if>
                                </sequential>
                            </for>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

配置关键点说明

  • 生命周期绑定:将插件执行绑定到verify阶段,和你要求的mvn clean verify命令匹配,执行到该阶段时自动触发文件写入操作。
  • 模块路径处理:通过<pom refid="maven.project.modules"/>获取父POM中声明的所有模块相对路径,再拼接父项目的根目录路径${project.basedir},得到模块的绝对目录。
  • 文件写入:使用Ant的<echo>任务写入文件,append="false"表示覆盖已有文件,encoding="UTF-8"保证文件编码统一,内容直接引用父项目的版本变量${project.version}。
  • 目录检查:添加<available>任务检查模块目录是否存在,避免因模块目录缺失导致报错。

完整父POM示例

将上述插件配置加入你提供的父POM后,完整内容如下:

<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>com.mp</groupId>
    <artifactId>parentApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>parentApp</name>
    <description>This is just to test pom inheritance</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modules>
        <module>../example1</module>
        <module>../example2</module>
        <module>../example3</module>
        <module>../example4</module>
        <module>../example5</module>
        <!-- 其他模块 -->
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>write-version-to-modules</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <for param="module">
                                    <path>
                                        <pathelements>
                                            <pom refid="maven.project.modules"/>
                                        </pathelements>
                                    </path>
                                    <sequential>
                                        <property name="module.dir" value="${project.basedir}/@{module}"/>
                                        <available file="${module.dir}" type="dir" property="module.dir.exists"/>
                                        <if>
                                            <equals arg1="${module.dir.exists}" arg2="true"/>
                                            <then>
                                                <echo file="${module.dir}/version.txt" append="false" encoding="UTF-8">
                                                    ${project.version}
                                                </echo>
                                            </then>
                                        </if>
                                    </sequential>
                                </for>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

测试验证

执行命令:

mvn clean verify

执行完成后,检查每个模块目录(如../example1、../example2等)下是否生成了version.txt文件,文件内容应为父项目的版本号0.0.1-SNAPSHOT。

内容的提问来源于stack exchange,提问作者Hare Krshn

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 14:20:23