How to use Maven for agile development

1

Category:

How to Use Maven on Your Project

Created by Susan Davis. Last edited by Jessamyn Smith, 3 years ago.Install MavenObtain Maven (version2.0.5) from file://Iwdbackup/iwd_apps/Maven or http://maven.apache.org/download.html Unzip maven-2.0.5-bin.zip to the directory you wish to install Maven. Add the bin directory to your path, eg. SET PATH="C:\Program Files\maven-2.0.5\bin";%PATH% Make sure that JAVA_HOME is set to the location of your JDK. Run mvn --version to verify that it is correctly installed. Modify maven's settings.xml to reference internal Intelliware repositories.Overwrite a standard settings.xml in /conf with this one: settings.xml. Use Maven to create an applicationMaven provides an archetype mechanism to allow you to quickly create different kinds of projects. Some common archetypes are:
maven-archetype-webapp for web applications maven-archetype-simple - quick start archetype to generate simple project maven-archetype-site archetype - to create a site docummentation for your project For a more comprehensive list see Codehaus's Archetypes ListYou can also create your own archetypes. To do so, refer to maven's Guide to Creating Archetypes
Example: Creating a web applicationRun a following command in the directory where you want your project to be created:
mvn archetype:create -DgroupId=ca.intelliware -DartifactId=simpleWebApp -DarchetypeArtifactId=maven-archetype-webapp
The result is: simpleWebApp+-- pom.xml+-- src/ +-- main/ +-- resources/ +-- webapp/ +-- index.jsp +-- WEB-INF/ +-- web.xml
The first time you run this (or any other) command, Maven will need to download all the plugins and related dependencies it needs to fulfill the command. Therefore it might take some time when you run it for the first time. Maven uses the concept of repositories to manage your dependencies. When you download maven and install it, it creates your local repository. When we say "maven downloads dependencies" we mean it gets them from the maven repo and copies them to your local repository.
Example: Creating a simple application:mvn archetype:create -DgroupId=ca.intelliware -DartifactId=simpleWebApp
Making sense of pom.xmlWhen you've used maven to create your project, notice that it created a pom.xml file in your main project directory. This file (arcronym of Project Object Model) contains every important piece of information about your project and is essentially one-stop-shopping for finding anything related to your project. To learn more about it, visit Introduction to the POM.
For an example of a POM file that includes configuration for the Intelliware Maven infrastructure see: Sample POM for Intelliware.
Maven on a rollNow that we have our project created, we can add in our code and utilize a whole new bag of Maven tricks.
Note all Maven commands must be run in the same directory as the project's pom.xml file.
mvn test: Runs the JUnit tests for the application. mvn package: Creates a .war file from our project. mvn install: Adds our project .war to the repository for use as a dependency if needed. mvn site: Generates the project website. mvn clean: Cleans the output created in the target directory. mvn eclipse:eclipse: Generates an Eclipse project file. Where, "test", "package", and "install" are Maven lifecycle phases, and "site" and "eclipse" are Maven plugins.
Deploy to WebServer and RunThe Quick and Easy Way
We can now deploy the application to the Jetty webserver and run it by entering:
mvn org.mortbay.jetty:maven-jetty-plugin:runWe can then hit the page and see our application in action: http://localhost:8080/simpleWebApp/
Defining the Webserver in your pom.xml
Specify the plugin for your web server in the project's pom.xml (the example below is for Jetty):
org.mortbay.jetty maven-jetty-pluginWe can then startup the web app by running: mvn jetty:run