2

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?

Naman
  • 27,789
  • 26
  • 218
  • 353
ghostrider
  • 2,046
  • 3
  • 23
  • 46
  • Have you taken a look at this link? https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html – vs97 Nov 25 '18 at 03:30

2 Answers2

1

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>
Naman
  • 27,789
  • 26
  • 218
  • 353
  • If I specifically need to include the log4j seperately in the dependency section then whats the use of having the dependency management section? I could have done the same thing without having all the additional sections of parent, dependency management etc and directly including the log4j. – ghostrider Nov 25 '18 at 04:33
  • 1
    *dependency management section* is mostly to control the version of the dependencies in a centralised way. you can read more about it in the docs https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management – Naman Nov 25 '18 at 04:38
  • So just to conclude, in my case only importing the parent dependencies in dependency management wont make the log4j jar available in child. I have to additionally include it in the dependency section as well right? – ghostrider Nov 25 '18 at 05:48
  • 1
    @ghostrider Yes, unless it's anyway *available via some transitive dependencies* – Naman Nov 25 '18 at 05:59
0

If you add below part to child pom.xml file, you don't need to import lof4j dependency separately though child pom.xml.

<parent>
    <artifactId>testing.parent</artifactId>
    <groupId>testing.group</groupId>
    <version>1.0</version>
</parent>
WGSSAMINTHA
  • 180
  • 1
  • 11
  • I am aware of this. I wanted to check the benefits of dependency management. For dependency management its a 2 step process right? – ghostrider Nov 25 '18 at 07:19