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.
Maven does set the classpath to the dependencies correctly, but not prefixed with repository location. It will look like this in your Manifest file. It is upto you to place the dependant jars in the same folder as the jar which you are running.
You can get this information in the Maven Tool Window. First go to View → Tool Windows → Maven, to make sure that the Maven window is visible. The top-level elements in the tree are your direct dependencies, and the child elements are the transitive dependencies.
Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With