0
点赞
收藏
分享

微信扫一扫

maven使用${revision}管理多模块版本

汤姆torn 2022-02-11 阅读 258
mavenjava

在maven多模块项目中,可配合插件flatten-maven-plugin及${revision}属性进行全局版本号管理。

flatten-maven-plugin插件示例:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>flatten-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <!-- 避免IDE将 .flattened-pom.xml 自动识别为功能模块 -->
                <flattenedPomFilename>pom-xml-flattened</flattenedPomFilename>
                <updatePomFile>true</updatePomFile>
                <flattenMode>resolveCiFriendliesOnly</flattenMode>
            </configuration>
            <executions>
                <execution>
                    <id>flatten</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>flatten</goal>
                    </goals>
                </execution>
                <execution>
                    <id>flatten.clean</id>
                    <phase>clean</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

父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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.luo.demo</groupId>
    <artifactId>maven-package-demo</artifactId>
    <packaging>pom</packaging>
    <version>${revision}</version>
    <modules>
        <module>module-1</module>
        <module>module-2</module>
    </modules>

    <name>maven-package-demo</name>

    <properties>
        <revision>1.0.0-SNAPSHOT</revision>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--
            推荐使用${project.version}管理子模块依赖版本(兼容不使用${revision}的模式),
            亦可直接使用${revision}。
            -->
            <dependency>
                <groupId>com.luo.demo</groupId>
                <artifactId>module-1-1</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.luo.demo</groupId>
                <artifactId>module-1-2</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                ...
            </plugin>        
        </plugins>
       
    </build>
</project>

子模块pom示例;

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>maven-package-demo</artifactId>
        <groupId>com.luo.demo</groupId>
        <version>${revision}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>module-1</artifactId>

</project>

关于子模块中parent.relativePath使用:

  • 默认值,不设置relativePath即为默认值,等价于<relativePath>../pom.xml</relativePath>,即遵循父子目录层次
    • 即优先查找上层目录../pom.xml
    • 然后查找本地仓库
    • 最后查找远程仓库
    • 推荐自定义的开发项目遵循此种方式(即父子模块需遵循父子目录层次,且保持parent.relativePath的默认值)
  • 空值<relativePath/>,即跳过本地文件目录查找
    • 直接查找本地仓库
    • 最后查找远程仓库
    • 适用于父依赖为第三方公有仓库中的依赖,如spring-boot-starter-parent
  • 其他值,可根据目录层次自行定义(推荐使用相对目录层次),如<relativePath>../module-1/pom.xml</relativePath>

不可混合使用${revision}和明确字符串版本号

若出现父子模块版本号混合使用${revision}和明确字符串形式如1.0.0.-SNAPSHOT,在mvn package会出现类似如下错误:

若使用插件flatten-maven-plugin及${revision},切记要全局(父pom及所有子pom)都使用${revision}来管理版本号(即<version>${revision}</version>

举报

相关推荐

0 条评论