Maven:添加对父pom项目的引用

tre*_*bor 14 java eclipse m2eclipse maven

我不确定我是否理解如何正确使用父pom项目.我定义了以下父pom:

<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>../child1</module>
    <module>../child2</module>
</modules>
Run Code Online (Sandbox Code Playgroud)

然后子节点pom引用父节点(每个子节点都有自己的一组依赖关系,这些依赖关系未显示):

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../Parent/pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>child1</artifactId>
Run Code Online (Sandbox Code Playgroud)

这个设置工作正常,并在eclipse(m2eclipse)中正确解析.我可以将这些部署到我的本地存储库,最终得到以下结构,这应该是正确的:

--com
   --example
      --parent
        --0.0.1-SNAPSHOT
          --parent-0.0.1-SNAPSHOT.pom
      --child1
        --0.0.1-SNAPSHOT
          --child1-0.0.1-SNAPSHOT.jar
          --child1-0.0.1-SNAPSHOT.pom
      --child2
        --0.0.1-SNAPSHOT
          --child2-0.0.1-SNAPSHOT.jar
          --child2-0.0.1-SNAPSHOT.pom
Run Code Online (Sandbox Code Playgroud)

我的问题是我现在想要在不同的项目(不是parent,child1或child2)中引用项目,从而引入所有父项的子项.我可以在我的其他项目中添加对它的引用:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

执行此操作时,项目在eclipse中没有显示任何错误,但是没有工件添加到我的类路径中:not child1,child2或它们的任何依赖项.

我一直认为必须有一种方法来拥有一个"主"pom项目,它本身不是一个罐子,但只引用其他罐子,然后能够在某处引用那个"主人",但我不知道这是怎么回事完成了.

khm*_*ise 14

我建议改变你的项目结构的第一件事.

--com
   --example
      --parent
        --0.0.1-SNAPSHOT
          --parent-0.0.1-SNAPSHOT.pom
      --child1
        --0.0.1-SNAPSHOT
          --child1-0.0.1-SNAPSHOT.jar
          --child1-0.0.1-SNAPSHOT.pom
      --child2
        --0.0.1-SNAPSHOT
          --child2-0.0.1-SNAPSHOT.jar
          --child2-0.0.1-SNAPSHOT.pom
Run Code Online (Sandbox Code Playgroud)

我建议采用以下方式:

  --parent (pom.xml)
      +--child1
      +--child2
Run Code Online (Sandbox Code Playgroud)

父进程可以签入版本控制(Git,SVN)进入主服务器或进入主干.除此之外,所有孩子都必须像这样引用父母:

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>child1</artifactId>
Run Code Online (Sandbox Code Playgroud)

在你的父母你可以使用这样的东西:

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>
Run Code Online (Sandbox Code Playgroud)

此外,您只需向项目父级添加父引用即可使用公司pom:

<parent>
    <groupId>com.example</groupId>
    <artifactId>master-pom</artifactId>
    <version>0.0.1</version>
</parent>

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>
Run Code Online (Sandbox Code Playgroud)

在公司pom中,您可以(并且应该)固定不同的插件版本,并使用适当的版本定义公司默认值和依赖关系的建议.

如果你喜欢公司的超级pom,你可以简单地创建一个单独的maven项目,它只包含pom文件,其中包含你的配置,如插件,依赖项等,当然还要将它检入版本控制(Git,SVN等).与其他项目分开.

如果你想引用一个jar文件,换句话说就是多模块构建的工件,你必须使用正确的groupId,artifactId和version.如果您想在其他项目中使用child1,则需要提供以下内容:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>child1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

重要的是要理解多模块构建的父级不是通常使用的工件,因为基于打包类型pom它不会真正创建工件(jar文件).

更新: 您可以仅以有限的方式对具有范围import的dependencyManagement区域执行此操作,但这仅用于在depdencyManagement中使用而不是用于依赖项.另一种解决方案可能是创建一个单独的虚拟项目,该项目具有两个子项作为传递deps的依赖项.但这不是真的美.

因此,解决方案只是直接添加您在项目中使用的两个或更多依赖项,就是这样.

  • 你的最后两段让我觉得我不明白我如何使用父pom.虽然我使用在其中的所有子模块共享的公共依赖项,但我真的想要一个可以添加到其他项目的依赖项,这些项目将引入独立但相关的"子项".我想添加一个将拉入_child1_和_child2_的依赖项. (3认同)

Mik*_*keW 0

尝试创建具有相同打包类型的超级 pom 并将当前父级添加为依赖项。