Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to include maven dependencies in war file (WEB-INF/lib) deployed using m2eclipse plugin

 
Benjamin Weaver
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to using the m2eclipse plugin. I am using eclipse Helios 3.6.2. java: 1.6.0.31

I want to include various maven dependencies (.jar files) in my .war depolyment. I want these jars to end up in the war file under myApp/WEB-INF/lib.

Currently I can build the project using the traditional eclipse method: from the Tomcat option | Deploy to war file path. Eclipse is apparently building against my .m2 repository. But presently only my project classes are exported to the war.


1. By what steps do I select the maven dependencies in eclipse for export to WEB-INF/lib ?

2. Once these are set up, how can I run a maven command to build the war file from within eclipse that will export the dependencies? Should I run the command using maven or can I do it in eclipse?


For example, in a simple JDBC application connecting to say oracle, let's say I want to include the jta-1.0.1.jar in under WEB-INF/lib on deployment. I have an entry in my pom file for it, as a dependency. jta-1.0.1.jar needs to end up under myApp/WEB-INF/lib/.

Many thanks in advance!

 
Tim Holloway
Saloon Keeper
Pie
Posts: 18169
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just ensure that your POM is set up to generate a WAR (and not something such as JAR, instead).

Dependencies in "compile" scope will automatically be copied to the target's WEB-INF/lib as part of the Maven build. Dependencies in "provided" scope will not (you would use this for server-supplied jars such as the JSP-api.jar).


To generate a WAR in Maven, 2 goals have to be run: "compile" and "war:war". Running the war goal alone won't compile the Java source . If you prefer, run one of the master goals, such as "package".

To run Maven in Eclipse, select the POM in the project explorer and select the "Run" context menu option. This will build and execute Maven run profile (or use an existing one). Maven run profiles behave just like regular run/debug profiles except that their profile edit dialogs support Maven-specific features.
 
Benjamin Weaver
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Tim, this was an extremely helpful answer.
This helped loads

I also found a good web page for this:
http://www.mkyong.com/maven/how-do-use-maven-to-create-a-dynamic-web-project-in-eclipse/


One thing I notice I am still getting is this file, called 'maven' which is deposited in my META-INF directory. I delete it after expanding the war, but I'll admit it's annoying.

Thanks for the help.

Ben
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get rid of the maven directory under META-INF, set addMavenDescriptor to false for the war plugin:



It is often worthwhile to examine the available configuration options on various plugins:
For the war:war mojo: http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html
For the <archive> config item: http://maven.apache.org/shared/maven-archiver/index.html (these apply to WAR and JAR...)

By the way, if you would have followed the maven directory and its subdirectories you would have eventually reached a copy of you pom.xml. Sometimes this is handy (especially when you examine a JAR file that you got from someone else) and sometimes it's not so handy (if you are writing proprietary software and you have the developer's email addresses in the <developers> section of the POM - you really want your customers to contact support, and not the developers directly).
 
Benjamin Weaver
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Peter: perfect, did the trick.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic