Maven入门指南

第1章        Introduction

1.1   What’smaven?

At first glance Maven can appear to be many things, but ina nutshell Maven is an attempt toapply patterns to a project's build infrastructure in order to promotecomprehension and productivity by providing a clear path in the use of bestpractices. Maven is essentially a project management and comprehension tooland as such provides a way to help with managing:

  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution

Maven's primary goal is to allow a developer to comprehend the completestate of a development effort in the shortest period of time. In order toattain this goal there are several areas of concern that Maven attempts to dealwith:

  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features

1.2   Download

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

1.3   MavenPhases

Although hardly a comprehensive list, these are the mostcommon default lifecycle phases executed.

  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • 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: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

There aretwo other Maven lifecycles of note beyond the default list above. They are

  • clean: cleans up artifacts created by prior builds
  • site: generates site documentation for this project

Phases areactually mapped to underlying goals. The specific goals executed per phase isdependant upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war is the project type is - you guessedit - a WAR.

Aninteresting thing to note is that phases and goals may be executed in sequence.

mvn clean dependency:copy-dependencies package

Thiscommand will clean the project, copy dependencies, and package the project(executing all phases up to package,of course).

第2章        Configuring Maven

2.1   Configuringa proxy

Update setting.xmlin  ${user.home}/.m2/settings.xml or ${maven.installation.home}/settings.xml,and then set the proxy as follow:

<settings>

  .

  .

 <proxies>

  <proxy>

     <active>true</active>

     <protocol>http</protocol>

     <host>proxy.somewhere.com</host>

      <port>8080</port>

     <username>proxyuser</username>

      <password>somepassword</password>

     <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>

   </proxy>

 </proxies>

  .

  .

</settings>

 

第3章        Usage Guide

3.1   Check theversion of Maven

The command:

mvn –version

It would turns out like this:

Apache Maven 2.2.1 (r801777; 2009-08-0703:16:01+0800)

Java version: 1.6.0_11

Java home: C:\ProgramFiles\Java\jdk1.6.0_11\jre

Default locale: en_US, platform encoding:Cp1252

OS name: "windows vista" version:"6.0" arch: "x86" Family: "windows"

 

3.2   Creatinga new project

3.2.1 Command

mvn archetype:generate \

 -DgroupId=com.test.management.quality \

 -DartifactId=quality-visualization \

 -DarchetypeArtifactId=maven-archetype-quickstart\

 -DinteractiveMode=false

3.2.2 pom.xml

Once the command is executed, you willnotice that a directory named quality-visualizationhas been created for the new project, and this directorycontains a file named pom.xmlthat should look like this:

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 

 <groupId>com.test.management.quality</groupId>

 <artifactId>quality-visualization</artifactId>

 <version>1.0-SNAPSHOT</version>

 <packaging>jar</packaging>

 

  <name>quality-visualization</name>

 <url>http://maven.apache.org</url>

 

  <properties>

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  </properties>

 

  <dependencies>

    <dependency>

     <groupId>junit</groupId>

      <artifactId>junit</artifactId>

     <version>3.8.1</version>

     <scope>test</scope>

    </dependency>

  </dependencies>

</project>

3.2.3 Project Structure

After the archetype generation of yourfirst project you will also notice that the following directory structure hasbeen created:

 

3.3   Compilingapplication source

Change to the directory where pom.xml is created andexecute the following command to compile your application sources:

mvn compile

 

3.4   Compilingtest source and running unit test

Compile the testing source code and then run unit test:

mvn test

If you simply want to compile your test sources (but not execute thetests), you can execute the following:

 mvn test-compile

 

 

3.5   Making aJAR file

Making a JAR file by executing thefollowing command:

mvn package

If you take a look at the POM for yourproject you will notice the packaging element isset to jar. This is how Maven knows to produce a JAR file from theabove command (we'll talk more about this later). You can now take a look inthe ${basedir}/target directoryand you will see the generated JAR file.

3.6  Installing the artifact

To install the artifact you've generated (the JAR file) inyour local repository (~/.m2/repository is the default location) by the followingcommand:

mvn install

 

3.7  Cleaning the project

To remove target directory with all the build data:

mvn clean

After this command is executed, only src and pom.xml isleft.

3.8  Generating the eclipse project

To generate the eclipse project files from your POM you execute thefollowing command:

mvn eclipse:eclipse

 

后记:新项目使用maven取代ant+ivy,这是在学习的过程中从maven官方网站所摘录的介绍和常用命令。

 

      Written bysmilings in GuangZhou,Apile 1th, 2012

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值