无法在Maven依赖关系管理中找到jar

时间:2018-11-25 02:59:24

标签: java maven dependency-management maven-dependency

我有2个项目testing.parenttesting.child。 项目结构如下:

enter image description here 他们的个人诗歌如下:

testing.parent

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>testing.group</groupId>
    <artifactId>testing.parent</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <name>testing.parent</name>
    <url>http://maven.apache.org</url>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>    

testing.child

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>testing.group</groupId>
    <artifactId>testing.child</artifactId>
    <version>1.1</version>
    <packaging>jar</packaging>
    <name>testing.child</name>
    <url>http://maven.apache.org</url>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>testing.group</groupId>
                <artifactId>testing.parent</artifactId>
                <version>1.0</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

为了测试dependency management,我尝试将import jar log4j转换为child项目,但是子项目中仍然没有该jar。这是导入罐子的正确方法吗?

2 个答案:

答案 0 :(得分:1)

  

这是导入罐子的正确方法吗?

,将log4j包括在子dependency的{​​{1}}部分中。

pom.xml

此外,请确保也从孩子那里引用<dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <!-- version is optional when using dependency management --> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> ,为此在孩子parent中包括以下内容:

pom.xml

确保父级<parent> <artifactId>testing.parent</artifactId> <groupId>testing.group</groupId> <version>1.0</version> </parent> 中与

相似的引用
pom.xml

答案 1 :(得分:0)

如果将以下部分添加到子pom.xml文件中,则不需要通过子pom.xml单独导入lof4j依赖项。

<parent>
    <artifactId>testing.parent</artifactId>
    <groupId>testing.group</groupId>
    <version>1.0</version>
</parent>