一 关于Maven
它是一个命令行构建工具.本身配置比较简单.主要是几个环境变量的设置.
在${Maven.home}/bin/下有个批处理文件maven.bat,其中:
@REM ----------------------------------------------------------------------------
@REM JAVA_HOME - 一个JDK的主目录.
@REM MAVEN_HOME - Maven安装主目录位置.
@REM 可选的环境变量
@REM MAVEN_HOME_LOCAL - 覆盖Maven工作所产生的文件的缺省路径
@REM MAVEN_BATCH_ECHO - 设为'on'使能批处理命令的反馈
@REM MAVEN_BATCH_PAUSE - 设为'on',在结束前等待一个键的按下.
@REM MAVEN_OPTS - Maven运行时要传给Java VM的参数.例如,在调试Maven本身时,使用 set MAVEN_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@REM ----------------------------------------------------------------------------
这个批提供了调用外部文件的设置:
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
为了修改一些配置和加入我们自己的配置项,我们使用mavenrc_pre.bat,其内容为:
把MAVEN_HOME_LOCAL指向i:/maven_plugin,可以使Maven在构建项目时所下载的插件放在这个目录中,而不是缺省的%HOME%\.maven中了.
set MAVEN_HOME_LOCAL=i:/maven_plugin
set MAVEN_BATCH_ECHO=off
set MAVEN_BATCH_PAUSE=off
由于%HOME%是用户的主目录,不想把这两个批处理那里,而想和Maven放在一起,则修改上面%HOME%为"%MAVEN_HOME%:
if exist "%MAVEN_HOME%\mavenrc_pre.bat" call "%MAVEN_HOME%\mavenrc_pre.bat"
if exist "%MAVEN_HOME%\mavenrc_post.bat" call "%MAVEN_HOME%\mavenrc_post.bat"
此外,MAVEN_HOME_LOCAL还有其它的修改方式,那就是在build.properties中指定,其中的有关项为:
maven.home.local Maven用于写一些用户相关的细节所到的本机目录,例如,未打包的插件和缓存数据,缺省为${user.home}/.maven
maven.plugin.dir Maven能找到它自己的插件的地方,缺省为${maven.home}/plugins
maven.plugin.user.dir Maven只为特定用户查找的插件位置,缺省为${maven.home.local}/plugins
maven.plugin.unpacked.dir 处理用的为打包插件的位置,缺省为 ${maven.home.local}/cache
maven.repo.local 本机上的repository,Maven用来保存下载的制品,如(jars etc),缺省为${maven.home.local}/repository
二 开始用Maven
1 准备构建配置文件build.properties,其内容为:
maven.appserver.home =D:/jakarta-tomcat-
turbine.app.name=your appname
#如果不想生成演示页面,则设为false
turbine.app.setup.demo=true
2 运行maven turbine:setup,生成项目.也可以带有参数,如
maven -Dturbine.app.name=helloworld -Dmaven.appserver.home=D:/jakarta-tomcat-
如果命令行中有这两个参数,则不需要第一步的工作,因为对于maven来说,这两个参数是必须要有的!
其中:maven.appserver.home用于在发布应用时,把打包的部署文件发布到该项指定的目录中的容器环境.
turbine.app.name则是你的项目名称.
在运行了这个命令后,当前目录下会产生以你指定的your appname为名字的目录,在进入这个目录后,我们可以进行第三步了.
3 运行maven turbine:deploy.
它根据2步中生成的setup.properties,project.properties,project.xml来生成可供发布的制品.项目中使用到的库,都定义在project.xml中,可以根据自己的项目需要裁剪这个文件. 经过裁剪后,运行这个任务产生发布.
为了解决执行过程中出现的问题,先看看project.xml中的主要项:
<dependency>
<groupId>turbine</groupId>
<artifactId>turbine</artifactId>
<version>
<type>jar</type>
<properties>
<war.bundle>true</war.bundle>
</properties>
</dependency>
dependency元素定义了项目中依赖的库,该库位于${maven.home.local}/repository:
<groupId>\jars\<artifactId>-<version>.<type>
turbine\jars\ turbine -
上面展示了插件文件和元素dependency中子元素的对应关系.
如果无法下载所需插件,或自己下载,或使用本机上有的,然后可以按照这种方式创建一个符合项目要求的插件来供它使用.
原作者:Rob Herbst
当您的项目逐渐变得庞大和复杂时,最好使用一种构建工具来自动构建您的项目。例如,一个典型的java项目,每次构建时都要经历编译java源代码,把class文件打成.jar包,生成javadocs文档等步骤。这些步骤都可以用构建工具帮您自动完成。说到构建工具,大家肯定都知道make,但make是依赖具体操作系统的。Java-centric选择了Ant,一种可以跨平台的使用xml来替换Makefile糟糕语法的构建工具。
来自Apache软件组织的构建工具Maven更可能成为您的选择,Maven不仅提供了out-of-the-box的解决方案来统一处理构建相关的任务,还提供了信息统计的功能。使您的开发团队可以更好地跟踪项目的进展情况。
Maven的原理
作为构建工具,Maven和Ant一样,利用构建配置文件进行编译,打包,测试等操作。您可以用Maven自带的功能进行任何的操作,但前提是做好了相应的配置。当然,修改已有的模板来开始新的项目是个好方法。除非您在写特有的task,不然都会有target重用的问题。
Maven进行了一些改进。您将项目配置内容写成XML文件,并且可以使用很多Maven自带的功能。另外还可以在Maven项目中调用任何Ant的task。
Maven自带的"goals"有以下功能:
编译源代码
产生Javadoc文档
运行unit测试
源代码文法分析
产生违反团队编码规范的详细报告
产生CVS最新提交报告
产生CVS更改最频繁的文件报告和提交最频繁的开发人员报告
产生可以交叉引用的HTML格式的源代码,等等。
Maven的功能是通过众多的插件来实现的。在Maven的网站上可以找到一个Maven插件列表。例如,创建EAR包和控制J2EE应用服务器的插件。
Maven的另外一个特色是利用中央构建资源库去访问您项目中需要的.jars(像Perl's CPAN)。您只用列出需要的.jars及相应版本,Maven会自动下载。当第一次运行Maven,或者运行一个新的goal时,可以在控制台上看到中央构建资源库下载.jars的详细过程。这一功能不仅使Maven更加容易使用,而且能帮您省下不少搜集维护本地或远程构建资源库中.jars版本的时间和精力。
Maven的易用性来源于配置过程的自然性。Out of the box,不仅提供了项目构建的基本步骤,还可以自定义项目对象模型(POM)。POM基于易于编辑的XML格式,由不同元素的名称和属性组成。一旦您完成POM的配置,就可以在构造过程中使用很多Maven自带的激动人心的功能。
Maven也易于自定义,特别是当您已经熟悉Ant后。您可以增加新的功能或钩子到已有的构造过程以添加自定义的步骤。通过修改maven.xml文件可以添加自定义的功能。
Maven概述
Maven最基本的运行单位是goal。例如,当您在控制台运行如下命令:
| maven site:generate |
Maven会去访问并执行generate goal of site插件。如果想要查看配置文件中所有的goals,输入:
| maven -g |
对于任何一个团队,运行maven site:generate的goal生成的站点可以给团队成员展示项目当前的状态。第一页是单元测试的报告,提供单元测试的技术统计结果。
Checkstyle插件对于在团队中坚持编码规范很有帮组。产生的报告中列出了在多少个文件里面出现了多少个违反编码规范的统计信息,并列出了相应文件和行号。通过点击相应链接,可以轻易的找到代码中违反编码规范的地方。
事实上,Maven的开发者也在自己的项目中使用Maven来管理,所以你可以看到在开发Maven过程中产生的Checkstyle报告,如图一

图一. Checkstyle报告摘要
在摘要之后就是按照文件为单位组织的违反编码规范的报告,如图二。

图二. Checkstyle详细报告
CVS插件可以产生关于代码提交的统计报告,包括以下内容:
更改日志报告:列出最新提交的文件及提交日期,作者和注释。
文件活跃度报告:提供更改最频繁文件的详细内容
开发人员活跃度报告:提供最新提交文件的统计报告及每个开发人员提交的文件数。
以上报告文档同样使用在Maven开发项目中,最新的一次更改日志报告如图三。

图三. 更改日志报告
试一试
如果您已经准备好要使用Maven,下面是安装的步骤.
到Apache下载Maven的最新版本.
解压到系统的某个目录,比如C:\maven或者~/maven.
设置MAVEN_HOME环境变量指向解压到的目录.
确认您已经配置好JAVA_HOME环境变量.
在系统path环境变量中添加MAVEN_HOME/bin.
现在,在控制台输入
| maven -v |
您可以看到下面的输出:
__ __
| \/ |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \ ~ intelligent projects ~
|_| |_\__,_|\_/\___|_||_| v. 1.0-beta-10
Maven可以工作了!想看所有可用的Maven选项,输入:
| maven -h |
所有可用的goals:
| maven -g |
您将看到可用的插件列表及其提供的goals:
Available [Plugins] / Goals
```````````````````````````
[ant] : Generate an Ant build file
generate-build ............. Generate an Ant
build file
[antlr] ( NO DEFAULT GOAL )
generate ................... Generate source
from antlr grammars
prepare-filesystem ......... Make any necessary
directories for antlr
processing
再往下,可以看到[wizard]插件和其goal ui。想运行那个goal就输入:
| maven wizard:ui |
您可以在控制台上看到Maven自动到中央构建资源库或网上下载需要的.jar包.
我们在进一步,创建一个简单的项目。例如,创建一个名为maven-sample的目录(~/maven-sample或者C:\maven-sample)。运行如下命令:
| maven -D package=com.oreillynet.mavensample genapp |
maven会自动创建一个标准的项目文件。上面的package属性是必须的,如果输入错误或不输会编译错误。
现在,运行命令:
| maven site:generate |
Maven会依据配置好的POM文件(project.xml)为您项目生成相应的站点。如果是第一次运行,还可以看到Maven会到中央构建资源库或网上下载必须的.jars包。
运行这个goal,Maven会为您编译源代码,拷贝conf目录里的资源文件到类文件classes的目录,生成Javadoc文档以及项目分析报告。可以看到,一个目标含有很多的功能!
进入target目录,您可以看到site:generate goal生成的文件。target目录下的docs目录存放着Maven为您的项目产生的报告文档。
Maven中使用Ant任务
要整合Ant任务到Maven中,您需要创建名为maven.xml的文件并存放到project.xml文件的目录。在maven.xml文件中写入:
| <project default="myGoal"> <goal name="myGoal"> <echo>Hello Maven!</echo> </goal> </project> |
其中
| <echo> |
是Ant中用于输出的task。同样,在自定义的goals中也可以运行任何Ant task。
保存maven.xml文件,在project.xml和maven.xml所在的目录中运行
| maven |
您可以看到以下输出:
myGoal:
[echo] Hello Maven!
BUILD SUCCESSFUL
Total time: 1 seconds
自定义Maven
到目前为止,您已经看到一个简单的项目是怎样运行的,您还可以按需要自定义Maven。在您最喜欢文本编辑器中打开project.xml文件,可以看到带有注释的项目文件。您可以把这作为扩展的基础,根据项目的需要添加需要的代码和资源。
如果要使用Checkstyle插件,需要在project.properties文件中指定使用的编码规范。默认使用的是标准的Sun和Turbine的编码规范。您也可以通过设置
| maven.checkstyle.properties=team-checkstyle.xml |
来使用团队自己的编码规范。
Checksytle插件会使用team-checkstyle.xml文件作为编码规范检查的依据。要创建自己的编码规范文件,可以使用Eclipse插件,或者修改标准的编码规范。解压MAVEN_HOME/plugins目录下的maven-checkstyle-plugin.jar文件,在解压目录中的plugin-resources目录中可以找到sun_checks.xml和tuibine_checks.xml两个文件。选择和您团队最接近的编码规范再根据需要修改一下就可以了。
要更改项目产生的站点的默认颜色,可以在project.properites文件中修改。颜色属性要用标准的HTML语法。
| maven.ui.section.background=#cfcfbb maven.ui.banner.border.top=#cfcfbb maven.ui.banner.background=#cfcfbb maven.ui.source.background=#cfcfbb |
如果您想定义在ibiblio.org的Maven构建资源库中没有的公司自己的项目相关性,您可以照下面定义
| <dependency> <id>SomeJar</id> <jar>someJar1.2.jar</jar> </dependency> |
然后,您需要将somJar1.2.jar文件放到您自己的中央构建资源库目录中。该目录由project.properties文件中的maven.repo.local属性指定。默认为${maven.home.local}/repository或${user.home}/.maven。在project.xml文件中添加下列<reports>元素还可以自定义生成的报告文档
| <reports> <report>maven-checkstyle-plugin</report> <report>maven-changelog-plugin</report> <report>maven-developer-activity-plugin</report> <report>maven-file-activity-plugin</report> <report>maven-javadoc-plugin</report> <report>maven-jdepend-plugin</report> <report>maven-junit-report-plugin</report> <report>maven-jxr-plugin</report> <report>maven-statcvs-plugin</report> <report>maven-simian-plugin</report> </reports> |
除了最后两个,其他都是默认配置的。maven-statcvs-plugin产生CVS统计报告,maven-simian-plugin可以识辨程序里重复的代码。一旦您自己定义了一个
| <reports> |
元素,将会覆盖掉所有默认文档配置。
总结
本文介绍了Maven的基本功能。用Maven来管理您的项目可以改善团队开发过程并可以让您更好的了解当前项目的情况。
在此感谢Maven的开发者及开发社区的朋友,是他们创造出了这么卓越的构建工具!
原文地址:http://www.theserverside.com/articles/article.tss?l=MavenMagic
本文的翻译已经原作者同意,转载请保持原文。如有问题和意见可以和原作者或我联系。
Part 2 : http://www.blogjava.net/jinfeng_wang/archive/2005/03/14/2074.html
Part 3 : http://www.blogjava.net/jinfeng_wang/archive/2005/03/15/2089.html
Introduction(简介)
Maven is a high-level, intelligent project management, build and deployment tool from the Apache project. There is nothing that Maven does that Ant cannot do. Ant gives the ultimate power and flexibility in build and deployment to the developer. Why do you need Maven then? Maven adds a layer of abstraction above Ant (and uses Jelly). Maven can be used to build any Java application, but in this article we will investigate the applicability of Maven from a J2EE standpoint. J2EE build and deployment as we know it today is pretty much standardized. Every enterprise has some variations, but in general it is all the same: deploying EARs, WARs, and EJB-JARs. Maven captures this intelligence and lets you achieve the build and deployment in about 5-6 lines of Maven script compared to dozens of lines in an Ant build script. In other words, Maven simplifies a developer's life. Everybody loves things to be simple right?
Maven是Apache的高度智能的项目管理、构建、部署工具,它能做Ant所有能做的事情。Ant给开发者在构建部署过程中提供了极强的火力和灵活性,那么为何你还会选择Maven呢?Maven在Ant的基础上(利用Jelly)提供了一个中间抽象层。Maven可以用了build任何Java应用,但在这篇文章中我们只针对J2EE细谈Maven的适用性。正如我们已熟知的,目前J2EE的构建部署过程已经相当的标准化,虽然各企业应用之间可能仍存在着一定的区别,但总的来说它们的build、deploy过程几乎都是相同的:部署R、WAR、EJB JAR。Maven正是抓住了这其中的相同性,用户用5~6行的Maven脚本就完成build、deploy过程,而完成相同的任务Ant脚本则需要许多行。换句话说,Maven简化了开发者的工作,每个人都喜欢工作被简化,不是么?
If you think of Ant as a modular language like C, then Maven can be compared to an object oriented language like C++ or Java. Maven plugins are like the standard JDK libraries. The C language, albeit a giant leap from assembly languages, falls short of addressing the complexities of enterprise projects. Java on the other hand, being object oriented, forces the developer to program in a certain way and reduces complexities in large enterprise projects by letting you manage dependencies by breaking them into chunks via CRC - Class, Responsibility and Collaboration - which is a big paradigm shift from C. There are some tasks that C alone can do, but for the majority of enterprise programming Java will suffice.
如果把Ant比作模块化语言(例如C),那么就可以把Maven比作面向对象语言(例如C++、Java),Maven的插件(plug-in)则扮演着标准JDK库的角色。C语言虽然大大超越了汇编语言,但还是陷入了企业项目复杂性的泥潭。而作为面向对象语言的Java,强制开发者按照一定的思路进行开发,使用CRC(Class, Responsibility和Collaboration)将企业应用中的联系进行分解(这是由C转移来的新的编程范型),便于开发者掌握它们之间的依赖关系,减少大型企业项目中的复杂性。虽然某些任务仍然还是必须使用C才能完成,但对于绝大部分企业编程使用Java足够了。
Every comparison between C and Java listed above applies to Ant and Maven. Ant lets you do any variations you want, but requires a lot of scripting. Maven on the other hand mandates certain directories and file names, but it provides plugins to make life easier. The restriction imposed by Maven is that only one artifact is generated per project (A project in Maven terminology is a folder with a project.xml file in it). If you are thinking "My EAR artifact itself consists of multiple artifacts, I am out of luck, I cannot use Maven", it is time to take a closer look. A Maven project can have sub projects. Each sub project can build its own artifact. The topmost project can aggregate the artifacts into a larger one. This is synonymous to jars and WARs put together to form an EAR. Maven also provides inheritance in projects. I will address these topics in the following sections. At the end of this article you will be able to build J2EE project artifacts using Maven.
上面对于C和Java的比较完全可以适用于Ant和Maven,Ant让你可以做任何的事情,但是需要许多的脚本,而另一方面Maven会强制你使用一定的目录和文件名,但是Maven提供了许多的插件,让你的工作变得相当简单。Maven给项目所加的限制就是“每个项目(在Maven中一个项目是指带有project.xml的一个文件夹)只能生成一个制品(artifact)”。如果你觉得“我的EAR中包含多个制品,很不幸,我无法使用Maven”,那就请继续往后看吧。一个Maven项目可以由子项目,每个子项目可以build生成自己的制品,最顶层的项目可以将子项目的制品打包生成一个大的包。Maven在项目之间还提供了继承(inheritance),这将在随后讲述。在文章的最后,将会教你如何使用Maven构建J2EE项目。
Maven simplifies build enormously by imposing certain fixed file names and acceptable restrictions like one artifact per project.
Maven给项目加上了一定的限制,例如项目的文件名、每个项目只能生成一个制品等,大大简化了构建过程。
First there was make. But make was not cross platform. Then came Ant. Ant lets you do cross-platform builds in a systematic manner. It is very elegant compared to make. However as you may have already realized, build tasks in a project are pretty standard. The tricky part is partitioning, compiling and packaging classes, resources and descriptors into the right deployment artifacts like jars, WARs and EARs and managing dependencies on the right version of libraries - both internal and third party ones. After a while you will find yourself copying Ant script snippets from one place to another and modifying them. Then there is the universal problem of classpath. We all have different versions of different libraries installed in different locations on the development workstations. Nobody can be sure if the Ant build copied the right version of the library into the deployment artifact at all. We all have seen the classic developer shrug - "It works on my machine"! Ant does little to address this problem. Enter Maven.
首先是Make,但是Make不是跨平台的。随后产生了Ant,它运行你能够以一种更加系统化的方式进行跨平台build。与Make相比,Ant变得优雅多了,但正如你已经意识到的,在项目中的build是相当标准的过程。最好的方式就是将项目进行分解,编译,将class、资源和描述符打包成jar、WAR、EAR,管理各版本正确的库(包括内部库和第三方库)之间的依赖关系。你会发现自己经常的将Ant的脚本到处拷贝,经常的classpath问题也就随之出现了。在我们的开发平台中在许多地方安装了各种版本各异的库。你根本无法确定Ant在build、deploy制品时,拷贝的是否是版本正确的库。我们会经常看到开发者耸肩摇头的经典镜头,“它在我机器上是好的啊”!Ant对于此问题毫无办法,那就进入Maven的世界吧。
What is it that makes Maven an attractive option for today's enterprises? The basic tenet of Ant build scripts is copying, renaming and deleting files under various conditions into various deployable artifacts - which are again treated as files on your computer by the build script. Maven hides the fact that everything is a file and forces you to think and script to create a deployable artifact such as an EAR, that has a dependency on a particular version of a third party library residing in a shared remote (or local) enterprise repository, and then publish your library into the repository as well for others to use. No more classpath issues. No more mismatch in libraries. This sounds like a very real and practical solution to our everyday problems. It also gives you the power to embed Ant scripts within Maven scripts if absolutely essential.
那么是什么让Maven变成一个如此具有吸引力的选择呢?Ant脚本的基本原则就是在各种情况下拷贝、改名和删除文件,生成可部署制品,而Ant脚本又将这些制品看作是机器中的文件,再对它进行拷贝、改名和删除……,不断重复以上过程。Maven则隐藏了一切都是文件的事实,强制你按照特定的方式思考、编写脚本,创建依赖于特定版本的第三方库的可部署制品(例如EAR),而这些库则存放在共享远程(或本地)企业仓库(repository)中,然后将你自己开发的库发布到仓库中供别人使用,再也没有了classpath问题,再也不会出现库版本不一致的问题。Maven真实有效的解决了之前我们每天都面对的问题。此外,Maven也提供了在必要情况下将Ant脚本嵌入到Maven脚本中的功能。
In the words of Jason van Zyl, an important contributor to the Maven source code base, "the intent of Maven is to make intra-project development highly manageable in the hopes of providing more time for cross-project development. You might call it cross-project pollination or the sharing of project development knowledge; this is what Maven attempts to encourage".
用Jason van Zyl(Maven源代码的重要贡献者)的话说,“Maven的目的就是使项目内部开发变得高度可控,进一步的目标是能为跨项目的开发节余更多的时间。你可以称之为跨项目授粉,或者共享项目开发知识,而这恰是Maven所鼓励的。”
Maven installation (Maven安装)
You can download the 1.0 Release Candidate from http://maven.apache.org/builds/release/1.0-rc1/.
The version of Maven available at the time of this writing was 1.0-beta 10. Everything in this article refers to the 1.0-beta 10. It is feature complete for 1.0 pending any defects. Unzip the archive onto your machine. It creates a directory called maven-1.0-beta-10. Before you go any further, set the MAVEN_HOME environment variable to this directory. Also add the MAVEN_HOME/bin to the PATH environment variable. Make sure you have set the JAVA_HOME appropriately. Go to the command line and type maven -g. If you see a long list of output, your installation has succeeded.
你可以从http://maven.apache.org/builds/release/1.0-rc1/ 下载1.0 Release版。
在写这篇文章的时候,Maven的当前版本是1.0-beta 10。因此这篇文章中的所有内容都是指1.0-beta 10。此版本已提供了所有的特性、修补了所有缺陷。将压缩包在你机器解压,它将创建名为maven-1.0-beta-10的目录。在做其他之前,请创建环境变量MAVEN_HOME,并设值为此目录。然后将MAVEN_HOME/bin添加到环境变量Path中。确认你已经正确设置了JAVA_HOME。然后转到命令行方式下,键入“maven -g”命令。如果成功看到一系列的输出,那么就说明安装成功。
Maven basics (Maven基础)
The basic concept of Maven is a project. In Maven terms, any directory that has a project.xml in it is a project. When the sub-directories underneath have their own project.xml, they are projects on their own too. Another concept in Maven is that of a repository. The repository holds the artifacts on which your project depends. There are two kinds of repository: local and remote. Both of them can be declaratively set. Unless specified otherwise, the local repository is created in a special directory called ".maven/repository". In Windows, this directory is created in C:\Documents And Settings. For example, if your project depends on commons-logging version 1.0.2, you can specify the dependency in project.xml and when maven is executed, it will copy the appropriate commons-logging jar file from the remote repository to the local repository and then use it to build your project's artifact. The maven repository folder has subfolders for each library. For instance, there is a sub folder for commons-logging. Beneath the commons-logging folder there is another subfolder called jars. This jars folder has the commons logging jar files suffixed by version number. Jars are not the only type of artifacts supported in the repository. You can have EARs and WARs too.
Maven的基本概念就是Project,在Maven里面,每个目录包含有project.xml的目录都是一个Project。如果子目录中含有project.xml,那么他们自己就是project。在Maven中的另外一个概念就是仓库(repository)。仓库用于保存项目所使用的所有的制品。Maven有两种仓库:本地仓库和远程仓库。它们都可以用声明的方式进行配置。除非特别声明,本地仓库的位置一边在“.maven/repository”目录。在Windows中,该目录在C:\Documents And Settings。例如,如果你的项目使用了commons-logging的1.0.2版本,那么你可以在project.xml中进行声明,当运行maven命令是,它将会从远程仓库中拷贝正确的commons-logging的jar文件到本地仓库,然后构建你的项目,生成制品。Maven仓库为每个库创建生成一个子目录。例如,它将会为commons-logging创建一个子目录。在commons-logging文件夹中,会含有一个名为jars的子目录。在jars文件夹中,将会存放含有相应版本号为后缀的jar文件。在Maven仓库中,它所能够支持的制品不仅仅是jar,还有EAR和WAR等。
The role of the repository is immediately obvious. Instead of each project having its own copies of third party libraries, the repository helps developers across projects to share the libraries. Each project can also in turn generate its artifacts and publish it into the remote repository. The process of publishing a jar into the repository is called "install" in Maven lingo. This install process helps organizations to share internal artifacts across projects in a standard manner. This also holds the basis for continuous integration among inter-dependent projects. Continuous Integration is a concept that was developed by Martin Fowler and developers of Cruise Control. Individual projects can continue to build and publish the release and snapshot artifacts to corporate repositories. Daemon processes running on dedicated machines can schedule integration builds, deploy to execution platforms (application servers) and run automated tests to verify the build status. Figure 1 shows the role of project.xml, repository, goals and plugins in Maven build. The grey colored rectangles are provided by you. The green colored rectangles are provided by Maven. The pink colored rectangle shaded is the output - the deployment artifacts from your project. Custom plugins and maven.xml are optional. The rest of the inputs are mandatory.
仓库的角色是相当明显的,每个项目不再需要各自包含自己所依赖的第三方库,仓库将会帮助开发者在多个项目间共享库。反过来,每个项目也可以自己build制品,然后deploy到远程仓库中。按照Maven的术语,往仓库中发布jar的过程被称为“安装(install)”。安装过程可以帮助开发者用一种标准的方式在项目间共享内部制品。这也就是相互依赖的项目间持续集成(continuous integration)的基础。持续集成是由Martin Fowler提出的概念,他还开发了Cruise Control工具。项目可以连续的build,最新release或snapshot制品可连续的发布到公司仓库中。在专门机器运行的守护进程(daemon process)将会定时的持续构建、发布制品到应用服务器中、进行自动测试并检验build状态。图1展示了在Maven构建过程中project.xml、仓库、goal、plug-in各自的角色。灰色方框的内容将由你提供,绿色方框的内容则由Maven提供,粉红色方框的内容则用于输出―项目生成的部署制品。其中定制的plug-in和maven.xml则是可选的,而其他输入部分则必须的。
Figure 1 Overview of Maven build elements
图1 Maven构建元素总览
project.xml details (project.xml细述)
The project.xml is divided into four main parts namely, Project Management Section, Project Dependency Section, Project Build Section and Project Reports Section. Listing 1 shows the outline of a typical project.xml. The mandatory items are shown in bold.
整个project.xml被分为四个部分:项目管理部分、项目依赖部分、项目构建部分、项目报告部分。在表1中给出了典型project.xml的概要,其中必需项使用粗体显示。
Listing 1 Outline of project.xml
01 <?xml version="1.0"?>
02 <project>
03 <pomVersion>3pomVersion>
04 <groupId>Sample-Maven-ProjectgroupId>
05 <id>sample-projectid>
06 <currentVersion>1.1currentVersion>
07 <name>Sample Maven Projectname>
08
09 <!-- Project Management section goes here -->
10
11 <!-- Project Dependency section goes here -->
12
13 <!-- Project Build section goes here -->
14
15 <!-- Project Reports section goes here -->
16
17 project>
- Line 02 - Root element defining the project.
- Line 03 - Project Object Model (POM) Version. This tag is unused but needed.
- Line 04 - A directory with this name is created in Maven repository to hold the artifacts of projects sharing the group id.
- Line 05, 06 - The id and version is used to create the artifact name as
- .jar
- Line 07 - Name of the Project
表1 project.xml概要第2行:义项目的根元素。
01 <?xml version="1.0"?>
02 <project>
03 <pomVersion>3pomVersion>
04 <groupId>Sample-Maven-ProjectgroupId>
05 <id>sample-projectid>
06 <currentVersion>1.1currentVersion>
07 <name>Sample Maven Projectname>
08
09 <!-- 项目管理部分 -->
10
11 <!-- 项目依赖部分 -->
12
13 <!-- 项目构建部分 -->
14
15 <!-- 项目报告部分 -->
16
17 project>
- 第3行:Project Object Model (POM)版本,此tag虽然无用,但却是必需的。
- 第4行:在Maven仓库中将会创建一个用此命名的目录,所有相同groupId的项目生成的制品都存放于此目录。
- 第5,6行:id和version,用于做为生成制品的文件名,
- .jar。
- 第7行项目名。
The Project Management Section is shown in detail in Listing 2 and has general information on the organization, its web site, project web site, location of SCM, deployment and issue tracking site, developer list, mailing lists to name a few. Most of the items in this section is boilerplate and are optional. Maven allows inheritance in the project.xml. Every organization can have an enterprise wide template which can be extended for projects. Each subproject will simply fill in the relevant section of this file. Most of the project management section elements get defined in the enterprise wide template and top-level project template. The only mandatory element is the organization name.
在表2中详细列出项目管理部分的内容,它包含的信息有:开发组织、开发组织网站、项目站点,SCM地点、发布以及问题跟踪站点、开发者列表、邮件列表等。在项目管理部分的绝大部分item都可以作为模板使用,并且它们大都是可选的。Maven支持在project.xml中继承。每一个开发组织都可以拥有一个企业范围内的模板,供各项目使用。每一个子项目只需要填写相应部分project.xml中的部分内容即可,项目管理部分的信息已经在企业范围的模板或者在最顶层项目模板中进行了定义。这里开发组织名(organization name)是必需的。
Listing 2 Project Management Section in project.xmlLines 01-05 - Organization details
01 <organization>
02 <name>Foobar Travelsname>
03 <url>http://www.foobar.comurl>
04 <logo>http://www.foobar.com/logo.jpglogo>
05 organization>
06
07 <inceptionYear>2003inceptionYear>
08 <package>foobar.blah.*package>
09 <logo>http://www.foobar.com/project-logo.jpglogo> |
10 <description>Project description goes heredescription>
11 <shortDescription>Short DescriptionshortDescription>
12 <url>http://www.foobar.comurl>
13 <issueTrackingUrl>http://jira.foobar.comissueTrackingUrl>
14 <siteAddress>http://staging.foobar.comsiteAddress>
15 <siteDirectory>/etc/stagingsiteDirectory>
16 <distributionDirectory>/etc/buildsdistributionDirectory>
Maven simplifies build enormously by imposing certain fixed file names and acceptable restrictions like one artifact per project.
Maven给项目加上了一定的限制,例如项目的文件名、每个项目只能生成一个制品等,大大简化了构建过程。
Figure 1 Overview of Maven build elements
01 <?xml version="1.0"?>
02 <project>
03 <pomVersion>3pomVersion>
04 <groupId>Sample-Maven-ProjectgroupId>
05 <id>sample-projectid>
06 <currentVersion>1.1currentVersion>
07 <name>Sample Maven Projectname>
08
09 <!-- Project Management section goes here -->
10
11 <!-- Project Dependency section goes here -->
12
13 <!-- Project Build section goes here -->
14
15 <!-- Project Reports section goes here -->
16
17 project>
02 <project>
03 <pomVersion>3pomVersion>
04 <groupId>Sample-Maven-ProjectgroupId>
05 <id>sample-projectid>
06 <currentVersion>1.1currentVersion>
07 <name>Sample Maven Projectname>
08
09 <!-- 项目管理部分 -->
10
11 <!-- 项目依赖部分 -->
12
13 <!-- 项目构建部分 -->
14
15 <!-- 项目报告部分 -->
16
17 project>
01 <organization>
02 <name>Foobar Travelsname>
03 <url>http://www.foobar.comurl>
04 <logo>http://www.foobar.com/logo.jpglogo>
05 organization>
06
07 <inceptionYear>2003inceptionYear>
08 <package>foobar.blah.*package>
09 <logo>http://www.foobar.com/project-logo.jpglogo> |
10 <description>Project description goes heredescription>
11 <shortDescription>Short DescriptionshortDescription>
12 <url>http://www.foobar.comurl>
13 <issueTrackingUrl>http://jira.foobar.comissueTrackingUrl>
14 <siteAddress>http://staging.foobar.comsiteAddress>
15 <siteDirectory>/etc/stagingsiteDirectory>
16 <distributionDirectory>/etc/buildsdistributionDirectory>