4

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

M. Justin
  • 14,487
  • 7
  • 91
  • 130
yogii
  • 88
  • 1
  • 6
  • This is inside a `` section, so either you need to add a `` outside of it refencing it, or you take it out of the dependency management. Refer to http://stackoverflow.com/questions/2619598/differences-between-dependencymanagement-and-dependencies-in-maven?rq=1 – Tunaki Aug 11 '16 at 10:11
  • @Tunaki Thanks for your answer, I edited my original post. Adding the dependency outside the `dependencyManager` did not work so far. – yogii Aug 11 '16 at 10:29
  • Yes you need to add the type (and maybe the scope, I'm not sure). – Tunaki Aug 11 '16 at 10:31
  • So I should just copy the `dependency`from the `dependecyManagement` section to `dependencies`? This also does not work, the corresponding packages can not be found when I try to compile / package, – yogii Aug 11 '16 at 10:35
  • Add the type as Tunaki mentioned, and try adding '-U' to the mvn command to force Maven to re-resolve the dependencies. – user944849 Aug 11 '16 at 12:53

1 Answers1

5

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.

RITZ XAVI
  • 3,633
  • 1
  • 25
  • 35