Maven2

•Installing Maven
•Java Installation
•Windows 7
•Linux
•Maven Configuration
•m2eclipse
•Installing m2eclipse
•Import project modules as Maven projects
•Initialize project project
•Build project project



Installing Maven
Java Installation
While Maven can run on Java 1.4, you should run at least Java 5. Go with the most recent stable Java Development Kit (JDK) available for your operating system.

?
$ java -versionjava version "1.6.0_20"OpenJDK Runtime Environment (IcedTea6 1.9.10) (6b20-1.9.10-0ubuntu1~10.04.2)OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)
Windows 7
•Download apache-maven-2.2.1-bin.zip from http://maven.apache.org/download.html.
•Unzip in C:\Program Files\Apache Software Foundation.
•Add environment variable M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-2.2.1. (to see how to add, modify, remove environment variables on windows 7 : [])
•Add environment variable M2 =%M2_HOME%\bin.
•Upgrade the environment variable Path by adding $M2 and $JAVA_HOME.
Linux
•Download apache-maven-2.2.1-bin.tar.gz from http://maven.apache.org/download.html.
•Pick an appropriate place for it to live, and expand the archive there. If you expanded the archive into the directory /usr/local/apache-maven-2.2.1, you may want to create a symbolic link to make it easier to work with and to avoid the need to change any environment
configuration when you upgrade to a newer version:
?
/usr/local % cd /usr/local/usr/local % ln -s apache-maven-2.2.1 maven/usr/local % export M2_HOME=/usr/local/maven/usr/local % export PATH=${M2_HOME}/bin:${PATH}
•You’ll need to add both M2_HOME and PATH to a script that will run every time you login. To do this, add the following lines to .bash_login.
?
export M2_HOME=/usr/local/mavenexport PATH=${M2_HOME}/bin:${PATH}
Once you’ve added these lines to your own environment, you will be able to run Maven from the command line.

Maven Configuration
In $M2_HOME/conf/settings.xml :

Copy the file settings.xml if it is not present in the directory :
C:\Documents and Settings\username\.m2\

You need login password on nexus server. To obtain them, send an email to *project support mailing list

You can set up the nexus access parameters and the access parameters to your local tomcat (how to configure your tomcat) :

?
<servers>      <server>          <id>project _projects_releases</id>          <username>YOUR_NEXUS_LOGIN</username>          <password>YOUR_NEXUS_PWD</password>      </server>      <server>          <id>project _projects_snapshots</id>          <username>YOUR_NEXUS_LOGIN</username>          <password>YOUR_NEXUS_PWD</password>      </server>      <server>           <id>tomcat</id>           <username>YOUR_LOCAL_TOMCAT_LOGIN</username>           <password>YOUR_LOCAL_TOMCAT_PWD</password>      </server>      <server>           <id>tomcat_project </id>           <username>admin</username>           <password>admin</password>      </server>  </servers>


m2eclipse
The goal of the Eclipse m2eclipse project is to provide Apache Maven support in the Eclipse IDE, making it easier to edit Maven's pom.xml, run a build from the IDE and much more.

Installing m2eclipse
In your eclipse environment (Indigo 3.7) :

•Help -> Install new software...
•Write the URL http://m2eclipse.sonatype.org/sites/m2e in field Work with
•Select m2e - Maven Integration for Eclipse
•Accept the license terms and finish
•Restart eclipse
To display POM files as xml files :

•Window -> Preferences
•Maven -> User interface
•Select the two options as shown in the following screenshot, then click OK



Import project modules as Maven projects
In this section, we suppose that you have already import project project with the Egit plugin (2_Git with Eclipse).

In your Package explorer, you can now see the project project.

To import all the Maven modules, select the project project in the Package explorer and right click in a free area in this window.

Select import -> Existing Maven Project -> select all the module except the project module and finish.

Initialize project project
Before continuing, follow the instructions here:

4_Installing and configuring Tomcat

5_Mysql server configuration

On eclipse launch a maven build on project project  :

•Right click on the project/module you want to build
•Select Run As -> Maven Build...
•The following window appears :


Goals : clean install

Profiles : development integration

Select Skip Tests option.

Build project project


•Right click on the project/module you want to build
•Select Run As -> Maven Build...
•The following window appears :




In the field Goals, define the two following actions for project project :

?
Goals : clean install
•This command :
•builds the targets
•runs the unitary tests  (tests prefixed by TU)
•runs the code coverage with the cobertura maven plugin
_______________________________________________________________________________________________________________________________________________________________________________________________________

?
Goals : clean installProfiles : integration
•This command activates the integrationprofile defined in pom.xml files :
•builds the targets
•runs the unitary tests (tests prefixed by TU)
•runs the code coverage with the cobertura maven plugin
•deploys the war files on local Tomcat
•resets the mysql databases on localhost
•performs the integration tests (tests prefixed by TI)
_______________________________________________________________________________________________________________________________________________________________________________________________________

Important : development profile has to be always added in Profiles field when you are developping :

?
Goals : clean installProfiles : development
•This command activates the developmentprofile defined in pom.xml files :
•replaces the configuration files (log4j.poperties, web.xml...) with the appropriate configuration files  for development (log4j_developement.poperties, web-development.xml...)
•builds the targets
•runs the unitary tests (tests prefixed by TU)
•runs the code coverage with the cobertura maven plugin
_______________________________________________________________________________________________________________________________________________________________________________________________________----

