使用Maven 2构建子项目时丢失工件

Dam*_*ien 4 inheritance maven-2 aggregation

我有一个父项目,有5个孩子,彼此之间也有依赖关系.我<parent>在子<module>节点pom.xml中使用了inheritence 元素,在父节点中使用了元素的聚合.

我的父母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.domain</groupId>
<artifactId>Parent</artifactId>
<packaging>pom</packaging>
<version>RELEASE</version>
<name>Parent</name>

<modules>
    <module>../Child1</module>
    <module>../Child2</module>
    <module>../Child3</module>
    <module>../Child4</module>
    <module>../Child5</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.domain</groupId>
            <artifactId>Child1</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.domain</groupId>
            <artifactId>Child2</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>
</project>
Run Code Online (Sandbox Code Playgroud)

Child3 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.domain</groupId>
<artifactId>Child3</artifactId>
<name>Child3</name>
<packaging>war</packaging>

<parent>
    <artifactId>Parent</artifactId>
    <groupId>com.domain</groupId>
    <version>RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>com.domain</groupId>
        <artifactId>Child1</artifactId>
    </dependency>
    <dependency>
        <groupId>com.domain</groupId>
        <artifactId>Child2</artifactId>
    </dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

当我mvn install在Parent或Child1上运行时,一切正常.但是当我在Child3上运行它时,我得到以下错误:

[INFO] Failed to resolve artifact.
Missing:
----------
1) com.domain:Child1:jar:RELEASE
...
2) com.domain:Child2:jar:RELEASE
Run Code Online (Sandbox Code Playgroud)

我的设置出了什么问题?

Pas*_*ent 6

我不会真正尝试解决您当前的方法问题,而是会考虑我认为在这种情况下的最佳做法.随意采用或不采用:)

首先,请注意,RELEASE(和LATEST)是一个特殊的关键字(不确定你是否知道这一点)并且RELEASE在某种程度上被误用(具有定义的版本的父版本RELEASE没有意义).无论如何,这些特殊关键字是一个坏主意,并且为了构建可重复性而被弃用(我不确定它们是否在Maven3中得到支持),只是避免使用它们.

因此,如果项目处于活动开发状态,请使用SNAPSHOT版本,即修改父项,如下所示:

<project>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.domain</groupId>
  <artifactId>Parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>Parent</name>

  <modules>
    <module>../Child1</module>
    <module>../Child2</module>
    <module>../Child3</module>
    <module>../Child4</module>
    <module>../Child5</module>
  </modules>

</project>
Run Code Online (Sandbox Code Playgroud)

请注意,我删除了dependencyManagement元素,我不认为它为多模块构建的内部依赖项提供了很多附加值,并建议在声明它们时使用${project.groupId}${project.version}内置属性:

<project>
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <artifactId>Parent</artifactId>
    <groupId>com.domain</groupId>
    <version>1.0-SNAPSHOT</version><!-- must hard code this -->
  </parent>

  <!--groupId>com.domain</groupId--><!-- you can skip this, you inherit it -->
  <artifactId>Child3</artifactId>
  <packaging>war</packaging>

  <name>Child3</name>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>Child1</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>Child2</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

正如我所写,我认为使用dependencyManagement内部依赖非常有用,这就是我设置项目的方式.但如果你愿意,你可以.只需使用属性不重复信息.