Saturday, February 4, 2012

Maven intro + Get your first Spring MVC JPA project up and running in no time

Here you can find few basic things about Maven, see how to install m2e Eclipse plugin, create and run a simple spring-mvc-jpa project.

Intro

Maven is a build tool for managing tasks you repeat a lot, such as building project, generating documentation etc. 

Nevertheless, one of the first associations on Maven is dependency management. Once you declare a dependencies, they are resolved automatically - Maven takes care of downloading them, sorting out a classpath, even deploying them if needed.

It comes very handy if you want to establish a project structure (using archetypes - "patterns" for Maven projects) i.e. define a project backbone and thereby allow quick start of every next project, and prevent versatility and incompatibility.

If you want to see a quick overview of Ant and Maven differences/advantages I recommend Adam Bien's Maven vs Ant resume.

Main Maven functionalities are plugins, archetypes, dependency mechanism, profiles, repositories, scopes etc. 

Installing Maven

Maven installation instructions can be found here.

Intalling m2e Eclipse plugin

Go to Help>Install New Software...
Set "Work with" to "--All Available Sites--"
Narow down the search by typing in "maven".

Choose m2e - Maven Integration for Eclipse and complete the installation.



(In case there's no such option, add m2eclipse update site yourself and then choose m2e - Maven Integration for Eclipse.)


After Eclipse restart we can test the m2e plugin by, for example, creating a new project.


Creating a  simple web project

Click on File>New>Project...
Narrow down the choice by typing "maven", and pick Maven Project.


Wizard will lead you through the same steps you would pass if you were using Maven from a command line.


Let's create a Spring-MVC-JPA project. From a archetype list choose spring-mvc-jpa-demo-archetype.


Fill in the field values:
groupId - to which group the artifact should belong
artifactId - how the project and java artifact will be named
version - initial version of the project
package - main package for your java code


Click "Finish" and let the magic begin! Maven will soon create project structure according to choosen archetype (in this case spring-mvc-jpa-demo-archetype).


To run a project:
Right click on a project>Run as>Maven build...
Specify goal "jetty:run" and click on Run. 
First build might take a little longer.


Open your browser and check: http://localhost:8080/testproject.


Voila!


No comments:

Post a Comment