Search code examples
javamavendependency-managementmaven-dependency

Unable to locate the jar in Maven Dependency Management


I have 2 projects testing.parent and testing.child. The project structure is as below:

enter image description here Their individual poms are as below:

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>

In order to test the dependency management I tried to import the log4j jar to child project but still the jar is not available in the child project. Is this the correct way to import the jars?


Solution

  • Is this the correct way to import the jars?

    Yes, include the log4j in the dependency section of the child 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>
    

    Also, make sure to refer the parent from the child as well, include the following in child pom.xml for that:

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

    ensuring the similar reference in the parent pom.xml as

    <modules>
        <module>testing.child</module>
    </modules>