【问题标题】:Maven version not getting updated in the .POM file.POM 文件中未更新 Maven 版本
【发布时间】:2013-10-02 01:13:34
【问题描述】:

我正在使用 Maven 构建我的代码。我创建了基于模块的 Maven 结构,如下所示

  • 父-POM
    • 子父级1
      • SP1_Child1
      • SP1_Child2
      • SP1_Child3
    • 子父级2
      • SP2_Child1
      • SP2_Child2
      • SP2_Child3

我所有的模块版本和外部依赖版本都在父 POM 中维护。当我进行完整的 mvn 安装时一切正常,但是当我尝试构建一个像 SP1_Child1 这样的子模块时,构建失败,因为它无法识别其依赖项的版本。我检查了本地机器上的 maven 存储库,我的所有模块都已安装,但 .POM 文件没有版本号。这可能是因为父 POM 上的 mvn install 没有将 ${module.version} 替换为子模块的实际版本。

父-POM

<project ..>  

  <groupId>the.company.project</groupId>
  <artifactId>Parent-POM</artifactId>
  <version>1.0-SNAPSHOT</version>
  ...

  <properties>
     <module.version>1.0</module.version>
  </properties>

</project>

SP1_Child1

<project ..>  

  <parent>
  ...
  </parent>

  <groupId>the.company.project</groupId>
  <artifactId>SP1_Child1</artifactId>
  <version>${module.version}</version>
  ...

</project>

我的 mvn install 如何更新 maven 存储库中 .POM 文件中的版本?或者我怎样才能在没有任何版本错误的情况下运行我的子模块之一?

【问题讨论】:

    标签: maven dependencies version parent pom.xml


    【解决方案1】:

    子 pom 的默认布局应如下所示。

    <project ..>  
    
      <parent>
        <groupId>the.company.project</groupId>
        <artifactId>SP1_Child1</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
    
      <groupId>the.company.project</groupId>
      <artifactId>SP1_Child1</artifactId>
      ...
    
    </project>
    

    但是你的孩子不应该只在父元素中单独定义版本而不使用属性。该版本会自动从父模块继承到子模块。如果您有相同的组,您也不需要在 child 中定义组。你可以这样使用它:

    <project ..>  
    
      <parent>
        <groupId>the.company.project</groupId>
        <artifactId>SP1_Child1</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
    
      <artifactId>SP1_Child1</artifactId>
    
      ...
    
    </project>
    

    【讨论】:

    • 嗨,如果 SP1_Child1 的父部分说 the.company.project2SP1_Parent21.0-SNAPSHOT version> 并且.company.project2 的 SP1_Parent2 现在的最新版本为 0.0.5 (0.0.6-SNAPSHOT) ?即即使.company.project 将SP1_Parent1 列为其模块之一,并且.company.project 的SP1_Child1 将SP1_Child1 列为其模块之一,但此SP1_Child1 pom 文件的父部分说,父不是.company.project但它是.company.project2/SP1_Parent2。
    【解决方案2】:

    从父项获取 SP1_Child1 版本对您来说非常烦人,因为它会强制您为任何新版本的 SP1_Child1 项目安装父项的新版本。

    有两种不同的可能情况:

    • 您希望能够管理具有不同生命周期的不同项目。 然后在 SP1_Child1 项目中指定一个版本,在父 POM 中指定其他项目使用的 SP1_Child1 的版本(在这种情况下,两个值可以不同)。

    • 您的应用程序是单片的,即使为了方便而将其组织在不同的模块中。那么在这种情况下,最好的做法是按照 khmarbaise 的建议,为所有项目保留一个版本,并从父级继承该版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-24
      • 2015-04-01
      • 2023-03-19
      • 2017-10-25
      • 2015-08-02
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多