File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Ant, Maven and Other Build Tools and the fly likes Maven: Using jars in classpath without defining dependencies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Maven: Using jars in classpath without defining dependencies" Watch "Maven: Using jars in classpath without defining dependencies" New topic

Maven: Using jars in classpath without defining dependencies

Gunnar Meyer

Joined: Jun 12, 2009
Posts: 2

I have quite a lot of jars which I need to run JUnit tests with Maven. Unfortunatly I would have to add them all as dependencies by hand. As it would take AGES doing so, I would rather include them by providing the path on my local harddrive. They are contained in a single folder anyway.

Is there any possibility to add an entry to the classpath Maven uses?

Martijn Verburg

Joined: Jun 24, 2003
Posts: 3275

Hi there and welcome to Javaranch!

It's definitely recommended to add them as dependencies, this can be annoying, but it is a one-off task! That said you can do it, see this for details.

Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

You'd still have a lot of work to do, since you can't add all the JARs in a directory to the classpath in a generic way, you have to add each one individually.

You will have to define them as dependencies to get Maven to pull them into its test classpath. You can avoid adding them to the repository, if you prefer and just make them external references, but I forget the scope value that does that.

Here's a little snippet I use to add external Jars to Maven control. A variation of it could be used to generate the dependency XML. I just do a straight-up directory list to generate the basic input file, then edit it to add the details.

An IDE is no substitute for an Intelligent Developer.
Peter Johnson

Joined: May 14, 2008
Posts: 5852

You might not have to list all of the JARs as dependencies. Your code might only directly require a few JARs which in turn have dependencies on other JARs, and so on. Provided those dependencies are properly configured within the Maven repository, specifying the first-level dependencies in your pom should suffice.

The JBoss team recently released a tool, TattleTale, that can be used to track dependencies and transitive dependencies, it might help you with this.

JBoss In Action
Gunnar Meyer

Joined: Jun 12, 2009
Posts: 2

thanks for your replies. As I had about 200 jars (provided by a liferay portal installation btw), adding them all as dependencies was not a realistic approach, as it would require lots of manual work. Finding out which of these .jars I actually need would reduce the number of dependencies, but will most likely take even more time.

Fortunatly the surefire plugin, which is responsible for running tests in Maven, provides an additional classpath functionality which does (almost) exactly what I wanted. As Tim already mentioned, you can't just add all .jars contained in a directory, but have to specify every single one by itself.

So I did the following:

and copy/pasted the content in my pom.

Definitely not a very elegant solution, but it saved me a couple of hours.

Thanks again for your time,
I agree. Here's the link:
subject: Maven: Using jars in classpath without defining dependencies
It's not a secret anymore!