Important : you can activate two (or more) profiles in a build, e.g. :

?
Goals : clean installProfiles : development integration
•This command activates the development and the integrationprofiles defined in pom.xml files :
•replaces the configuration files (log4j.poperties, web.xml...) with the appropriate configuration files  for development (log4j_developement.poperties, web-development.xml...)
•builds the targets
•runs the unitary tests (tests prefixed by TU)
•runs the code coverage with the cobertura maven plugin
•deploys the war files on local Tomcat
•resets the mysql databases on localhost
•performs the integration tests (tests prefixed by TI)
_______________________________________________________________________________________________________________________________________________________________________________________________________----

?
Goals : clean installProfiles : development integration soapui
•This command activates the development and the integrationprofiles defined in pom.xml files :
•replaces the configuration files (log4j.poperties, web.xml...) with the appropriate configuration files  for development (log4j_developement.poperties, web-development.xml...)
•builds the targets
•runs the unitary tests (tests prefixed by TU)
•runs the code coverage with the cobertura maven plugin
•deploys the war files on local Tomcat
•resets the mysql databases on localhost
•performs the integration tests (tests prefixed by TI)
•performs the soapui tests
_______________________________________________________________________________________________________________________________________________________________________________________________________     

       Be careful : use the next profile only for PRODUCTION build !

?
Goals : clean installProfiles : production
•This command activates the productionprofile defined in pom.xml files :
•replaces the configuration files (log4j.poperties, web.xml...) with the appropriate configuration files for production (log4j_developement.poperties, web-development.xml...)
•builds the targets
•runs the unitary tests (tests prefixed by TU)
•runs the code coverage with the cobertura maven plugin


Build lifecycle

Lifecycles phases are described in the following table. Bold phases are the most used.

Lifecycle Phase
Description

validate
Validate the project is correct and all necessary
information is available to complete a build

generate-sources
Generate any source code for inclusion in
compilation

process-sources
Process the source code, for example to filter any
values

generate-resources
Generate resources for inclusion in the package

process-resources
Copy and process the resources into the destination
directory, ready for packaging

compile
Compile the source code of the project

process-classes
Post-process the generated files from compilation,
for example to do bytecode enhancement on Java
classes

generate-test-sources
Generate any test source code for inclusion in
compilation

process-test-sources
Process the test source code, for example to filter
any values

generate-test-resources
Create resources for testing

process-test-resources
Copy and process the resources into the test
destination directory

test-compile
Compile the test source code into the test
destination directory

test
Run tests using a suitable unit testing framework. These
tests should not require the code be packaged or deployed

prepare-package
Perform any operations necessary to prepare a
package before the actual packaging. This often
results in an unpacked, processed version of the
package (coming in Maven 2.1+)

package
Take the compiled code and package it in its
distributable format, such as a JAR, WAR, or EAR

pre-integration-test
Perform actions required before integration tests are
executed. This may involve things such as setting
up the required environment

integration-test
Process and deploy the package if necessary into an
environment where integration tests can be run

post-integration-test
Perform actions required after integration tests have
been executed. This may include cleaning up the
environment

verify
Run any checks to verify the package is valid and
meets quality criteria

install
Install the package into the local repository, for use
as a dependency in other projects locally

deploy
Copies the final package to the remote repository
for sharing with other developers and projects
(usually only relevant during a formal release)

Some useful Maven command lines
At the top of your Maven project :

•Clean

?mvn clean
•See the dependencies tree

?mvn dependency:tree
•Compile

?mvn compile
•Compile and Test

?mvn test
•Compile, Test and generate target


?
mvn install


•Install and skip tests

?mvn install -Dmaven.test.skip=true
•Install and see if the dependencies are up to date


?
mvn install org.codehaus.mojo:versions-maven-plugin:1.2:display-dependency-updates


•Help


?
mvn help




M2eclipse
The goal of the Eclipse m2eclipse project is to provide Apache Maven support in the Eclipse IDE, making it easier to edit Maven's pom.xml, run a build from the IDE and much more.

Features
•Creating and importing Maven projects
•Dependency management and integration with the Eclipse classpath
•Automatic dependency downloads and updates
•Artifact Javadoc and source resolution
•Creating projects with Maven Archetypes
•Browsing and searching remote Maven repositories
•POM management with automatic update to dependency list
•Materializing a project from a Maven POM
•Checking out a Maven project from several SCM repositories
•Adapting nested multi-module Maven projects to the Eclipse IDE
•Integration with Web Tools Project ( WTP)
•Integration with AspectJ Development Tools ( AJDT)
•Integration with Subclipse, a Subversion Plugin for Eclipse
•Integration with Mylyn, a plugin that provides task-oriented context and integration with tools like JIRA and other issue/task-management systems.
Usage
M2eclipse usage is described in http://www.theserverside.com/news/1363817/Introduction-to-m2eclipse.

Build
•Right click on the project/module you want to build
•Select Run As -> Maven Build...
•The following window appears :


•In field goals, you define the action to perform, for example :
•clean : clean the project/module
•compile : compile
•test : compile and test
•install : compile, test and generate target
•deploy : compile, test, generate target and deploy the .jar .war on the nexus
•Ones you have created a build configuration, it is saved and you can reuse it by selecting Run Configurations

你可能感兴趣的:(maven)