C1 Using Maven

Install Maven

Download the maven package from its website and unzip the file. Set environment variables path to Maven’s bin directory, ie E:\maven-2.0.7-bin\maven-2.0.7\bin. Please also make sure that java classpath has been already provided.

 

Type the follwing command to check whether maven is running functionally.  

mvn –version

 

You can specify your user configuration in ${user.home}/.m2/settings.xml . A full reference to the configuration file is available. This section will show how to make some common configurations. Note that the file is not required – defaults will be used if it is not found.

Create maven structure

Using the following command to create the structure:

mvn archetype:create   -DgroupId=hongliang.com.firstHibernate  -DartifactId=hibernatews  -DarchetypeArtifactId=maven-archetype-webapp

 

Each archetype page should enforce the following pattern:

 

  • Archetype name
  • Command line to call the archetype
  • If the archetype can be used in an existing projects directory
  • A tree view of the resulting files
  • Some additional information like the additional properties used by the plugin

To use an archetype:

mvn archetype:create -DgroupId=<your group> -DartifactId=<your artifact> -DarchetypeArtifactId=<wanted artifact> -DarchetypeGroupId=<wanted artifact group>

 

http://docs.codehaus.org/display/MAVENUSER/Archetypes+List

Modify the POM.xml

Some key tags that used in pom file have been listed below:

  • groupIdA universally unique identifier for a project. It is normal to use a fully-qualified package name to distinguish it from other projects with a similar name (eg. &lt;code&gt;org.apache.maven&lt;/code&gt;).
  • artifactId The identifier for this artifact that is unique within the group given by the group ID. An artifact is something that is either produced or used by a project. Examples of artifacts produced by Maven for a project include: JARs, source and binary distributions, and WARs.
  • Packaging – The type of artifact this project produces, for example <code>jar</code> <code>war</code> <code>ear</code> <code>pom</code>. Plugins can create their own packaging, and therefore their own packaging types, so this list does not contain all possible types
  • ModulesThe modules (sometimes called subprojects) to build as a part of this project. Each module listed is a relative path to the directory containing the module.
  • RepositoriesThe lists of the remote repositories for discovering dependencies and extensions. 
  • Dependencies – This element describes all of the dependencies associated with a project. These dependencies are used to construct a classpath for your project during the build process. They are automatically downloaded from the repositories defined in this project. See &lt;a href=&quot;http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html&quot;&gt;the dependency mechanism&lt;/a&gt; for more information.
  • dependencyManagement Default dependency information for projects that inherit from this one. The dependencies in this section are not immediately resolved. Instead, when a POM derived from this one declares a dependency described by a matching groupId and artifactId, the version and other values from this section are used for that dependency if they were not already specified.
  • Build – Information required to build the project

 

http://maven.apache.org/pom.html

 

 

Build project with Eclipse

Go to the project directory and input command

mvn package

 

Unlike the first command executed (archetype:create) you may notice the second is simply a single word – package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get executed are

  • validate
  • generate-sources
  • process-sources
  • generate-resources
  • process-resources
  • compile

 

Input the command

mvn eclipse:eclipse

This will call the eclipse plug-in to build project fit into the eclipse format.

 

Open eclipse and create new project from existing source, which will be the root directory the new project created. You would also need to specify the build path for the artifact/libraries. You can create path variable to point to the directory, which you specified in the setting.xml. The procedure will be properties/Java build Path/ Libraries/ Add variables/ Configure variables/ new/ give the name and path.

 

 

Advertisements
This entry was posted in Maven. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s