Setup of Dynamic Web Project using Maven

Today I want to talk about Maven. It’s very powerful instrument and if you know how to use it you will make minimum effort to achieve maximum result. In general Maven helps you to manage a project including library dependencies, building process and etc… But in the article I’m going to show you one of the ways how to create a Dynamic Web Project using Maven (in Eclipse IDE).

Pre-requirements:

  • Eclipse IDE for Java EE Developers
  • M2E plugin for Eclipse
  • Maven

1. File > New (Alt+Shift+N) > Dynamic Web Project

  • Input some name for the project in the “Project name” field (e.g. mavenDWP);
  • Select some “Target runtime” (I use Apache Tomcat 7.0);
  • Click on the “Next” button;
  • Create the following directory structure (please pay attention to the directory structure. It’s an important detail in the creation of Dynamic Web Project):

Maven-Dynamic-Web-Project-directory-structure

  • Click on the “Next” button;
  • Select the “Generate web.xml deployment descriptor” check-box;
  • Click on the “Finish” button.

Structure-of-maven-web-project

2. Right click on the project > Configure > Convert to Maven Project

  • Select “Packaging” as a WAR;
  • Click on the “Finish” button;
  • After that, pom.xml will appear in the project’s structure;
  • Move all stuff from the WebContent folder to src/main/webapp;
  • WebContent folder can be deleted after that.

3. Open the project’s root folder in the file system

  • Execute the following command: mvn eclipse:eclipse -Dwtpversion=2.0
  • This command will generate a configuration for eclipse (.classpath, .project, etc.).
  • Expand src > main and make a right click on the webapp folder: Build Path > Use as Source Folder (This point is actual only if src/main/webapp disappeared from the source folders).

Here is the final view of the Dynamic Web Project configured with Maven:

Final-view-of-maven-web-project

That’s it. I hope this tutorial was clear enough and you will be able to set up everything described above without any problems. I will try to answer on every question in the comments.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
  • Fruzenshtein

    Sometimes you can get this error while setting up a Dynamic Web Project:

    CHKJ3000E: WAR Validation Failed: org.eclipse.jst.j2ee.commonarchivecore.internal.exception. DeploymentDescriptorLoadException: WEB-INF/web.xml

    In order to fix this, make right click on your project and choose “Validate”. After this action refresh the project and everything will be ok.

  • http://www.patrick-gotthard.de Patrick

    It’s much easier to install the m2e WTP integration plugin. With that, you only have to create a new Maven project with packaging type ‘war’ and everything else is done automatically.

    • Fruzenshtein

      Agree =)
      But I don’t look for the easiest ways =)

  • Yadab Raj Ojha

    You save my day! Nice clean tutorial. Thank you for this post!

  • prabakar

    awesome….thanks for for valuable time and sharing your knowledge to others…

  • siri

    I’m new to Maven, So my question is I have a dynamic web project wherein my project also contains .properities as well as .txt files. While mavenizing tis project where tis files needs to be resided. After mavenizing, the resultant war needs to be deployed.

    Will be waiting for your reply

    Thanks in advance
    siri

    • Fruzenshtein

      src/main/resources – this folder is for different configurations

  • sri

    Hi,
    First of all Thanks for the post
    I am new to eclipse.
    after creating a new dynamic web project am not able to find the configure option (right click of project) in eclipse.
    Can u please assist me in this regard.

    • Fruzenshtein

      Select the project in a navigator section (left side of Eclips by default), click on the “Project” item in the main menu, click on the “Properties”.

      Done

  • Jonathan García

    “Move all stuff from the WebContent folder to src/main/webapp;”

    for it is this step?

    • http://fruzenshtein.com/ Alex Zvolinskiy

      As soon as you created the src/main/webapp folder and added it to an application’s build path, you can move all stuff from the WebContent folder to the src/main/webapp

      • isccarrasco

        i do that…

        but always see this error…

        descripción: El recurso requerido no está disponible.

        but, if i create the WebContent/WEB-INF/page folder and put the jsp into this folder, the application run perfectly…

        can you help me??

  • my2 koseli

    Hi

    So I am still new to spring MVC and I am following your examples using the annotated version of the Spring MVC.

    I want to create a sample application where I want to list all the test results that are stored in a table. I also want to present a form

    This is my sample model which I want to present in a form:

    package foo;

    import javax.persistence.AttributeOverride;

    @Entity

    @Table(name=”testresults”)

    public class TestResult {

    private string name;

    private string time;

    private string host;

    }

    I then have a controller with following options:

    package foo;

    @Controller

    @RequestMapping(value=”/TestResult”)

    public class TestResultController {

    @RequestMapping(value=”/list”)

    public ModelAndView listOfTeams() {

    ModelAndView modelAndView = new ModelAndView(“list-of-teams”);

    List teams = testresultService.getTeams();

    modelAndView.addObject(“teams”, teams);

    modelAndView.addObject(“filteroptions”, this.getFilterOptions());

    return modelAndView;

    }

    public Map getFilterOptions(){

    Map filter_options = new HashMap();

    filter_options.put(“test_name”, “Test Name”);

    filter_options.put(“host”, “Host”);

    filter_options.put(“time”, “Time”);

    return filter_options;

    }

    }

    This is my jsp which is currently CHOKING up on me because I am not sure what I need to put first in “select path”. I am also not sure if I have the “commandName” correct. Can you please guide me?

    Name:

    host

    time

    name

    ${team.testid}

    ${team.testname}

    ${team.testtime}

    ${team.status}

    ${team.totaltime}

    ${team.db}

    ${team.host}

    ${team.user}

    ${team.version}

    ${team.release}

    • http://fruzenshtein.com/ Alex Zvolinskiy

      Man, you are asking me to do all stuff for you
      I can’t do this
      But you can go through articles on my blog and you will see that I explain step by step how to perform everything you want

      • my2 koseli

        Hi Alex,

        I am trying to pass a column name dynamically to the query in hibernate but I have not been able to do so. Can you shed light on how to do this? I have tried Restrictions like the following:

        getCurrentSession()
        .createCriteria(Result.class)
        .add(
        Restrictions.eq(option.column_name, “first_test”)).list();

        but i am getting following error: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property:

  • Waqar

    It’s something like difficult stuff. You can create maven project simple by click on the new project select maven project and click next and fill project name accordingly. That’s it. Why this drag it needed?

  • Env

    I not understand few things and of course example not work…

    1. You create srcmainwebapp and others but on eclipse screen u have src/main/webapp. In my project i have still srcmainwebapp

    2. You create WebContent folder and copy files into src/main/webapp
    What is puprpose this? (IMO this operation not have sense beacuse u can generate here src/main/webapp)

    2.2 Why u not write about deployment assembly?

    Note for article is: 2/10
    It is waste nerves and time and another trash in interent…

  • Lisa

    Perfect! Everything went smoothly thank you very much!

    • puhspak

      tell me at which place i put index.jsp so i can run this project?

  • MTK

    if the command “mvn eclipse:eclipse” doesn’t work try to do it using eclipse,
    go to the project , right click , and Run as > run Configuration… then click on ‘build maven’, it will show a new window, brows to the project folder, and put ‘eclipse:eclipse’ in goal text zone.

    • Hitesh Sethiya

      good one dude. :)

  • http://www.ingenieroperales.com/ jesus israel perales martinez

    Gracias !!

  • puhspak

    at which place i put my index.jsp so i can run this project any 1 suggest me?

    • ruksar

      you can put it in webapp

  • Nessus

    The best article i’ve seen about this subject. Its easy to understand and goes directly to the objective. Congratulations and thank you.