views:

516

answers:

2

I need to use some 3rd party jar in my project. The project is a Spring project and the jar is also using Spring.

Is there a way by which I can include the 3rd party jar in my project? I am finding it difficult to find each and every dependency of the 3rd part jar and inject it.

+2  A: 

I'm not clear exactly what you mean by "include".

If the 3rd party jar is defined as a dependency in your war project, it will automatically be bundled into the WEB-INF/lib folder of the war when it is packaged by the war plugin. Any class in the jar would then be on the classpath and therefore available to be referenced in your Spring configuration. Do you have a more specific requirement than this?

Also note that if the 3rd-party jar is a properly-defined Maven project, it's dependencies will be defined in its pom. Those transitive dependencies are also bundled into the war (unless you have them defined with a non-default scope in which case they might not be).

Any of the jars you find on the Maven central repository should be defined with all their transitive dependencies. If you're having trouble resolving them, please update your answer so that the relevant Maven credentials can be located.

Update based on your comment. Once the jar is on the war's classpath, you can reference any Spring configuration files it declares by importing them into your war's application context. You just specify the import in the form: "jar:file://jarName!/path/to/config.xml"

Rich Seller
How are you expecting that loading my-project's application context will actually load the 3rd party jars application context? This is my problem..
peakit
you didn't mention the any spring configuration in your question, so I wasn't expecting it at all. I've updated my answer to show how the configuration file in the 3rd party jar could be imported into you war's configuration
Rich Seller
Thanks Rich. +1 for the last part of the answer. BTW, do we have more *ways* to handle this. Just want to know all the ways :)
peakit
The only other ways I know involve writing your own configuration processing. I don't think there is a means to auto-discover configuration if that is what you're after.
Rich Seller
+4  A: 

Shouldn't be a problem. Application contexts can be loaded independent of one another in the same JVM, generally. But if you're loading your bean definitions from a resource file in the classpath (e.g. using ClasspathXmlApplicationContext), make sure the location and name of your file does not conflict with the third-party JAR. For example, if they're both located at "/applicationContext.xml" in different JARs in the classpath, you will have a problem. Make yours unique.

Rob H
Ah !! I see.. thanks.
peakit