yogii
yogii

Reputation: 88

Maven: How to import dependency of type pom?

I am trying to migrate a java application to maven. There are some dependencies which have been provided as jar files so far. One of these dependencies is jung2, which is available from the maven repository: mvnrepository.com

I need all of the provided modules and I do not understand how to declare this dependecy correctly in my pom.xml such that all corresponding jar files are downloaded and the classes are available at compile time.

This is what my pom.xml file looks like right now:

<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
    <artifactId>myProject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencyManagement>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/net.sf.jung/jung2 -->
            <dependency>
                <groupId>net.sf.jung</groupId>
                <artifactId>jung2</artifactId>
                <version>2.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement> 
</project>

I also tried leaving out <scope>import</scope> and put the dependency into the dependencies section. When executing mvn compile or mvn package, error message occur that the corresponding packages do not exist.

If I additionally add a dependency inside dependencies but outside of dependencyManagement, e.g.

<dependencies>
    <dependency>
    <groupId>net.sf.jung</groupId>
    <artifactId>jung2</artifactId>
</dependency>

I receive an error about missing version. But as far as I understood, this should not be necessary due to dependencyManagement? If I also add <version>2.0.1</version>, then I get the following error message:

Failure to find net.sf.jung:jung2:jar:2.0.1

Upvotes: 4

Views: 8386

Answers (1)

RITZ XAVI
RITZ XAVI

Reputation: 3799

The dependencyManagement tag is used generally when you have a multi module project in maven (where you will have parent-child relationship).

If you specify any dependencies within the dependencyManagement tag, it would NOT actually download the dependencies. Putting the dependency within this tag simply means that this dependency is available (to download / to use) for the child pom. The child pom will have to explicitly provide the groupId and the artifactId co-ordinates to download and use the jar to compile its classes.

If you just have a single module project (looks like yours is a single module project) then you can fix this issue by not using the dependencyManagement tag. Simply put your jars in the dependencies tag.

For ex:

<dependencies>
    <dependency>
        <groupId>com.abc</groupId>
        <artifactId>def</artifactId>
        <version>1.0.0</version>
        <type>pom</type>       // This will now download the pom and its associated transitive dependent jars
    </dependency>
    <dependency>
        <groupId>com.pqr</groupId>
        <artifactId>xyz</artifactId>
        <version>1.0.0</version>
        <type>pom</type>    // This will now download the pom and its associated transitive dependent jars
    </dependency>
</dependencies>

Like I said before, the dependencyManagement tag will mostly make sense to use if you have a multi-module project, which isn't your case.

Upvotes: 5

Related Questions