Maven 五分钟入门(中英文)

中文原文网址:http://blog.csdn.net/lqqldj/archive/2007/08/03/1723978.aspx

英文原文网址:http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Maven 五分钟入门
--- 本文翻译自Maven官网的Maven in 5 Minutes,稍有删改,所有版权归maven所有。本文只作学习交流之用。
安装
Maven 是一个java工具,因此,在继续之前你必须安装好java(即本机要安装好jre )。
之后,把系统变量 M2_HOME 和变量值 maven 安装目录/bin设置到你的系统的环境变量中。然后在系统控制台(windows cmd)或终端(linux)下敲入 mvn –version,如果你安装成功,控制台将打印出你安装的maven的版本号,如:
C:Documents and SettingsAdministrator>mvn -version
Maven version: 2.0.6
C:Documents and SettingsAdministrator>  
根据你的网络设置,你可能需要进行一些额外的配置,如果有需必要请查阅 如何配置Maven .
 
创建项目
在命令行中执行以下(Maven 把它叫作goal)命令:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app
如果你是首次运行该(goal)命令,maven将要花一些时间去把最新的工具包(Maven 把它叫作artifacts)下载到你的本地仓库(什么是本地仓库?先放着,稍后再作介绍)。你也许要执行很多次上面的命令才能成功,因为远程服务器有时可能连接不上或者超时(这种情况很少见,除非是你本地网络没有配置好)。
命令执行完后你将看到maven生成了一个名为my-app的目录,这个名字就是你在命令中指定的artifactId,进入该目录,你将发现以下标准的项目结构:
D:MY-APP
│ pom.xml
└─src
    ├─main
    │ └─java
    │      └─com
    │          └─mycompany
    │              └─app
    │                      App.java
   
    └─test
        └─java
             └─com
                └─mycompany
                    └─app
                            AppTest.java
 
其中,src/main/java 目录包含了项目的源代码,src/test/java 目录包含了项目的测试代码,pom.xml是项目的项目对象模型(Project Object Model or POM)。
 
POM
pom.xml 文件是maven对一个项目的核心配置,这个文件将包含你希望如何构建项目的大多数配置信息。POM大而复杂,但你不用全部去了解,只要使用一些常用的配置就可以了。下面列出这个POM的内容:
<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.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.app</groupId>
 <artifactId>my-app</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>my-app</name>
 <url>http://maven.apache.org</url>
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 </dependencies>
</project>
 
我刚刚做了什么?
 
你执行了Maven 命令(goal)archetype:create,并且设置了一些参数在这个命令中,前缀 archetype 是包含create命令的一个maven的plugin,如果你熟悉ant的话,你可能做过类似的事情。这个goal命令根据项目原型(符合maven标准的项目模板)建立了一个简单的项目。现在可以确定地说,一个maven 插件就是一些有着相同目的的goals命令的集合,例如 jboss-maven-plugin 这个插件,就是为了处理各种和jboss相关的任务。
 
Build 项目
 
mvn package
这时命令行将会打印出各种动作,并且以下面一段信息结束:
 ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Oct 05 21:16:04 CDT 2006
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
与首次执行的命令不同(archetype:create),你也可以注意到了,这次执行的只是一个简单的命令---package。不同于goal,这是一个phase(阶段),一个phase(阶段)是指构建生命周期的一个阶段,构建生命周期是指有序的一系列phase(阶段)。当给出一个phase(阶段),Maven将执行所有的在此阶段前的phase及其自身,例如,如果我们执行compile阶段,实际上执行的阶段有:
validate
generate-sources
process-sources
generate-resources
process-resources
compile
你可以使用以下的命令来测试新编译和打包出来的jar包,
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
这时将打印出最经典的:
Hello World!
 
运行Maven工具
 
Maven phases (阶段)
虽然很难列出一张非常全面的表,但在此可先列出最普通的默认的生命周期阶段:
validate ,验证工程是否正确,所有需要的资源是否可用。
compile ,编译项目的源代码。  
test: 使用合适的单元测试框架来测试已编译的源代码。这些测试不需要已打包和布署。
Package: 把已编译的代码打包成可发布的格式,比如jar。
integration-test: 如有需要,将包处理和发布到一个能够进行集成测试的环境。
verify: 运行所有检查,验证包是否有效且达到质量标准。
install: 把包安装在本地的repository中,可以被其他工程作为依赖来使用。
Deploy: 在集成或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享。
除以上介绍的默认阶段,还有两个重要的Maven 的生命周期。它们是:
clean: 清除先前构建的artifacts(这个词用中文还真不好翻译,在maven中,把由项目生成的包都叫作artifact)。
site: 为项目生成文档站点。
 
