【问题标题】:Is packaging type 'pom' needed when not using project aggregation (multimodule)?不使用项目聚合(多模块)时是否需要包装类型“pom”?
【发布时间】:2011-01-12 17:18:52
【问题描述】:

我想在 Maven 2.2.1 的子项目中继承(父)pom.xml 的依赖项;即使用项目继承。在这种情况下,似乎有必要将默认包装类型从jar 更改为pom

但是,the Maven2 documentation 不是声明打包类型 pom 是项目聚合所必需的,即使用子模块但不是项目继承的多模块项目?

<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>example</groupId>
 <artifactId>example-parent</artifactId>
 <version>1</version>

 <dependencies>
   <dependency>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
     <version>1.2.14</version>
   </dependency>
 </dependencies>
</project>

<project>     
 <parent>
   <groupId>example</groupId>
   <artifactId>example-parent</artifactId>
   <version>1</version>
 </parent>

 <modelVersion>4.0.0</modelVersion>
 <groupId>example</groupId> 
 <artifactId>example-child</artifactId>
</project>

但是如果你用上面的配置调用 Maven(例如mvn clean),你会得到一个错误:

Project ID: example:example-child

Reason: Parent: example:example-parent:jar:1 
 of project: example:example-child has wrong packaging: jar.
Must be 'pom'. for project example:example-child

另一方面,使用以下条目:

<project> 
 ... 
 <packaging>pom</packaging>
 ... 
</project>

在父pom.xml中,Maven可以正常执行。

Maven的这种行为正确吗?

【问题讨论】:

    标签: inheritance maven-2 packaging pom.xml


    【解决方案1】:

    POM ReferenceInheritance 部分所述:

    parentaggregation(多模块)项目的打包类型必须为 pom

    所以 Maven 的行为对我来说似乎是正确的(并且错误消息很好地自我解释)。

    【讨论】:

      【解决方案2】:

      如果您只是想继承依赖项,那么我认为它不需要输入“pom”。 您可以拥有它 jar 并简单地将其指定为您作为孩子的项目的依赖项。但是,您将不会拥有父/子关系,这会阻止您的父项目成为“pom”以外的类型。

      明确地说,您继承了所有依赖项的依赖项(传递依赖项)。

      【讨论】:

        【解决方案3】:

        正如 Pascal 所指出的,这种行为是正确的。

        如果您仍在寻找在模块之间共享依赖项的方法,您可以考虑将相关依赖项捆绑到一个 pom 中,然后让您的模块都依赖于新的“依赖项”pom。

        更多详情请见Maven Book Section 3.6.1

        【讨论】:

          猜你喜欢
          • 2012-06-26
          • 2013-09-15
          • 2018-01-03
          • 2017-06-21
          • 1970-01-01
          • 2014-02-03
          • 2019-03-28
          • 2012-02-06
          • 2019-08-21
          相关资源
          最近更新 更多