aspose file tools*
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
Author

Maven: Using jars in classpath without defining dependencies

Gunnar Meyer
Greenhorn

Joined: Jun 12, 2009
Posts: 2
Hi,

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?

Cheers,
-Gunnar
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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: 16250
    
  21

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.



Customer surveys are for companies who didn't pay proper attention to begin with.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5843
    
    7

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
Greenhorn

Joined: Jun 12, 2009
Posts: 2
Hi,

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,
-Gunnar
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Maven: Using jars in classpath without defining dependencies