Creating a Web Project using Maven and Eclipse
Last week I was in a business travel where I went help in an important project for a certain bank in Brazil. The project is interesting, however the build process is taking a long time. It happens because the build process is manual, in other words, the deployer must get the code from repository and generate the EARs, many and many .WARs and some .JARs manually. It is consuming too much time in the building process. Unfortunately it is a common trouble in many probjects and fortunately, there already is a great solution for that: An Automated build tool. In this topic, we’re going to talk about Maven 2, more precisely, Maven 2 + Eclipse + Web Project.
Maven 2
Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.
No matter if you’re working in a medium or large project, in all cases, Maven is useful for building/management process. In the internet and even in the official Maven’s site you’ll find a lot of information regarding Maven, however in this topic, I’ll show you, step by step, how to create a Web Project using Eclipse. For that, we’re going to use an excellent Eclipse Plugin, called m2eclipse.
Installing M2Eclipse Plugin
Through the Software Updates (within Eclipse), add the following URL: http://m2eclipse.sonatype.org/update/. Make sure to select Maven Integration and Maven Integration for WTP.

Setting Up the Maven Repository
Before go to the next step, let’s setup the Maven Repository. To do that, open the Maven Indexes View within Eclipse. Right mouse click on view and go to Add Index. The repository url is: http://repo1.maven.org/maven2/ and the repository id is: central.

Further information can be found in the Maven Wiki.
Creating the Web Project
Within Eclipse, go to New -> Project -> Maven -> Maven Project. On the next screen, keep the default values and click on Next.
On second screen, select the maven-archetype-webapp option and click on Next.

On the third screen, insert the Group Id and Artifact Id and click on Finish. The project will be created into eclipse workspace.

Setting up the Eclipse Folders
Although the project has already been created, we need to setup some folders to Maven “sees” our Java classes, as well as our JUnit classes. As you can see in the Project explorer, the project has been created with the src/main/webapp and src/main/resources folders. However, Maven sees the java classes into src/main/java folder and the JUnit classes into src/test/java. So, let’s create them.
Right click on Project Name -> Properties -> Java Build Path -> Source. By default, you’ll see only one record (src/main/resources). We must create more two entries, one point to src/main/java and another one point to src/test/java. To do that, click on Add Folder button and add them.
The folders should appear like below:

Setting up the POM.xml
So far, the M2Eclipse has been installed, the Maven project has been created and the Eclipse required folders have been created, however there are more two few changes to do. One is to setup the JUnit version and the another one is to setup the Java version. Both changes are made on the POM.xml file. Hence, edit the POM.xml file, change the JUnit version to 4.5 (the latest one) and insert the following lines into build tag.
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins>
We’re done. Now you can test the maven, typing mvn test or mvn package into console, or use the Maven shortcut, right click on project name -> Run As -> Maven test or Maven package.
M2Eclipse Dependency Feature
A nice feature from M2Eclipse is its dependency control. Instead edit yourself the POM.xml to add any dependency (like Hibernate, Log4J, etc), you can use a friendly interface.
Right click on project name -> Maven -> Add Dependency. Try out the hibernate-core. You’ll see a screen like this:

Simply click ADD to add hibernate dependency into the project. If you choose the option Update Dependency, Maven will download from its repository the required JARs files. When you package your application, Maven will, automatically, put the JARs files into WEB-INF/lib directory. Fantastic, isn’t it?
Conclusion
Maven is a fantastic and easy tool to manage our build process. It is indispensable for medium and large project (why not small one too?). You will gain productive using maven and will not waste time with boring process.
Even you’re working with a remote team, maven can help you in the software integration. Also, Maven can build friendly reports for the entire team (check out the surefire plugin). Hence, if you’re not using Maven yet in your projects, it is a good time to get used with it, certainly it will help you a lot (and you’ll never abandoned it).
I hope that topic be useful for anyone. If you have any question or comments, fell free to leave your message below.

Brazilian guy, IT Specialist, Linux and Mac User. Work with Java/JEE and IBM Products, such as: WebSphere and DB2. Like studying Ruby, Android and IOS. Also, I like playing tennis, however I am not good enough. Write a post in this blog once a year. Follow me on twitter if you understand portuguese: @jairrillo.
hi, jairillo,
i am a newbies using maven, and i so interested on it. i follow your guide above, but when i right klik project–> Maven -> Add Dependency .. then i write hibernate-core.. none is showed in box under it. could you please to help me…