This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Ant, Maven and Other Build Tools and the fly likes add lib jars to war file using maven Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "add lib jars to war file using maven" Watch "add lib jars to war file using maven" New topic
Author

add lib jars to war file using maven

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
I'd like to use Spring framework with my web application. I put a bunch of spring jar files under WEB-INF/lib/ directory, I tried this:

when I run mvn, it gives error:

Missing:
----------
1) spring:spring:jar:2.0.8

Try downloading the file manually from the project website.

Then, install it using the command:
mvn install:install-file -DgroupId=spring -DartifactId=spring -Dversion=2.0.8 -Dpackaging=jar -Dfile=/path/to/file

Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=spring -DartifactId=spring -Dversion=2.0.8 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

Path to dependency:
1) com.sun.faces:VOCPortal:war:1.0
2) spring:spring:jar:2.0.8

----------
1 required artifact is missing.



Then I tried: mvn install:install-file -DgroupId=springLib -DartifactId=springLib -Dversion=2.0.8 -Dpackaging=jar -Dfile=WebContent/WEB-INF/lib/spring-core-2.0.8.jar

It compiles fine. But when I look at the generated war file, there is no lib directory under WEB-INF, and it failed to run in tomcat

How can I add other spring jar files to the war file? Thanks.


SCJA, SCJP5.0, SCBCD, SCWCD
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://maven.apache.org/ref/2.2.1/maven-model/maven.html#class_dependency" target="_new" rel="nofollow">systemPath is not recommended.

In any case, maven assumes your runtime environment provides "system" scoped libraries.

Maven will automatically package runtime-scoped libraries. Why are you keeping your required libraries in /WEB-INF/lib rather than letting Maven do its job and keep them non-local/etc.?
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
David Newton wrote:
Maven will automatically package runtime-scoped libraries. Why are you keeping your required libraries in /WEB-INF/lib rather than letting Maven do its job and keep them non-local/etc.?


Could you tell me what should be the correct way to use maven to handle those jar files?
I think specify each jar with a <dependency> in the pom.xml is too long if there are many jars.... and I don't like to always download jars since I often encounter download failure.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't know how many dependencies you have, but there are a number of ways their management can be handled.

Maven won't download every time, only if the version you're dependent on isn't in your local repository.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15961
    
  19

reubin haz wrote:
I think specify each jar with a <dependency> in the pom.xml is too long if there are many jars.... and I don't like to always download jars since I often encounter download failure.


You don't have to list each and every jar. If a jar is a dependency of another JAR you can omit it from the POM, since Maven will resolve sub-dependencies using the depending jar's POM from the repository.

In any event, as David said, your local repository serves as a cache, so JARs are only downloaded once per JAR version. If you run maven using the "-o" (offline) option, the network won't even be referenced at all, although you need an online build at least once to get the dependencies into your local cache.

The ability to do all of the above counts among Maven's major strengths, and additionally eliminates the need to manually replicate copies of jars throughout multiple projects.


Customer surveys are for companies who didn't pay proper attention to begin with.
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
So could you tell me where to put other jar files in my 'local repository' or in my project, and how to specify in maven so maven know what I have already what not? I have tried to put them under /src/main/webapp/WEB-INF/lib/, and also tried to put in /WebContent/WEB-INF/lib/ then I specified in the pom I mentioned in my first posting. But they are not working.

I have spring-aop-2.0.8.jar, spring-beans-2.0.8.jar, spring-context-2.0.8.jar, ...., spring-web-2.0.8.jar, spring-webmvc-2.0.8.jar. There are 16 spring jars and a bunch of other jars. I could only guess dependency among jars by their names, like, spring-web-2.0.8.jar and spring-webmvc-2.0.8.jar. So its not easy to figure out which jars should be specified, which ones are not needed...

Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I think you should take a step back and read up on Maven--you might just be missing the point.

*You* don't put the jars anywhere--*Maven* does. You specify a dependency, say "spring" version "2.0.8". Maven downloads the jars and puts them in the repository. That's it. Maven handles the transitive dependencies--not you. If you know you're using a library, you can list it as a project dependency--but stop there; let Maven figure out the transitives.

When Maven packages the web app into a war your dependencies will be copied from the repo into your war.
 
Consider Paul's rocket mass heater.
 
subject: add lib jars to war file using maven
 
Similar Threads
Unable to resolve dependency to a jar file created by MVN in 1st module.
Maven doesn't find a jar that's in my local repo
Maven Dependency issue
Creating an ear file from war and jar files
Hibernate Jars Updation