22

I'm using a library that uses Maven to compile and test.
I was able to compile the library without any problems. While compiling, it seemed as if it downloaded all the dependencies of the library.

Now, I'm trying to use the library in my project. When I compiled the library, I found that a folder called target was created in the library folder and inside that folder, there was another folder called classes. I added the classes folder to my classpath. However, whenever I try to use that library in my project which does not use Maven, it says that it can't find that library's dependencies.

How do I add all of that library's dependencies to my classpath?
Do I need to go and manually download all the library's dependencies and add them to the classpath?
Is there any way I can have Maven do that for me?
What do I need to do so that I can use the library in my project?

My project is in a completely separate directory than the library. Right now, my project seems to be able to load the library files correctly, but just not the library dependencies.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
tyronegcarter
  • 3,876
  • 4
  • 21
  • 24

2 Answers2

14

When you executed mvn install for that library, it should have created a jar file and put it at target/libaryname-version.jar. It would be better to depend on this final jar instead of the contents of the classes folder. Maven also has a goal to download all dependencies of a project. You can execute

mvn dependency:copy-dependencies

Inside the libraries folder and it will copy all dependency jars to target/dependency. By default this will also include jars that are only needed for tests, to exclude these you could use

mvn dependency:copy-dependencies -DincludeScope=runtime
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • 2
    Cool hack! It worked for me but my need was slightly different. In my case, it was enough to get the classpath. You can generate classpath and output it to some file in resources, which you can use later. code is here ` org.apache.maven.plugins maven-dependency-plugin generate-sourcesbuild-classpath ${project.basedir}/src/main/resources/classpath` – raxith Jun 12 '13 at 14:37
  • @raxith submit this as your own answer, the code section is too long to interpret. – Ory Band Apr 09 '14 at 14:56
  • @Jörn Horstmann So after running the copy-dependency goal, to run the code we have to execute the `java -cp "target\*;target\dependency\*" com.myCompany.App` from the project folder? – Quazi Irfan Dec 27 '16 at 07:39
4

Well, there are a couple issues at work here. Maven does the hard work of figuring out all the dependencies required for your library to be built and downloads them. These dependencies get stored locally in your Maven repository (<user home>/.m2/repository), but unless they are needed as a part of an assembly you will not find them in the target folder. At least, not by default. What you need to do first is get Maven to store all dependencies in the build folder (this POM excerpt was cribbed from another SO post):

<project>
...
    <profiles>
        <profile>
            <id>qa</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-dependency-plugin</artifactId>
                            <executions>
                                <execution>
                                    <phase>install</phase>
                                    <goals>
                                        <goal>copy-dependencies</goal>
                                    </goals>
                                    <configuration>
                                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                    </configuration>
                                </execution>
                            </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

With the POM changes shown above you should now be able to get all the JAR dependencies needed by your library and include them in your classpath (from target/lib). I would recommend copying them to another folder on your workstation, since the target folder will be nuked every time you execute the clean goal for the libraries Maven build.

Now having said all that, why not adapt your project to use Maven as well? Then all you would need to do is include the top level JAR as a dependency in your POM, and Maven would handle all its sub-dependencies? This is the power of Maven after all - it would be to your advantage to leverage it.

In any case, good luck with whichever of the two approaches you select.

Community
  • 1
  • 1
Perception
  • 79,279
  • 19
  • 185
  • 195