将项目作为依赖项管理添加到父pom

时间:2019-08-21 21:31:22

标签: maven

我有以下任务: 1)创建3个Java项目A,B和C,它们使用位于包含(“父”)根文件夹中的单个父pom.xml 2)“父” pom.xml应该在根目录下兼作父和聚合器。 3)使用pom.xml创建一个项目D,该项目列出了Java项目中使用的所有依赖项版本 4)在“父” pom.xml中,将D添加为依赖项管理,然后将A,B,C构建为 子模块。

我的问题是如何实现3和4。

我编写了以下代码:

项目D中的pom.xml:

    <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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>parent</groupId>
    <artifactId>parent</artifactId>
    <version>${revision}${sha1}${changelist}</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <artifactId>d</artifactId>
  <packaging>pom</packaging>
</project>

项目“父级”中的Pom.xml:

<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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>parent</groupId>
  <artifactId>parent</artifactId>
  <version>${revision}${sha1}${changelist}</version>
  <packaging>pom</packaging>

  <properties>
    <revision>0.0.1</revision>
    <changelist>-SNAPSHOT</changelist>
    <sha1/>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <modules>
    <module>a</module>
    <module>b</module>
    <module>c</module>
    <module>d</module>
  </modules>

  <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>parent</groupId>
            <artifactId>d</artifactId>
            <type>pom</type>
            <scope>import</scope>
            <version>${revision}${sha1}${changelist}</version>
        </dependency>
        <dependency>
            <groupId>parent</groupId>
            <artifactId>b</artifactId>
            <version>${revision}${sha1}${changelist}</version>
        </dependency>
        <dependency>
            <groupId>parent</groupId>
            <artifactId>c</artifactId>
            <version>${revision}${sha1}${changelist}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13-beta-1</version>
        </dependency>
        <dependency>
           <groupId>com.github.stefanbirkner</groupId>
           <artifactId>system-rules</artifactId>
           <version>1.16.0</version>
           <scope>test</scope>
        </dependency>
    </dependencies>
  </dependencyManagement>

</project>

谢谢!

戴夫

1 个答案:

答案 0 :(得分:1)

您需要引用pom文件(不是默认类型的jar),并且需要将import部分“复制”到dependencyManagement的范围您当前的pom.xml:

    <dependency>
        <groupId>parent</groupId>
        <artifactId>d</artifactId>
        <type>pom</type>
        <scope>import</scope>
        <version>1.0</version>
    </dependency>