This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maven: Using jars in classpath without defining dependencies

 
Gunnar Meyer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gunnar Meyer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic