i m using eclipse 3.2.2 as IDE. i want maven code to build and deploy my web application, which is using spring web mvc 2.0.
Set your project up as a war project, see http://maven.apache.org/plugins/maven-war-plugin/index.html
Or you could use a reactor style project with a war module, see http://maven.apache.org/guides/mini/guide-multiple-modules.html.
For deployments you could look at the cargo plugin
I'll give you one way, the easiest way, to get started (the question is too vague to be more precise). First, to setup a webapp managed by maven and to build it, you'll have to create a project with a packaging of type war
. The good news is that maven as an archetype for that (a tool to generate a project) so this is actually a very easy step. Just type the following command:
$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp \
-DgroupId=com.stackoverflow.q2220593 \
-DartifactId=myfirstwebapp \
-Dversion=1.0-SNAPSHOT
On Windows, type this command in a command window on a single line without the '\
' at the end. On *nices, you can paste it as is. Once executed, you'll get a ready to use webapp project structure following Maven's conventions:
$ cd myfirstwebapp/
$ tree .
.
|-- pom.xml
`-- src
`-- main
|-- resources
`-- webapp
|-- WEB-INF
| `-- web.xml
`-- index.jsp
If you want to add Java classes directly in the webapp project, create a src/main/java
directory and put your sources there (test classes go in src/test/java
and test resources would go in src/test/resources
). Refer to the Introduction to the Standard Directory Layout for an overview of the default layout.
For a Spring project, you would typically have to add the Spring dependencies in the pom.xml
file and put the application context file in src/main/resources
(files in this directory are available on the class path and will be copied into WEB-INF/classes
).
To build the webapp (compile classes, compile tests, execute tests, package the webapp), simply run:
$ mvn package
This will create a .war in the target directory:
$ ls target/
classes maven-archiver myfirstwebapp myfirstwebapp.war test-classes war
To run the webapp, I suggest to use the Tomcat Maven Plugin:
$ mvn tomcat:run
No extra configuration is required for this basic use case. Check the examples for more advanced use cases.
Another option would be to use the Jetty Maven Plugin (which is often preferred during development). To do so, add jetty-maven-plugin to your pom.xml definition:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
And execute:
mvn jetty:run
to start jetty (and have it scan your project files for changes).
The Cargo Maven Plugin would allow to do similar things in a container agnostic way but it might be a bit harder to setup, at least for advanced use cases. Without more details on what you want to do exactly, I wouldn't recommend it for now.