Phases (阶段)实际上对应着潜在的goals,特殊的每个阶段执行的特殊的goals由项目的类型所决定,例如:如果项目的类型是jar,package阶段将默认执行jar:jar的goals,如果项目类型是war,那么package阶段执行的goals将是war:war。
有件有趣的事情需要注意,那就是phases和goals需要按一定的顺序来执行。
mvn clean dependency:copy-dependencies package
这个命令将先清除项目,然后拷贝依赖,最后把项目打包,当然,在打包之前,会先执行此阶段之前的阶段.如compile,test等.
生成站点
mvn site
这个阶段生成基于pom.xml配置的项目信息。你可以在target/site目录下看到生成的文档。
总结
我们希望这编快速入门能激起你对Maven的兴趣。注意这仅仅是一编浓缩了的快速入门指南。需了解Maven更多更全的特性,请查阅Maven入门指南
 

 


Maven in 5 Minutes

Installation

Maven is a Java tool, so you must have Java installed in order to proceed

First, download Maven and unzip it to your desired installation directory, for example C:maven in windows, or /usr/local/maven in Linux. After this, add the system variable M2_HOME as well as the $M2_HOME/bin directory to your system path. Type the following in a terminal or command prompt:

mvn --version

It should print out your installed version of Maven, for example:

Maven version: 2.0.4

Depending upon your network setup, you may require extra configuration. Check out the Guide to Configuring Maven if necessary.

Creating a Project

On your command line, execute the following maven goal:

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app

If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete. Don't worry, there are ways to fix that.

You will notice that the create goal created a directory with the same name given as the artifactId. Change into that directory.

cd my-app

Under this directory you will notice the following standard project structure .

my-app |-- pom.xml `-- src     |-- main     |   `-- java     |       `-- com     |           `-- mycompany     |               `-- app     |                   `-- App.java     `-- test         `-- java             `-- com                 `-- mycompany                     `-- app                         `-- AppTest.java

The src/main/java directory contains the project source code, the src/test/java directory contains the test source, and the pom.xml is the project's Project Object Model, or POM.

The POM

The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively. This project's POM is:

<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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.mycompany.app</groupId>   <artifactId>my-app</artifactId>   <packaging>jar</packaging>   <version>1.0-SNAPSHOT</version>   <name>Maven Quick Start Archetype</name>   <url>http://maven.apache.org</url>   <dependencies>     <dependency>       <groupId>junit</groupId>       <artifactId>junit</artifactId>       <version>3.8.1</version>       <scope>test</scope>     </dependency>   </dependencies> </project>
What did I just do?

You executed the Maven goal archetype:create , and passed in various parameters to that goal. The prefix archetype is the plugin that contains the goal. If you are familiar with Ant , you may concieve of this as similar to a task. This goal created a simple project based upon an archetype. Suffice it to say for now that a plugin is a collection of goals with a general common purpose. For example the jboss-maven-plugin, whose purpose is "deal with various jboss items".

Build the Project
mvn package

The command line will print out various actions, and end with the following:

... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2 seconds [INFO] Finished at: Thu Oct 05 21:16:04 CDT 2006 [INFO] Final Memory: 3M/6M [INFO] ------------------------------------------------------------------------

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:

  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile

You may test the newly compiled and packaged JAR with the following command:

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

Which will print the quintessential:

Hello World!

Running Maven Tools

Maven Phases

Although hardly a comprehensive list, these are the most common 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 are two 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 are actually mapped to underlying goals. The specific goals executed per phase is dependant 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 guessed it - a WAR.

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

mvn clean dependency:copy-dependencies package

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

Generating the Site
mvn site

This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site .

Conclusion

We hope this quick overview has piqued your interest in the versitility of Maven. Note that this is a very truncated quick-start guide. Now you are ready for more comprehensive details concerning the actions you have just performed. Check out the Maven Getting Started Guide .

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值