Maven多模块项目版本管理

时间:2023-01-13 12:50:00

What is the best practice for specifying version of a multimodule maven project?

指定多模块maven项目版本的最佳做法是什么?

I would like to have one version string across all modules. Even if I can have only one version definition in the root parent pom, I need to specify the parent pom version in each pom's. Which means, if I need to change version, I need to change all poms. Practically defeats the purpose. Any ideas??

我想在所有模块中都有一个版本字符串。即使我在根父pom中只能有一个版本定义,我需要在每个pom中指定父pom版本。这意味着,如果我需要更改版本,我需要更改所有poms。实际上违背了目的。有任何想法吗??

3 个解决方案

#1


28  

Have you tried the versions-maven plugin ?

你试过版本的maven插件吗?

with mvn versions:set -DnewVersion="1.1-SNAPSHOT" you are able to set in all underlying maven projects the given version.

使用mvn版本:set -DnewVersion =“1.1-SNAPSHOT”您可以在所有底层maven项目中设置给定版本。

Afterwards you have to do a mvn versions:commit to remove temporary files and commit to your VCS

之后你必须做一个mvn版本:提交删除临时文件并提交到你的VCS

#2


7  

The better way is define your version in parent pom.xml as follows.

更好的方法是在父pom.xml中定义您的版本,如下所示。

<groupId>com.my.code</groupId>
<artifactId>my_pro</artifactId>
<version>${YOUR_VERSION}</version>

<properties>
    <JDK_VERSION>1.7</JDK_VERSION>        
    <YOUR_VERSION>1.0-SNAPSHOT</YOUR_VERSION>// here you define your version
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
</properties>

Then you don't want to change your version number one by one in all child pom.xml.

然后,您不希望在所有子pom.xml中逐个更改您的版本号。

child pom.xml can add dependency as follows

child pom.xml可以添加依赖项,如下所示

<version>${YOUR_VERSION}</version>

#3


7  

one other "best" way is to create a parent pom beside the multimodule pom which will provide a dependency management containing the multimodule project reference with the version of the parent pom it self. If the multimodule project need one other multimodule project as dependency this will be the best for you. The parent pom have to be part of the multimodule pom:

另一个“最好”的方法是在多模块pom旁边创建一个父pom,它将提供一个依赖关系管理,其中包含多模块项目引用和它自己的父pom的版本。如果多模块项目需要另一个多模块项目作为依赖项,那么这对您来说是最好的。父pom必须是多模块pom的一部分:

<modules>
    <module>my.parent.Pom</module>
    <module>my.first.project</module>
    <module>my.second.project</module>
    <module>my.third.project</module>
</modules>

And the parent pom should contains the dependency management for your multimodule project

父pom应包含多模块项目的依赖关系管理

<version>VersionNumber</version>
<packaging>pom</packaging>
...
...
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.first.project</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.second.project</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.third.project</artifactId>
            <version>${project.version}</version>
        </dependency>

All your multimodule project will not define the groupId and the version number because they will got those information from the parent pom and additionally if the project "third" need the dependency my.first.project then the dependency in the project "third" pom file will be:

你的所有多模块项目都不会定义groupId和版本号,因为他们将从父pom获取这些信息,另外如果项目“third”需要依赖my.first.project那么项目“third”pom文件中的依赖项将会:

<dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.first.project</artifactId>
        </dependency>

This is the way or we are dealing with the multi module projects and version management.

这是我们处理多模块项目和版本管理的方式。

#1


28  

Have you tried the versions-maven plugin ?

你试过版本的maven插件吗?

with mvn versions:set -DnewVersion="1.1-SNAPSHOT" you are able to set in all underlying maven projects the given version.

使用mvn版本:set -DnewVersion =“1.1-SNAPSHOT”您可以在所有底层maven项目中设置给定版本。

Afterwards you have to do a mvn versions:commit to remove temporary files and commit to your VCS

之后你必须做一个mvn版本:提交删除临时文件并提交到你的VCS

#2


7  

The better way is define your version in parent pom.xml as follows.

更好的方法是在父pom.xml中定义您的版本,如下所示。

<groupId>com.my.code</groupId>
<artifactId>my_pro</artifactId>
<version>${YOUR_VERSION}</version>

<properties>
    <JDK_VERSION>1.7</JDK_VERSION>        
    <YOUR_VERSION>1.0-SNAPSHOT</YOUR_VERSION>// here you define your version
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
</properties>

Then you don't want to change your version number one by one in all child pom.xml.

然后,您不希望在所有子pom.xml中逐个更改您的版本号。

child pom.xml can add dependency as follows

child pom.xml可以添加依赖项,如下所示

<version>${YOUR_VERSION}</version>

#3


7  

one other "best" way is to create a parent pom beside the multimodule pom which will provide a dependency management containing the multimodule project reference with the version of the parent pom it self. If the multimodule project need one other multimodule project as dependency this will be the best for you. The parent pom have to be part of the multimodule pom:

另一个“最好”的方法是在多模块pom旁边创建一个父pom,它将提供一个依赖关系管理,其中包含多模块项目引用和它自己的父pom的版本。如果多模块项目需要另一个多模块项目作为依赖项,那么这对您来说是最好的。父pom必须是多模块pom的一部分:

<modules>
    <module>my.parent.Pom</module>
    <module>my.first.project</module>
    <module>my.second.project</module>
    <module>my.third.project</module>
</modules>

And the parent pom should contains the dependency management for your multimodule project

父pom应包含多模块项目的依赖关系管理

<version>VersionNumber</version>
<packaging>pom</packaging>
...
...
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.first.project</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.second.project</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.third.project</artifactId>
            <version>${project.version}</version>
        </dependency>

All your multimodule project will not define the groupId and the version number because they will got those information from the parent pom and additionally if the project "third" need the dependency my.first.project then the dependency in the project "third" pom file will be:

你的所有多模块项目都不会定义groupId和版本号,因为他们将从父pom获取这些信息,另外如果项目“third”需要依赖my.first.project那么项目“third”pom文件中的依赖项将会:

<dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.first.project</artifactId>
        </dependency>

This is the way or we are dealing with the multi module projects and version management.

这是我们处理多模块项目和版本管理的方式。