Rupeal
RSSLinkedInTwitterFacebook

Setting up a web project using Maven 2

Setting up a web project using Maven 2 is the simplest thing in the world. You just have to type in:


mvn archetype:create -DgroupId=groupId -DartifactId=TestWebapp -Dpackagename=your.package.name

-DarchetypeArtifactId=maven-archetype-webapp

Et voila! A new directory with your project name is created with the full directory structure of your web application. Lets take a look on what's inside of our project directory:

Directory structure

Inside our TestWebApp we can found our newly created pom.xml with a JUnit dependency already setup.

The rest of the structure is pretty intuitive. On src you'll find a directory with our main sources. Under this directory (src) we have resources where we put all properties and configuration files, webapp is where all files related with our web application goes (jsp, images, javascripts, stylesheets, etc.) including the WEB-INF stuff under the directory with the same name. The java files are under a directory that is not created initially by the archetype, but of course it's named java .

This archetype doesn't create our test directory, but it's pretty straight forward since it's same structure as main but it's named test (and also lives under the same directory src as we would expect to). To run our tests we just have to type mvn test.

After completing the structure by creating the java and test directory we have the following project structure:

Complete Directory Structure

To create the WAR file we just have to type in mvn package and all the maven life cycle phases run, including building all source files, resolving dependencies, testing and packaging our WAR file.

Running this command for the first time will take a little longer, since Maven needs to download all necessary files (including our project dependencies) to complete the requested task. On success, Maven will deploy our WAR file on the target directory under the root directory where all compiled classes are!

Simple isn't it?

Rui Alves on February 13th, 2007

Software Development

1
 

Um comentário

  • venkat

    7 December 2007

    Hi

    I want to create the project structure as shown in second diagram. please help me on this. Because i am not able to understand what you are explaining in below lines.

    “The rest of the structure is pretty intuitive. On src you’ll find a directory with our main sources. Under this directory (src) we have resources where we put all properties and configuration files, webapp is where all files related with our web application goes (jsp, images, javascripts, stylesheets, etc.) including the WEB-INF stuff under the directory with the same name. The java files are under a directory that is not created initially by the archetype, but of course it’s named java .

    This archetype doesn’t create our test directory, but it’s pretty straight forward since it’s same structure as main but it’s named test (and also lives under the same directory src as we would expect to). To run our tests we just have to type mvn test.”

    Thanks,
    Venka

     

Deixe um Comentário