在Eclipse中使用Maven 2.x指南
2014-11-27 23:57
337 查看
原文地址:http://blog.csdn.net/it_man/article/details/1346626
你也可以在eclipse内定义一个新的classpath变量:从菜单条,选择Window>Preferences,选择java>BuildPath>Classpath变量页。进入eclipse,导入workspace下面的Sample
Project。由于缺少MAVEN_REPO变量,Eclipse提示编译失败。在Eclipse的Preferences–Java-BuildPath-ClasspathVariables中添加名为MAVEN_REPO的变量,指向D:/maven/local/repository。
如果想让eclipse支持直接运行maven的goal,可以安装mavenide插件。
Tools,选择Program),你可以在位置字段引用刚才定义的maven_exec。进一步,引用project_log作为工作目录,指定所选的mavengoals作为参数,比如eclipse:eclipse。更多的信息请参考eclipse的帮助。
问题:如果插件可以产生变量和runner可能会好一点。
如果你已经使用eclipse创建或者检出了项目,你只需在workspace中刷新项目。否则,你必须将项目导入eclipse工作空间(从菜单条选择File>Import>ExistingProjectsintoWorkspace)。在后面的例子,项目(目录)不应在你的workspace,因为eclipse可能导致错误,特别是你将eclipse作为scm客户端时。
aflatmultiplemoduleprojectlayoutisthepossibilitytocheckout
andedittheparentPOMwithoutcheckingoutthewholeproject.Thefollowingsampleshowshowtohandlemavenmultiplemoduleprojectswitheclipsewhilekeepingtherecommended
hierachicalprojectlayout.
假设eclipse是你的SCM客户端,这个例子将告诉你如何设置一个新的多模块项目。
1.设置一个新的叫做step-by-step的workspace,并且象上面所述增加M2_REPO类路径。
2.打开命令行shell,并且切换到新建的工作空间(workspace)目录。
3.从命令行使用archetype插件新建一个maven项目
4.在step-by-step工作空间,使用eclipse创建一个新的简单项目guide-ide-eclipse(从菜单条,选择File>New>Project,选择Simple>Project)。Eclipse会为你的guide-ide-eclipse项目创建一个简单的.project文件,你应该能够看到一个pom.xml文件。
5.删除src文件夹,并且打开pom.xml文件,将父项目的packaging改为pom。
问题:mvneclipse:eclipse也许应该为pompackaging类型产生一个简单的.project文件。
6.从命令行切换到guide-ide-eclipse项目路径,并且创建一些模块:
7.将新建的模块添加到父pom中。
8.在新建的模块的POMs中添加parent。
9.添加模块间依赖:
10.把项目安装到本地仓库,并且产生eclipse文件:
11.使用eclipse的团队开发支持(从context菜单选择Team>Share
Project)检入项目。记住,不要检入产生的eclipse文件。如果使用CVS,你因该在每个模块有一个.cvsignore文件,内容如下:
即使是父项目,也应该有.cvsignore文件。当你从repository检出项目时,Eclipse自动生成新的简单的.project文件。
问题:插件应该可选的产生.cvsignore文件。
现在,你有不同的处理选项。如果你同时工作于所有模块,你应该具有eclipse的项目依赖,而不是二进制依赖,你可以设置一个新的workspace,并且从step-by-step/guide-ide-eclipse中导入。记住,你必须先删除父项目的.project文件。结果和从命令行检出整个项目相同,运行mvneclipse:eclipse并且最后把项目导入到你的eclipse工作空间。两种方式都可以使用eclipse同步你对项目的修改。
对于有许多人参与的大项目而言,检出所有模块并且保持更新是一件相当乏味的工作。特别是你只对其中某(几)个模块感兴趣。这种情况,使用二进制依赖更合适。只要在eclipse上检出你需要工作的模块,然后为每个模块运行mvneclipse:eclipse。当然所有用到的artifacts在你的maven仓库中必须可用。
扁平项目布局:
有可能将父POM从自己的目录搬移到和模块相同级别的目录。
1、在guide-ide-eclipse下创建一个名为guide-ide-eclipse-project的新目录,并且将父POM搬移到这个目录。
2、对父POM修改模块引用:
问题:发布(releasee)插件不支持扁平结构。
项目有生命周期(lifecycle),生命周期中有一系列阶段,每个阶段都和一组goals绑定,goals由插件提供。
对于构建项目的人来说,只要学习一些命令就可以来构建所有的Maven项目,并且POM保证他们能获得期望的结果。
对于一个项目来说,会可能使用以下公共生命周期阶段:
ØValidate
–验证项目正确性,以及所有必要的信息是否齐备;
ØCompile
–编译项目源代码;
ØTest
–使用单元测试框架测试编译过的源代码。这些测试无需代码被打包或者部署;
ØPackage
–将编译过的代码打包,如JAR;
ØIntegration-test
–为了集成测试能够进行,处理和部署应用到合时的环境;
ØVerify
–检查和检验package是否有效并且符合质量要求;
ØInstall
–将package安装到本地仓库,供本地其他项目使用(作为依赖项);
ØDeploy
–在集成或者发布环境中,将最终的package拷贝到远程仓库,供其它开发人员或者项目共享。
记住,对于每个阶段,所有它之前的生命周期阶段都会执行,所以,你在命令行只要指定最后的那个就行。比如:
如果执行这个命令的话,会编译、测试、打包、校验和安装package到本地仓库。
同时,同样的命令可以在多模块场景中使用。比如:
这个命令会遍历所有的子项目,对每个子项目执行clean和install(包括所有install之前的阶段)。
这是标准的一组绑定;然而,有些打包处理不同,比如,一个项目仅仅有元数据,那么只要绑定install和deploy阶段。
记住,对于一些packaging类型,你需要在pom.xml文件的build节包含特定的plugin。一个例子就是Plexus插件,它提供了plexus-application和plexus-service
packaging。
这些配置的goals将会加入到由packaging绑定到生命周期的goals。如果绑定到特定阶段的goal超过一个,那么来自packaging的goal会先执行,然后执行在POM配置的goal。可以通过executions元素控制特定goals的顺序。
比如,Modello插件将modello:java绑定到generate-souces阶段,使用Modello插件从模型产生源代码,你必须将以下内容加入到POM的plugins节:
你可能会觉得奇怪,那儿为什么会有executions元素,这可以让你在需要时,使用不同的配置多次运行相同的goal。不同的execution可以给于不同的id,这样当用于继承或者应用profiles时,你就可以控制goal配置是否合并或者变成附加的execution中。
当有多个executions匹配一个阶段(phase)时,他们以POM中指定的顺序执行,来自继承的executions首先运行。
对于modello:java,它只对generate-source阶段(phase)有意义。不过某些goals可以用于多个阶段,并且没有默认值,对于这种情况,你可以(必须)自己指定阶段(phase)。比如,假设有一个goal
touch:timestamp,用于将当前时间显示到文件中,你需要在process-test-resources阶段用它来指出测试是何时开始的,可以像下面那样配置:
invokesthe
projectusingthe
现在,要将此类库应用到一个web应用程序。为了简单,我们的web应用程序将由一个jsp文件直接调用HotelModel类。首先,我们使用archetype插件创建一个新的web应用项目:
Next,weneedtoincludeourbusinesslogicJARinthisapplication.Allweneedtodoistoaddadependencytothenewpom.xml,pointingtoour
接着,我们需要将业务逻辑JAR包含进此web应用程序。我们所需做的事情就是加入一个依赖项到此新的pom.xml文件,并指向我们的HotelDatabase组件:
Nowweimplementthemain(andonly)JSPpage.Itsimplyliststheavailablecitiesand,ifacityischosen,liststhecorrespondinghotels:
现在,我们实现主要的JSP页面。它仅简单的列出所有可用的城市,如果选择了某个城市,就列出相应的宾馆:
Nowrun
the
anddeploytheHotelWebapp.warfiletoyourlocalrepository(youcanalsofinditinthe
ifyouneedto).Nowyoucandeploythiswarfiletoyourfavoriteapplicationserverandseewhatyouget(seeFigure4).
现在,从HotelWebapp目录运行mvn
install命令,这将编译、打包并且部署HotelWebapp.war文件到你的本地仓库(你也可以从target目录找到它)。现在,你可以把此war文件部署到你熟悉的应用服务器,并且可以看到如下的页面:
ittoalifecyclephase.Mavenwillthenfigureoutwhen(andhow)touseit.Someplug-insarealreadyusedbyMavenbehindthescenes,soyoujusthavetodeclaretheminthe
ofyourpom.xmlfile.Thefollowingplug-in,forexample,isusedtocompilewithJ2SE1.5sourcecode:
Maven2含有乙一组数量不断增长的插件,用来为你的构建过程增加额外的功能。为了使用插件,你需要将它绑定到生命周期阶段。Maven将指出何时(以及如何)使用它们。一些组件,Maven在背后已经使用了,所以你只要在pom.xml文件的plugins节声明它们。以下插件,用来编译J2SE
1.5的源代码:
Inothercases,youbindtheplug-intoa
lifecyclephasesothatMavenwillknowwhentouseit.Inthefollowingexample,wewanttorunsomestandardAnttasks.Todothis,webindthe
the
the
另外,你可以将插件和某个生命周期阶段绑定,这样,Maven就会知道何时使用它们。下面的例子,我们要运行一些标准的Ant任务。为了达到这个目的,我们将maven-antrun-plugin插件绑定到generate-sources阶段,并且把ant任务插入到tasks标记处,如下所示:
Javaapplication.Mavencompletelyintegratesunittestingintothedevelopmentlifecycle.Torunallyourunittests,youinvokethe
phase:
现在来测试应用程序,在源代码中可以找到一些简单测试类。单元测试是(或者应该是)任何企业java应用程序的重要组成部分。Maven将单元测试完全集成进了开发生命周期。要运行所有的单元测试,可以调用test生命周期阶段:
Ifyouwanttorunonlyonetest,youcanusethe
如果你只想运行某个单元测试,可以使用test参数指定:
AnicefeatureofMaven2isitsuseofregularexpressionsandthe
tocontrolthetestsyouwanttorun.Ifyouwanttorunonlyonetest,youjustindicatethenameofthetestclass:
Maven2的一个特性是test参数使用规则表达式来运行测试,如果只想运行某个测试,你只要指出测试类的名称就可以了:
Ifyouwanttorunonlyasubsetoftheunittests,youcanuseastandardregularexpression.Forexample,totestall
如果你想运行一组单元测试,可以使用标准的规则表达式,比如,为了测试所有的ModelTest类:
compiles,tests,andbundlesyourclassesintoajarfileanddeploysittoyourlocalMaven2repository,whereitcanbeseenbyotherprojects:
一旦完成了单元测试,就可以构建和部署新的JAR。Install命令编译、测试和将所有的类文件打包成一个jar文件,并把此jar文件部署到你的本地Maven
2仓库中。
Maven2介绍(3)
2onasimpleJavaEnterpriseEditionproject.Thedemoapplicationinvolvesanimaginary(andsimplified)hoteldatabasesystem.TodemonstratehowMavenhandlesdependenciesbetweenprojectsandcomponents,thisapplicationwillbebuiltusingtwocomponents
(seeFigure3):
现在,让我们来看一些Maven2中的基本标记,看看它们如何工作。以下部分用Maven
2来开发一个简单的java企业级应用项目。示例应用程序涉及到虚构的(简化的)宾馆数据库系统。为了介绍Maven如何处理项目以及组件之间的依赖,这个应用使用两个组件(子项目)。
Abusinesslogiccomponent:HotelDatabase.jar
AWebapplicationcomponent:HotelWebApp.war
YoucandownloadthesourcecodetofollowalongwiththetutorialinResources.
notbedistributedtoallusers.Ifyouarebehindafirewallwithaproxy,forexample,youneedtoconfiguretheproxysettingssothatMavencandownloadJARsfromrepositoriesontheWeb.ForMaven1users,thebuild.propertiesandproject.propertiesfiles
dothisjob.InMaven2,theyhavebeenreplacedbyasettings.xmlfile,whichgoesinthe$HOME/.m2directory.Hereisanexample:
我们从配置工作环境起步。在真实项目中,你经常需要定义和配置环境或者用户指定的参数,这些环境和参数不和其他用户共享。如果你使用防火墙,那么你需要配置代理服务器,让Maven可以从远程仓库下载JARs。对于Maven
1的用户,由build.properties和project.properties文件来做这项工作,而在Maven
2中,则由$HOME/.m2目录下的settings.xml文件来做这项工作:
whichbuildsanemptyMaven2-compatibleprojectdirectorystructure.Thisplug-inprovesconvenientforgettingabasicprojectenvironmentupandrunningquickly.ThedefaultarchetypemodelwillproduceaJARlibraryproject.Severalotherartifacttypes
areavailableforotherspecificprojecttypes,includingWebapplications,Mavenplug-ins,andothers.
接下来,就是为业务逻辑组件创建一个新的Maven2项目模板。Maven
2提供了archetype插件,用来创建空的、兼容Maven2的项目目录结构。这个插件为快速建立基本项目环境提供了方便。默认的archetype模型将产生JAR库项目。其他一些artifact类型可用来创建其他指定的项目类型,包括WEB应用程序,Maven插件等等。
RunthefollowingcommandtosetupyourHotelDatabase.jarproject:
运行以下命令来创建你的HoteDatabase.jar项目:
NowyouhaveabrandnewMaven2projectdirectorystructure.Switchtothe
tocontinuethetutorial.
现在,你有了一个新的Maven2项目目录结构,切换到HotelDatabase目录,继续此向导。
isasimpleJavaBean.The
whichlistsavailablecities,andthe
allhotelsinagivencity.Asimple,memory-basedimplementationofthe
ispresentedhere:
现在,我们实现业务逻辑,Hotel类是一个简单的JavaBean。而HotelModel类实现两个服务:findAvailabelCities()方法,列出所有能够获得的城市;
findHotelsByCity()方法,则依据给定的城市获取宾馆列表。
以下是一个简单的,基于内存实现的HotelModel类:
Maven2介绍(2)
withnameslikethose.InMaven1,correspondingplug-insarecalleddirectly.TocompileJavasourcecode,forinstance,the
isused:
项目生命周期是Maven2的中心。大多数开发人员熟悉构建阶段的一些标记,比如compile,test,以及deploy。Ant的targets的名称也和这个类似。在Maven
1中,相应的插件是直接调用的,为了编译Jave源代码,使用java插件:
InMaven2,thisnotionisstandardizedintoasetofwell-knownandwell-definedlifecyclephases(seeFigure2).Insteadofinvokingplug-ins,theMaven2developer
invokesalifecyclephase:
在Maven2中,这些标记被标准化到一组众所周知的、明确的生命周期阶段(参见图2)。在Maven
2中,开发人员直接调用生命周期阶段,而不是调用插件:
SomeofthemoreusefulMaven2lifecyclephasesarethefollowing:以下是一些有用的Maven
2生命周期阶段:
whichisgenerallyaccomplishedusingtheappropriateplug-ins。生成额外的应用程序所需的源代码,通常由相应插件获得。
WAR,etc.)。以分发的格式(jar,war等等)打包。
anenvironmentwhereintegrationtestscanberun。集成测试。
dependencyinotherprojectsonyourlocalmachine。安装包到本地仓库,作为你本机的其他项目的依赖项。
packagetotheremoterepositoryforsharingwithotherdevelopersandprojects。在集成和发布环境下使用,拷贝最终的包到远程仓库,分享给其他开发人员和项目。
Manyotherlifecyclephasesareavailable.SeeResourcesfor
moredetails.
ThesephasesillustratethebenefitsoftherecommendedpracticesencouragedbyMaven2:onceadeveloperisfamiliarwiththemainMaven2lifecyclephases,heshould
feelateasewiththelifecyclephasesofanyMavenproject.
还有许多其他生命周期阶段。这些阶段阐明了使用Maven2推荐的开发实践的好处:一旦一个开发人员熟悉了主要的Maven
2生命周期阶段,他就会易于接受其他Maven项目的生命周期阶段。
Thelifecyclephaseinvokestheplug-insitneedstodothejob.Invokingalifecyclephaseautomaticallyinvokesanypreviouslifecyclephasesaswell.Sincethe
lifecyclephasesarelimitedinnumber,easytounderstand,andwellorganized,becomingfamiliarwiththelifecycleofanewMaven2projectiseasy.
生命周期阶段调用它所需的插件来完成任务。调用一个生命周期阶段自动调用所有在它之前的生命周期阶段。由于生命周期阶段在数量上有限,易于理解,组织良好,理解一个使用Maven2的新项目的生命周期是相对容易的事情。
7.4.及物依赖Transitive
OneofthehighlightsofMaven2istransitivedependencymanagement.Ifyouhaveeverusedatoollikeurpmion
aLinuxbox,you'llknowwhattransitivedependenciesare.WithMaven1,youhavetodeclareeachandeveryJARthatwillbeneeded,directlyorindirectly,byyourapplication.Forexample,canyoulisttheJARsneededbyaHibernateapplication?WithMaven
2,youdon'thaveto.YoujusttellMavenwhichlibrariesyouneed,andMavenwilltake
careofthelibrariesthatyourlibrariesneed(andsoon).
Maven2的一大亮点就是及物依赖管理。如果你使用过象Linux平台上的urpmi的工具,你会知道什么是及物依赖。在Maven
1中,你必须声明你所需要的每个应用程序直接或者间接需要的JAR。比如,Hibernate应用程序所需的JARs就有很多。在Maven
2中,你不必如此,你只要告诉Maven你所需要的库,Maven会照顾到你的库所需要的库。
SupposeyouwanttouseHibernateinyourproject.Youwouldsimplyaddanewdependencytothe
inpom.xml,asfollows:
假设你在项目中需要使用Hibernate,你只要简单的在pom.xml文件的dependencies节中加入一个新的dependency,如下所示:
Andthat'sit!Youdon'thavetohuntaroundtoknowinwhichotherJARs(andinwhichversions)youneedtorunHibernate3.0.3;Mavenwilldoitforyou!
这就够了!你不必知道运行Hibernate3.0.3需要哪些JARs,以及它们的版本,Maven会为你做这些工作!
TheXMLstructurefordependenciesinMaven2issimilartotheoneusedinMaven1.Themaindifferenceisthe
whichisexplainedinthefollowingsection.
testing,whileotherswillbeprovidedatruntimebytheapplicationserver.Usingatechniquecalleddependencyscoping,
Maven2letsyouusecertainJARsonlywhenyoureallyneedthemandexcludesthemfromtheclasspathwhenyoudon't.
在实际企业应用程序中,你可能无需包含所有的依赖项到你的部署应用程序中,有些JARs只有单元测试需要,而有些可能由应用服务器提供。使用称为依赖范围的技术,Maven
2让你使用那些你需要的JARs,而在不需要时则从classpath排除它们。
Mavenprovidesfourdependencyscopes:Maven提供了4个依赖范围
isthedefaultvalue.编译时依赖在所有阶段都可获得,这是默认值。
butwillnotbedeployed.YouwouldusethisscopewhenyouexpecttheJDKorapplicationservertoprovidetheJAR.TheservletAPIsareagoodexample.Provided依赖范围用来编译应用程序,但无需部署。若你用到jdk或者应用服务器提供的JAR,则使用此范围,servlet
APIs就属于这个依赖范围。
onlyforexecution,suchasJDBC(JavaDatabaseConnectivity)drivers.运行依赖范围在编译阶段是不需要的,只有在运行时需要,比如JDBC驱动程序。
(JUnit,forexample).测试范围依赖,仅在编译和运行单元测试时需要(比如Junit)。
visibilitywithintheteam.Withminimaleffort,youcanhaveaprofessional-qualityprojectWebsiteupandrunninginverylittletime.
任何项目中,内部沟通都是一个重要部分,一个集中的项目技术网站可以显著提高开发组内的沟通能力。通过Maven2,你能够轻易的创建一个专业级的项目网站。
ThistakesawholenewdimensionwhentheMavensitegenerationisintegratedintoabuildprocessusingcontinuousintegrationorevenautomaticnightlybuilds.
AtypicalMavensitecanpublish,onadailybasis:
当Maven站点产生功能使用持续集成或者自动每日构建,集成到构建过程中后,将会带来一个全新的境界。典型的Maven站点能够每日发布:
Generalprojectinformationsuchassourcerepositories,defecttracking,teammembers,etc.一般项目信息,比如源代码库,缺失跟踪,小组成员等等。
Unittestandtestcoveragereports。单元测试以及测试覆盖报表。
AutomaticcodereviewsandwithCheckstyleandPMD。使用Checkstyle和PMD自动进行代码reviews。
Configurationandversioninginformation。配置和版本信息。
Dependencies。依赖项
Javadoc。
Sourcecodeinindexedandcross-referencedHTMLformat。索引的以及交叉引用的HTML格式的源代码。
Teammemberlist。小组成员列表。
Andmuchmore。以及其他。
Onceagain,anyMaven-savvydeveloperwillimmediatelyknowwheretolooktobecomefamiliarwithanewMaven2project.
再重申一下,任何熟悉Maven的开发人员将能够快速的熟悉另一个新的基于Maven
2的项目。
Maven2介绍(1)
7.Maven
MavenisapopularopensourcebuildtoolforenterpriseJavaprojects,designedtotakemuchofthehardworkoutofthebuildprocess.Mavenusesadeclarative
approach,wheretheprojectstructureandcontentsaredescribed,ratherthenthetask-basedapproachusedinAntorintraditionalmakefiles,forexample.Thishelpsenforcecompany-widedevelopmentstandardsandreducesthetimeneededtowriteandmaintain
buildscripts.
Maven是一个流行的构建企业级java项目的开源构建工具,它的设计目标是将一些构建过程中的繁重工作剥离出来。Maven使用宣告式的方法,项目的结构和内容是被描述的,而不是像使用ant或者传统make文件那样基于任务的方法。这样可以加强企业范围内的开发标准,并且减少编写和维护构建脚本的时间。
Thedeclarative,lifecycle-basedapproachusedbyMaven1is,formany,aradicaldeparturefrommoretraditionalbuildtechniques,andMaven2goesevenfurther
inthisregard.Inthisarticle,IgothroughsomeofthebasicprincipalsbehindMaven2andthenstepthroughaworkingexample.Let'sstartbyreviewingthefundamentalsofMaven2.
宣告式的,基于生命周期的方法在Maven1中就已经得到应用,在这方面,Maven
2走的更远。先来看一下Maven2的基础。
aboutversioningandconfigurationmanagement,dependencies,applicationandtestingresources,teammembersandstructure,andmuchmore.ThePOMtakestheformofanXMLfile(pom.xml),
whichisplacedinyourprojecthomedirectory.Asimplepom.xmlfileisshownhere:
Maven2项目的核心是项目对象模型,缩写为POM。项目对象模型包括对项目的详细描述,包括项目的版本信息、配置管理、依赖、应用和测试资源、开发组成员和结构、以及更多。POM采用XML文件方式(pom.xml),放置在项目的根目录下,下面是一个pom.xml文件的示例:
7.2.Maven
MuchofMaven'spowercomesfromthestandardpracticesitencourages.AdeveloperwhohaspreviouslyworkedonaMavenprojectwillimmediatelyfeelfamiliarwith
thestructureandorganizationofanewone.Timeneednotbewastedreinventingdirectorystructures,conventions,andcustomizedAntbuildscriptsforeachproject.Althoughyoucanoverrideanyparticulardirectorylocationforyourownspecificends,you
reallyshouldrespectthestandardMaven2directorystructureasmuchaspossible,forseveralreasons:
Maven的魔力来自于他所鼓励的标准实践。一个经历过使用Maven开发项目的开发人员会马上对新的项目结构和组织感到熟悉。这样就无需浪费对每个项目重新熟悉目录结构,约定以及定制ANT构建脚本的时间。尽管你可以用自己的方式覆盖任何特定的目录位置,你还是应该尽量遵从标准Maven
2的目录结构:
ItmakesyourPOMfilesmallerandsimpler(使你的POM文件简短)
Itmakestheprojecteasiertounderstandandmakeslifeeasierforthepoorguywhomustmaintaintheprojectwhenyouleave(使项目易于理解)
Itmakesiteasiertointegrateplug-ins(易于集成插件)
ThestandardMaven2directorystructureisillustratedinFigure1.IntheprojecthomedirectorygoesthePOM(pom.xml)andtwosubdirectories:srcforallsource
codeandtargetforgeneratedartifacts.
标准的Maven2目录结构如图1所示。项目主目录下有一个POM(pom.xml)以及两个子目录:src放置所有的源代码,target目录放置构建产生的文件。
Thesrcdirectoryhasanumberofsubdirectories,eachofwhichhasaclearlydefinedpurpose:
src目录有一些子目录,每个子目录都有清晰定义的目的:
src/main/java:YourJavasourcecodegoeshere(strangelyenough!)。java源代码目录
src/main/resources:Otherresourcesyourapplicationneeds。应用程序所需的资源目录。
src/main/filters:Resourcefilters,intheformofpropertiesfiles,which
maybeusedtodefinevariablesonlyknownatruntime。资源过滤文件,属性文件格式,用于定义运行时所需得变量。
src/main/config:Configurationfiles。配置文件。
src/main/webapp:TheWebapplicationdirectoryforaWARproject。WAR项目的web应用程序目录
src/test/java:Unittests。单元测试源代码。
src/test/resources:Resourcestobeusedforunittests,butwillnotbedeployed。用于单元测试的资源,不会被部署。
src/test/filters:Resourcesfilterstobeusedforunittests,butwillnot
bedeployed。单元测试所用到的资源过滤,不会被部署。
src/site:FilesusedtogeneratetheMavenprojectWebsite。用于产生maven项目web站点的目录。
MAVEN指南(8)
MAVEN指南(7)
MAVEN指南(6)
为了给你的站点添加报表,必须在POM的reporting节加入插件。下面的例子显示如何配置标准的项目信息报表。
如果在site.xml中包含有${reports},那么产生的站点中将出现这些报表。
为了允许使用多个locales,按如下配置就可以了:
这样,就会同时产生一个英语和法语的站点版本。如果en是你的当前locale,那么,它会在站点的根目录下,而法语翻译版本则在fr/子目录下。
为了取代默认的翻译内容,可以在站点目录下放置一个以locale的名字为名称的子目录,同时,创建该locale的站点描述符文件,比如:
通过创建相关语言的站点描述符,翻译的站点就可以独立发展了。
这个命令必须在一行内。运行此命令后,将会创建一个my-webapp的目录,同时包含如下项目描述符:
注意到packaging元素,告诉maven构建一个WAR,尝试以下命令:
可以看到target/my-webapp.war被构建。
首先,在两个项目的父目录里,需要创建一个新的父pom.xml文件,如下所示:
父pom.xml文件包含如下内容:
Webapp项目需要添加对jar项目的依赖项:
最后,同时在两个模块的pom.xml文件中加入以下内容:
现在,在顶层目录,运行以下命令:
这时,创建了my-webapp/target/my-webapp.war,它包含了my-app-1.0-SNAPSHOT.jar。
这是怎样工作的呢?首先,创建了父POM(名为app),它包含了一组模块,这就告诉maven,对这组模块项目进行操作(可以通过使用—non-recursive命令行选项覆盖此行为)。
接着,我们告诉WAR项目需要my-appjar文件,这样,使此jar出现在了classpath上,war项目中的所有代码都可以使用它。Jar项目总是在war项目之前构建,并且告诉war插件在lib目录包含那个jar文件。
你可能注意到junit-3.8.1.jar也是一个依赖项,但却没有出现war的lib目录,因为scope元素被设为test,只有测试才需要junit-3.8.1.jar。
最后,就是在模块pom.xml文件中包含parent定义。这和maven1.0中的extend元素不同:确保POM总是能够定位,即使项目和其父分布在不同地点,只要可以通过库查找。
你可以在顶层目录,通过以下命令来产生IDEA的工作空间:
MAVEN指南(5)
如果你看一下产生的项目目录结构,可以看到如下内容:
注意到现在多了一个${basedir}/src/site目录,里面有站点描述符和所支持的各种文档类型。
Xdoc格式,和maven1.0中一样,只是把navigation.xml杯替换成了站点表述符。
APT格式,基本为文本格式,类似于wiki格式,可以用来编写简单的,结构画的文档。请参考《APT格式》。
FML格式,FAQ格式,在maven1.0中也有。
还有其它格式。
Maven支持几种输出格式,但对于2.0,只支持XHTML格式。
产生的站点位于target/site/目录
Id元素用于标识库,所以在settings.xml文件中,可以和其它库一样附加安全凭据。Url给出了部署位置。当前,仅支持SSH,上面的配置将把站点拷贝到www.mycompany.com站点的/www/docs/project目录。
部署站点使用site-deploygoal。
示例:
里面的${reports}将会被配置的报表替换。
文件site.css将被用于默认的XHTML输出,可以用它来调整maven默认的样式。
文件pic1.jpg可以在站点中的任何页面引用。
为了让maven过滤资源,只要在pom.xml的resource节将filtering设为true。
注意到我们加入了以前没有的build,resoucses,resource元素,另外,我们必须明确的指定资源的位置,这里为src/main/resources目录,所有这些以前都是作为默认值的,但因为filtering的默认值为false,我们必须把这些都加入到pom.xml中,覆盖默认设置。
为了引用在pom.xml中定义的属性,属性名称使用XML元素来定义值,“pom”允许作为项目根元素的别名。所以${pom.name}指向项目的名称,而${pom.version}指向项目的版本,${pom.build.finalName}指向最后创建的打包文件的名称,等等。注意,某些POM元素有默认值,所以没有必要在pom.xml文件中显式定义。同样的,在用户settings.xml文件中的值可以通过以“settings”开头的属性名字来引用(比如,${settings.localRepository}指向用户本地库的路径)。
为了继续我们的例子,在application.properties文件中加入一对属性
在这里,执行以下命令(process-resources是构建生命周期中的一个拷贝、过滤资源的阶段)
为了引用外部文件中定义的属性,必须在pom.xml文件中增加对此外部文件的引用。首先,创建一个外部属性文件,假设为src/main/filters/filter.properties。
接下来,我们在pom.xml文件中增加对此外部资源文件的引用:
然后,如果我们在application.properties文件中加入属性引用
再次执行mvnprocesss-resources命令,将会把新属性的值放入application.properties文件中。作为在外部文件中定义my.filter.value属性的替代,你可以在pom.xml中定义properties节,可以获得同样的效果。
资源过滤同样可以从系统属性中获取值;比如java的内建属性(java.version或者user.home)或者命令行定义的属性(使用java–D参数)。
现在,当执行以下命令后,application.properties文件将包含系统属性的值:
Pom.xml文件中的dependencies节列出了所有构建时需要的外部依赖项(不管是编译时、测试时、运行时)。示例中,目前只用到了Junit。
对于每个外部依赖项,必须定义4样东西:goupId,artifactId,version以及scope。GroupId,artifactId以及version元素没有什么特别,scope元素指明项目如何使用此依赖项,其值可以是compile,test以及runtime。要想获得可以指定的依赖项的更多信息,请参考《项目描述符参考》。
对于更多的依赖项机制,请参考《依赖项管理》。
通过此dependency信息,maven可以在构建项目时引用此依赖项。那么,maven从哪儿引用此依赖项呢?Maven会去本地库(默认位置:~/.m2/repository)中查找所有依赖项。在上一节,我们把我们项目的产品(my-app-1.0-SNAPSHOT.jar)安装到了本地库中,一旦安装完成,其它项目就可以把此jar文件作为一个依赖项引用:
当Maven发现项目引用的依赖项在本地库中不存在时,自动把它从远程库中下载到本地库中。你可能已经注意到,当你构建第一个项目时,Maven下载了一些包(这些依赖项与构建项目时用到的插件有关)。默认情况下,Maven使用的远程库在http://www.ibiblio.org/maven2。当然,你也可以自己设置远程库(公司的中心库),作为ibiblio的替代或者补充。关于库德更多信息,请参考《库介绍》。
让我们增加一个依赖项到我们的项目中,假设我们需要日志功能,必须增加log4j作为依赖项。首先,我们需要知道log4j的groupId,artifactId以及version。
我们可以通过浏览ibiblio来查看它,或者使用Google来搜索“site:www.ibiblio.orgmaven2log4j”。这个搜索会显示一个称为“/maven2/log4j/log4j”或者“/pub/packages/maven2/log4j/log4j”的目录。在那个目录中,有一个名为maven-metadata.xml的文件,这里是那个文件的内容:
我们从这个文件查看到log4j的groupId=log4j,artifactId=log4j。同时,我们可以看到有多个不同的版本可以选择。现在,我们仅仅使用最新版本,1.2.12(某些maven-metadata.xml文件会指定哪个版本为当前版本)。除了maven-metadata.xml文件,我们还可以看到对应版本的一组目录,里面放了相应版本的log4j包,以及pom.xml(指明log4j的依赖项),还有另一个maven-metadata.xml文件和各个文件的md5文件。
现在,我们知道了所需的信息,可以在我们的pom.xml增加依赖项了,如下所示:
现在执行编译项目(mvncompile)时,可以看到maven下载log4j。
如果你看以下项目的POM,你可以注意到packaging元素被设置为jar。这让maven知道从以上命令产生一个jar文件。在${basedir}/target目录下,产生了一个jar文件。
现在,你想将产生的jar文件安装到你的本地库中(~/.m2/repository是默认的位置)。想了解库的更多信息,请参考《库介绍》。执行以下命令,就能安装到本地库:
执行过程中会产生如下输出:
注意执行测试的surefire插件,它会按命名规则查找所有的测试文件,默认情况下,测试文件包括:
Ø**/*Test.java
Ø**/Test*.java
Ø**/*TestCase.jave
默认情况下,被排除的:
Ø**/Abstract*Test.java
Ø**/Abstract*TestCase.jave
到目前为止,已经经历了安装、构建、测试、打包、安装典型的maven项目等步骤。这可能是大多数项目使用maven需要经历的步骤,而驱动的项目模型文件只有18行内容,对比典型的build文件,它的行数比pom多的多。
不需任何附加工作maven依靠此POM就可以为你的项目产生一个网站,你只须定制你的maven网站,如果时间紧迫,你只须执行以下命令:
还有许多goals可以执行,比如
此命令删除target目录。
可能你需要为你的项目产生一个IntelliJIDEA描述符,执行以下命令:
在maven2.0中,所有的插件看起来更像依赖项,并且某些方面确实就是依赖项。这个插件(包含特定的版本,如果指定的话,否则使用最新版本)将被自动下载并使用。
Configuration元素用来对编译插件的每个goal提供参数。具体请参考《插件列表》、《构建生命周期介绍》。
在这个例子中,我们在${basedir}/src/main/resources目录下放了一个META-INF目录并在META-INF目录中放了一个application.properties文件。如果你将jar文件解压,可以看到以下内容:
就像你看到的那样,${basedir}/src/main/resources中的内容在jar文件的起始位置,我们的application.properties文件在META-INF目录下。同时,一些其他文件如META-INF/MANIFEST.MF,以及pom.xml和pom.properties文件也在其中,对于maven产生的jar文件来说,这是maven的标准行为。你可以自行创建自己的manifest,不过如果你不指定,maven会产生一个默认的。由于pom.xml和pom.properties文件都被打包到jar文件中,所以maven产生的产品都是自描述的,一个简单的应用就是获得应用程序版本。打开POM文件可能需要一些maven的工具,但打开属性文件可以使用标准的java
API来做到,属性文件pom.properties如下所示:
为了单元测试,可能需要把资源加入到classpath,你可以遵照同样的模式:
那么在测试代码中,只须使用如下代码就可访问这些资源:
Ø构建
Ø文档
Ø报表
Ø依赖(相关性)
ØSCMs
Ø发布
Ø分发
Maven不只是一个项目构建工具,并且也不只是ant的一个替代品。Maven是和ant完全不同的一个东西。Ant仅仅是一个简单的跨平台的构建工具,而Maven是一个模式应用,为了达到可视性、复用性、维护性和易于理解的一个下部构造(Maven
isabouttheapplicationofpatternsinordertoacheiveaninfrastructurewhichdisplaysthecharacteristicsofvisibility,reusability,maintainability,andcomprehensibility.)。
下面开始创建第一个项目,执行一下命令行:
一旦执行了以上命令,你会发现maven为这个新项目创建了一个名为my-app的目录,同时此目录中有一个pom.xml文件,文件内容如下所示:
Pom.xml包含项目对象模型(POM)。POM是Maven的基本单元,记住,Maven是以项目为中心的,所有的东西都围绕项目展开。简而言之,POM包含项目的所有重要信息,并且本质上提供了一站式的项目信息。关于pom的详细介绍,请参考《POM介绍》。
下面是pom中的一些重要元素:
Øproject:pom.xml文件中的顶层元素;
ØmodelVersion:指明POM使用的对象模型的版本。这个值很少改动。
ØgroupId:指明创建项目的组织或者小组的唯一标识。GroupId是项目的关键标识,典型的,此标识以组织的完全限定名来定义。比如,org.apache.maven.plugins是所有Maven插件项目指定的groupId。
ØartifactId:指明此项目产生的主要产品的基本名称。项目的主要产品通常为一个JAR文件。第二,象源代码包通常使用artifactId作为最后名称的一部分。典型的产品名称使用这个格式:<artifactId>-<version>.<extension>(比如:myapp-1.0.jar)。
Øversion:项目产品的版本号。Maven帮助你管理版本,可以经常看到SNAPSHOT这个版本,表明项目处于开发阶段。
Øname:项目的显示名称,通常用于maven产生的文档中。
Øurl:指定项目站点,通常用于maven产生的文档中。
Ødescription:描述此项目,通常用于maven产生的文档中。
POM中所有元素的说明,请参考《POM参考》。
产生了第一个项目的archetype后,你可以发现maven为你创建了如下的目录结构:
从archetype产生的项目有一个POM,一个源代码树,以及一个测试代码树,这是maven项目的标准布局(应用程序源码位于${basedir}/src/main/java,而测试源码位于${basedir}/src/test/java,这里${basedir}代表包含pom.xml的根目录)。
如果你想改变这个默认布局,请参考《标准目录布局介绍》。
当执行此命令的时候,可以看到以下信息:
第一次执行此命令(或者其他命令)时,maven需要下载所有插件以及满足此命令的依赖项。对于一个干净的maven安装版本,这需要花费一点时间。如果你再次运行此命令,maven就无需下载新的插件和依赖项了。
就像你从输出中看到的那样,编译过的类文件被存放到${basedir}/target/classes目录中,这个也是Maven标准的目录布局。
运行过程中会有以下输出:
输出中,有些内容值得注意:
Ø这个时候,maven会下载更多的依赖项。这些依赖项和插件是运行测试时所需要的。
Ø在编译和执行测试之前,maven按需编译main代码
如果你只须编译测试代码(无需运行测试),执行以下代码:
http://maven.apache.org下载合适的分发包,
系统需求:
安装:
在Windows2000/XP下:
Ø解压maven-2.0.2-bin.zip到相应的安装目录,以下假设目录为C:/Program
Files/ApacheSoftwareFoundation/maven-2.0.2。
Ø设置环境变量M2_HOME=
C:/ProgramFiles/ApacheSoftwareFoundation/maven-2.0.2,同时,将MAVEN的bin路径加到path环境变量中,PATH=%PATH%;%M2_HOME%/bin
Ø运行mvn
--version,来验证安装。
在Unix系列操作系统下(Linux,SolarisandMacOSX):
Ø将分发包解压到安装路径下,假设为/usr/local/maven-2.0.2
Ø将bin目录加入到path环境变量,比如:export
PATH=/usr/local/maven-2.0.2y/bin:$PATH
Ø运行mvn
–version来验证安装。
ApacheMaven是一个软件项目管理和综合工具。基于项目对象模型(POM)的概念,Maven可以从一个中心资料片管理项目构建,报告和文件。
本教程将介绍如何使用Maven在Java开发,或任何其他的编程语言的任何项目。
本教程是为初学者准备的,帮助他们了解Maven工具的基本功能。完成本教程后,希望能够熟悉使用ApacheMaven。
假设需要使用Maven来处理企业级Java项目开发。如果有软件开发的知识,如:JavaSE,JavaEE的开发和部署过程经验,对学Maven将有一定的帮助。
Maven概述,Maven是什么?
Maven环境配置
MavenPOM
Maven构建生命周期
Maven构建配置文件
Maven存储库
Maven插件
Maven创建Java项目
使用Maven构建和测试Java项目
Maven外部依赖
Maven项目文档
Maven项目模板
Maven快照
Maven构建自动化-Hudson
Maven依赖管理
Maven自动化部署
MavenWeb应用
EclispeIDE集成Maven
NetBeansIDE集成Maven
另附几个学习Maven不错的网址:
---------------------------------------------------
1、Maven官方网站http://maven.apache.org/
2、Maven中文教程http://www.yiibai.com/maven/
3、ApacheMaven入门
http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.html
1.Maven2Repository
Eclipse需要知道Maven的本地仓库的路径。所以,类路径变量M2_REPO必须设置。执行以下命令:mvn-Declipse.workspace=<path-to-eclipse-workspace>eclipse:add-maven-repo |
Project。由于缺少MAVEN_REPO变量,Eclipse提示编译失败。在Eclipse的Preferences–Java-BuildPath-ClasspathVariables中添加名为MAVEN_REPO的变量,指向D:/maven/local/repository。
如果想让eclipse支持直接运行maven的goal,可以安装mavenide插件。
2.把Maven作为外部工具
可能需要从eclipse执行一些mavengoals,这可以通过配置外部装载器实现。为eclise添加一个变量,指向本地maven的可执行文件(mvn.bat/mvn)。从菜单条,选择Window>Preferences,选择Run/Debug>StringSubstitution,增加一个新的变量,比如maven_exec。当你设置新的外部装载器(丛菜单条,选择Run>ExternalTools,选择Program),你可以在位置字段引用刚才定义的maven_exec。进一步,引用project_log作为工作目录,指定所选的mavengoals作为参数,比如eclipse:eclipse。更多的信息请参考eclipse的帮助。
问题:如果插件可以产生变量和runner可能会好一点。
3.一个简单例子
如果你有一个单模块的简单java项目,使用eclipse将十分简单。为了从你的POM产生eclipse项目文件,执行以下命令:mvneclipse:eclipse |
4.多模块项目
Duetotheworkspaceideamanyeclipseusersareusedtoaflatlayoutandthereforewanttokeepthisstructure,whichispossiblebutnotrecommended.Actually,theonlyreasonfora
andedittheparentPOMwithoutcheckingoutthewholeproject.Thefollowingsampleshowshowtohandlemavenmultiplemoduleprojectswitheclipsewhilekeepingtherecommended
hierachicalprojectlayout.
假设eclipse是你的SCM客户端,这个例子将告诉你如何设置一个新的多模块项目。
1.设置一个新的叫做step-by-step的workspace,并且象上面所述增加M2_REPO类路径。
2.打开命令行shell,并且切换到新建的工作空间(workspace)目录。
3.从命令行使用archetype插件新建一个maven项目
mvnarchetype:create-DgroupId=guide.ide.eclipse-DartifactId=guide-ide-eclipse |
5.删除src文件夹,并且打开pom.xml文件,将父项目的packaging改为pom。
<packaging>pom</packaging> |
6.从命令行切换到guide-ide-eclipse项目路径,并且创建一些模块:
cdguide-ide-eclipse mvnarchetype:create-DgroupId=guide.ide.eclipse-DartifactId=guide-ide-eclipse-site mvnarchetype:create-DgroupId=guide.ide.eclipse.core-DartifactId=guide-ide-eclipse-core mvnarchetype:create-DgroupId=guide.ide.eclipse.module1-DartifactId=guide-ide-eclipse-module1 |
<modules> <module>guide-ide-eclipse-site</module> <module>guide-ide-eclipse-core</module> <module>guide-ide-eclipse-module1</module> </modules> |
<parent> <groupId>guide.ide.eclipse</groupId> <artifactId>guide-ide-eclipse</artifactId> <version>1.0-SNAPSHOT</version> </parent> |
<dependency> <groupId>guide.ide.eclipse.core</groupId> <artifactId>guide-ide-eclipse-core</artifactId> <version>1.0-SNAPSHOT</version> </dependency> |
mvninstall mvneclipse:eclipse |
Project)检入项目。记住,不要检入产生的eclipse文件。如果使用CVS,你因该在每个模块有一个.cvsignore文件,内容如下:
target .classpath .project .wtpmodules |
问题:插件应该可选的产生.cvsignore文件。
现在,你有不同的处理选项。如果你同时工作于所有模块,你应该具有eclipse的项目依赖,而不是二进制依赖,你可以设置一个新的workspace,并且从step-by-step/guide-ide-eclipse中导入。记住,你必须先删除父项目的.project文件。结果和从命令行检出整个项目相同,运行mvneclipse:eclipse并且最后把项目导入到你的eclipse工作空间。两种方式都可以使用eclipse同步你对项目的修改。
对于有许多人参与的大项目而言,检出所有模块并且保持更新是一件相当乏味的工作。特别是你只对其中某(几)个模块感兴趣。这种情况,使用二进制依赖更合适。只要在eclipse上检出你需要工作的模块,然后为每个模块运行mvneclipse:eclipse。当然所有用到的artifacts在你的maven仓库中必须可用。
扁平项目布局:
有可能将父POM从自己的目录搬移到和模块相同级别的目录。
1、在guide-ide-eclipse下创建一个名为guide-ide-eclipse-project的新目录,并且将父POM搬移到这个目录。
2、对父POM修改模块引用:
<modules> <module>../guide-ide-eclipse-site</module> <module>../guide-ide-eclipse-core</module> <module>../guide-ide-eclipse-module1</module> </modules> |
IntroductiontotheBuildLifecycle(1)
8.构建生命周期介绍
8.1.构建生命周期基础
Maven2是以构建生命周期为中心概念的。这意味着构建和分发一个特定的artifact是被清晰定义的。项目有生命周期(lifecycle),生命周期中有一系列阶段,每个阶段都和一组goals绑定,goals由插件提供。
对于构建项目的人来说,只要学习一些命令就可以来构建所有的Maven项目,并且POM保证他们能获得期望的结果。
对于一个项目来说,会可能使用以下公共生命周期阶段:
ØValidate
–验证项目正确性,以及所有必要的信息是否齐备;
ØCompile
–编译项目源代码;
ØTest
–使用单元测试框架测试编译过的源代码。这些测试无需代码被打包或者部署;
ØPackage
–将编译过的代码打包,如JAR;
ØIntegration-test
–为了集成测试能够进行,处理和部署应用到合时的环境;
ØVerify
–检查和检验package是否有效并且符合质量要求;
ØInstall
–将package安装到本地仓库,供本地其他项目使用(作为依赖项);
ØDeploy
–在集成或者发布环境中,将最终的package拷贝到远程仓库,供其它开发人员或者项目共享。
记住,对于每个阶段,所有它之前的生命周期阶段都会执行,所以,你在命令行只要指定最后的那个就行。比如:
mvninstall |
同时,同样的命令可以在多模块场景中使用。比如:
mvncleaninstall |
8.2.设置项目以使用构建生命周期
使用构建生命周期是比较简单的,但当你为一个项目创建一个Maven构建时,如何将任务分配给那些构建阶段呢?8.2.1.Packaging
首先,是设置项目的packaging,默认为jar,所以不管你是否指定,packaging都会发生。每个packaging包含一系列绑定到特定阶段的goals。比如,对于JAR,会有以下goals绑定到生命周期:阶段 | Goal |
Process-resources | Resources:resources |
Compile | Compiler:compile |
Precess-test-resources | Resources:testResources |
Test-compile | Compiler:testCompile |
Test | Surefire:test |
Package | Jar:jar |
Install | Install:install |
Deploy | Deploy:deploy |
记住,对于一些packaging类型,你需要在pom.xml文件的build节包含特定的plugin。一个例子就是Plexus插件,它提供了plexus-application和plexus-service
packaging。
8.2.2.插件
第二种对阶段增加goals的方法就是在项目中配置插件。插件包含每个goal绑定到哪个阶段的信息。记住,仅仅增加插件还不够,你必须同时指定你需要的goals。这些配置的goals将会加入到由packaging绑定到生命周期的goals。如果绑定到特定阶段的goal超过一个,那么来自packaging的goal会先执行,然后执行在POM配置的goal。可以通过executions元素控制特定goals的顺序。
比如,Modello插件将modello:java绑定到generate-souces阶段,使用Modello插件从模型产生源代码,你必须将以下内容加入到POM的plugins节:
... <plugin> <groupId>org.codehaus.modello</groupId> <artifactId>modello-maven-plugin</artifactId> <executions> <execution> <configuration> <model>maven.mdo</model> <modelVersion>4.0.0</modelVersion> </configuration> <goals> <goal>java</goal> </goals> </execution> </executions> </plugin> ... |
当有多个executions匹配一个阶段(phase)时,他们以POM中指定的顺序执行,来自继承的executions首先运行。
对于modello:java,它只对generate-source阶段(phase)有意义。不过某些goals可以用于多个阶段,并且没有默认值,对于这种情况,你可以(必须)自己指定阶段(phase)。比如,假设有一个goal
touch:timestamp,用于将当前时间显示到文件中,你需要在process-test-resources阶段用它来指出测试是何时开始的,可以像下面那样配置:
... <plugin> <groupId>com.mycompany.example</groupId> <artifactId>touch-maven-plugin</artifactId> <executions> <execution> <phase>process-test-resources</phase> <configuration> <file>${project.output.directory}/timestamp.txt</file> </configuration> <goals> <goal>timestamp</goal> </goals> </execution> </executions> </plugin> ... |
Maven2介绍(5)
7.7.6.创建WEB应用程序
NowwewanttousethislibraryinaWebapplication.Forsimplicity,ourWebapplicationwillconsistofaJavaServerPages(JSP)filethatdirectlyinvokesthe
HotelModelclass.First,wecreateanewWebapplication
projectusingthe
archetypeplug-in:
现在,要将此类库应用到一个web应用程序。为了简单,我们的web应用程序将由一个jsp文件直接调用HotelModel类。首先,我们使用archetype插件创建一个新的web应用项目:
mvnarchetype:create-DgroupId=com.javaworld.hotels- DartifactId=HotelWebapp-Dpackagename=com.javaworld.hotels- DarchetypeArtifactId=maven-archetype-webapp |
HotelDatabasecomponent:
接着,我们需要将业务逻辑JAR包含进此web应用程序。我们所需做的事情就是加入一个依赖项到此新的pom.xml文件,并指向我们的HotelDatabase组件:
<dependency> <groupId>com.javaworld.hotels</groupId> <artifactId>HotelDatabase</artifactId> <version>1.0-SNAPSHOT</version> </dependency> |
现在,我们实现主要的JSP页面。它仅简单的列出所有可用的城市,如果选择了某个城市,就列出相应的宾馆:
<html> <body> <h2>Hoteldatabasetutorialapplication</h2> <%@pageimport=" java.util.List, com.javaworld.hotels.businessobjects.Hotel, com.javaworld.hotels.model.HotelModel" %> <% HotelModelmodel=newHotelModel(); String[]cityList=model.findAvailableCities(); StringselectedCity=request.getParameter("city"); List<Hotel>hotelList=model.findHotelsByCity(selectedCity); %> <h3>Chooseadestination</h3> <formaction="index.jsp"method="get"> Pleasechooseacity: <SELECTname="city"> <OPTIONvalue="">---Anycity---</OPTION> <% for(StringcityName:cityList){ %> <OPTIONvalue="<%=cityName%>"><%=cityName%></OPTION> <% } %> </SELECT> <BUTTONtype="submit">GO</BUTTON> </form> <%if(hotelList.size()>0){%> <h3>Availablehotelsin<%=selectedCity%></h3> <tableborder="1"> <tr> <th>Name</th> <th>Address</th> <th>City</th> <th>Stars</th> </tr> <% for(Hotelhotel:hotelList){ %> <tr> <td><%=hotel.getName()%></td> <td><%=hotel.getAddress()%></td> <td><%=hotel.getCity()%></td> <td><%=hotel.getStars()%>stars</td> </tr> <% } %> </table> <%}%> </body> </html> |
mvninstallfrom
the
HotelWebappdirectory;thiswillcompile,bundle,
anddeploytheHotelWebapp.warfiletoyourlocalrepository(youcanalsofinditinthe
targetdirectory
ifyouneedto).Nowyoucandeploythiswarfiletoyourfavoriteapplicationserverandseewhatyouget(seeFigure4).
现在,从HotelWebapp目录运行mvn
install命令,这将编译、打包并且部署HotelWebapp.war文件到你的本地仓库(你也可以从target目录找到它)。现在,你可以把此war文件部署到你熟悉的应用服务器,并且可以看到如下的页面:
Figure4.Thetutorialapplicationinaction |
7.7.7.使用插件
Maven2comeswithanever-increasingnumberofplug-insthataddextrafunctionstoyourbuildprocesswithlittleeffort.Touseaplug-in,youbindittoalifecyclephase.Mavenwillthenfigureoutwhen(andhow)touseit.Someplug-insarealreadyusedbyMavenbehindthescenes,soyoujusthavetodeclaretheminthe
pluginssection
ofyourpom.xmlfile.Thefollowingplug-in,forexample,isusedtocompilewithJ2SE1.5sourcecode:
Maven2含有乙一组数量不断增长的插件,用来为你的构建过程增加额外的功能。为了使用插件,你需要将它绑定到生命周期阶段。Maven将指出何时(以及如何)使用它们。一些组件,Maven在背后已经使用了,所以你只要在pom.xml文件的plugins节声明它们。以下插件,用来编译J2SE
1.5的源代码:
... <build> ... <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> |
lifecyclephasesothatMavenwillknowwhentouseit.Inthefollowingexample,wewanttorunsomestandardAnttasks.Todothis,webindthe
maven-antrun-pluginto
the
generate-sourcesphase,andaddtheAnttasksbetween
the
taskstags,asshownhere:
另外,你可以将插件和某个生命周期阶段绑定,这样,Maven就会知道何时使用它们。下面的例子,我们要运行一些标准的Ant任务。为了达到这个目的,我们将maven-antrun-plugin插件绑定到generate-sources阶段,并且把ant任务插入到tasks标记处,如下所示:
... <build> ... <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>generate-sources</phase> <configuration> <tasks> <!--Anttasksgohere--> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
7.7.4.使用Maven2进行单元测试
Nowlet'stesttheapplication.Afewsimpletestclassescanbefoundinthesourcecode.Unittestingis(orshouldbe!)animportantpartofanyenterpriseJavaapplication.Mavencompletelyintegratesunittestingintothedevelopmentlifecycle.Torunallyourunittests,youinvokethe
testlifecycle
phase:
现在来测试应用程序,在源代码中可以找到一些简单测试类。单元测试是(或者应该是)任何企业java应用程序的重要组成部分。Maven将单元测试完全集成进了开发生命周期。要运行所有的单元测试,可以调用test生命周期阶段:
mvntest |
testparameter:
如果你只想运行某个单元测试,可以使用test参数指定:
mvntest-Dtest=HotelModelTest |
testparameter
tocontrolthetestsyouwanttorun.Ifyouwanttorunonlyonetest,youjustindicatethenameofthetestclass:
Maven2的一个特性是test参数使用规则表达式来运行测试,如果只想运行某个测试,你只要指出测试类的名称就可以了:
mvntest-Dtest=HotelModelTest |
ModelTestclasses:
如果你想运行一组单元测试,可以使用标准的规则表达式,比如,为了测试所有的ModelTest类:
mvntest-Dtest=*ModelTest |
7.7.5.构建和部署JAR
Onceyou'rehappywiththetests,youcanbuildanddeployyournewJAR.Theinstallcommand
compiles,tests,andbundlesyourclassesintoajarfileanddeploysittoyourlocalMaven2repository,whereitcanbeseenbyotherprojects:
一旦完成了单元测试,就可以构建和部署新的JAR。Install命令编译、测试和将所有的类文件打包成一个jar文件,并把此jar文件部署到你的本地Maven
2仓库中。
mvninstall |
7.7.应用实例
NowthatwehaveseenafewofthebasicnotionsusedinMaven2,let'sseehowitworksintherealworld.TherestofthistutorialexamineshowwewoulduseMaven2onasimpleJavaEnterpriseEditionproject.Thedemoapplicationinvolvesanimaginary(andsimplified)hoteldatabasesystem.TodemonstratehowMavenhandlesdependenciesbetweenprojectsandcomponents,thisapplicationwillbebuiltusingtwocomponents
(seeFigure3):
现在,让我们来看一些Maven2中的基本标记,看看它们如何工作。以下部分用Maven
2来开发一个简单的java企业级应用项目。示例应用程序涉及到虚构的(简化的)宾馆数据库系统。为了介绍Maven如何处理项目以及组件之间的依赖,这个应用使用两个组件(子项目)。
Abusinesslogiccomponent:HotelDatabase.jar
AWebapplicationcomponent:HotelWebApp.war
Figure3.Thetutorialapplicationarchitectureinvolvestwosimplecomponents:aJAR(HotelDatabase.jar)andaWAR(HotelWebapp.war) |
7.7.1.设置项目环境
Westartbyconfiguringyourworkenvironment.Inreal-worldprojects,youwilloftenneedtodefineandconfigureenvironmentoruser-specificparametersthatshouldnotbedistributedtoallusers.Ifyouarebehindafirewallwithaproxy,forexample,youneedtoconfiguretheproxysettingssothatMavencandownloadJARsfromrepositoriesontheWeb.ForMaven1users,thebuild.propertiesandproject.propertiesfiles
dothisjob.InMaven2,theyhavebeenreplacedbyasettings.xmlfile,whichgoesinthe$HOME/.m2directory.Hereisanexample:
我们从配置工作环境起步。在真实项目中,你经常需要定义和配置环境或者用户指定的参数,这些环境和参数不和其他用户共享。如果你使用防火墙,那么你需要配置代理服务器,让Maven可以从远程仓库下载JARs。对于Maven
1的用户,由build.properties和project.properties文件来做这项工作,而在Maven
2中,则由$HOME/.m2目录下的settings.xml文件来做这项工作:
<?xmlversion="1.0"encoding="UTF-8"?> <settings> <proxies> <proxy> <active/> <protocol>http</protocol> <username>scott</username> <password>tiger</password> <port>8080</port> <host>my.proxy.url</host> <id/> </proxy> </proxies> </settings> |
7.7.2.使用archetype插件创建新项目
ThenextstepistocreateanewMaven2projecttemplateforthebusinesslogiccomponent.Maven2providesthearchetypeplug-in,
whichbuildsanemptyMaven2-compatibleprojectdirectorystructure.Thisplug-inprovesconvenientforgettingabasicprojectenvironmentupandrunningquickly.ThedefaultarchetypemodelwillproduceaJARlibraryproject.Severalotherartifacttypes
areavailableforotherspecificprojecttypes,includingWebapplications,Mavenplug-ins,andothers.
接下来,就是为业务逻辑组件创建一个新的Maven2项目模板。Maven
2提供了archetype插件,用来创建空的、兼容Maven2的项目目录结构。这个插件为快速建立基本项目环境提供了方便。默认的archetype模型将产生JAR库项目。其他一些artifact类型可用来创建其他指定的项目类型,包括WEB应用程序,Maven插件等等。
RunthefollowingcommandtosetupyourHotelDatabase.jarproject:
运行以下命令来创建你的HoteDatabase.jar项目:
mvnarchetype:create-DgroupId=com.javaworld.hotels-
DartifactId=HotelDatabase-Dpackagename=com.javaworld.hotels
NowyouhaveabrandnewMaven2projectdirectorystructure.Switchtothe
HotelDatabasedirectory
tocontinuethetutorial.
现在,你有了一个新的Maven2项目目录结构,切换到HotelDatabase目录,继续此向导。
7.7.3.实现业务逻辑
Nowweimplementthebusinesslogic.TheHotelclass
isasimpleJavaBean.The
HotelModelclassimplementstwoservices:the
findAvailableCities()method,
whichlistsavailablecities,andthe
findHotelsByCity()method,whichlists
allhotelsinagivencity.Asimple,memory-basedimplementationofthe
HotelModelclass
ispresentedhere:
现在,我们实现业务逻辑,Hotel类是一个简单的JavaBean。而HotelModel类实现两个服务:findAvailabelCities()方法,列出所有能够获得的城市;
findHotelsByCity()方法,则依据给定的城市获取宾馆列表。
以下是一个简单的,基于内存实现的HotelModel类:
packagecom.javaworld.hotels.model; importjava.util.ArrayList; importjava.util.List; importcom.javaworld.hotels.businessobjects.Hotel; publicclassHotelModel{ /** *Thelistofallknowncitiesinthedatabase. */ privatestaticString[]cities= { "Paris", "London", }; /** *Thelistofallhotelsinthedatabase. */ privatestaticHotel[]hotels={ newHotel("HotelLatin","Quartierlatin","Paris",3), newHotel("HotelEtoile","Placedel'Etoile","Paris",4), newHotel("HotelVendome","PlaceVendome","Paris",5), newHotel("HotelHilton","TrafalgarSquare","London",4), newHotel("HotelIbis","TheCity","London",3), }; /** *Returnsthehotelsinagivencity. *@paramcitythenameofthecity *@returnalistofHotelobjects */ publicList<Hotel>findHotelsByCity(Stringcity){ List<Hotel>hotelsFound=newArrayList<Hotel>(); for(Hotelhotel:hotels){ if(hotel.getCity().equalsIgnoreCase(city)){ hotelsFound.add(hotel); } } returnhotelsFound; } /** *Returnsthelistofcitiesinthedatabasewhichhaveahotel. *@returnalistofcitynames */ publicString[]findAvailableCities(){ returncities; } } |
Maven2介绍(2)
7.3.项目生命周期
ProjectlifecyclesarecentraltoMaven2.Mostdevelopersarefamiliarwiththenotionofbuildphasessuchascompile,test,anddeploy.Anthastargetswithnameslikethose.InMaven1,correspondingplug-insarecalleddirectly.TocompileJavasourcecode,forinstance,the
javaplug-in
isused:
项目生命周期是Maven2的中心。大多数开发人员熟悉构建阶段的一些标记,比如compile,test,以及deploy。Ant的targets的名称也和这个类似。在Maven
1中,相应的插件是直接调用的,为了编译Jave源代码,使用java插件:
$mavenjava:compile
InMaven2,thisnotionisstandardizedintoasetofwell-knownandwell-definedlifecyclephases(seeFigure2).Insteadofinvokingplug-ins,theMaven2developer
invokesalifecyclephase:
在Maven2中,这些标记被标准化到一组众所周知的、明确的生命周期阶段(参见图2)。在Maven
2中,开发人员直接调用生命周期阶段,而不是调用插件:
$mvncompile.
Figure2.Maven2lifecyclephases |
2生命周期阶段:
generate-sources:Generatesanyextrasourcecodeneededfortheapplication,
whichisgenerallyaccomplishedusingtheappropriateplug-ins。生成额外的应用程序所需的源代码,通常由相应插件获得。
compile:Compilestheprojectsourcecode。编译项目源代码。
test-compile:Compilestheprojectunittests。编译项目单元测试源代码。
test:Runstheunittests(typicallyusingJUnit)inthesrc/testdirectory。运行src/test目录下的单元测试(通常使用junit)。
package:Packagesthecompiledcodeinitsdistributableformat(JAR,
WAR,etc.)。以分发的格式(jar,war等等)打包。
integration-test:Processesanddeploysthepackageifnecessaryinto
anenvironmentwhereintegrationtestscanberun。集成测试。
install:Installsthepackageintothelocalrepositoryforuseasa
dependencyinotherprojectsonyourlocalmachine。安装包到本地仓库,作为你本机的其他项目的依赖项。
deploy:Doneinanintegrationorreleaseenvironment,copiesthefinal
packagetotheremoterepositoryforsharingwithotherdevelopersandprojects。在集成和发布环境下使用,拷贝最终的包到远程仓库,分享给其他开发人员和项目。
Manyotherlifecyclephasesareavailable.See
moredetails.
ThesephasesillustratethebenefitsoftherecommendedpracticesencouragedbyMaven2:onceadeveloperisfamiliarwiththemainMaven2lifecyclephases,heshould
feelateasewiththelifecyclephasesofanyMavenproject.
还有许多其他生命周期阶段。这些阶段阐明了使用Maven2推荐的开发实践的好处:一旦一个开发人员熟悉了主要的Maven
2生命周期阶段,他就会易于接受其他Maven项目的生命周期阶段。
Thelifecyclephaseinvokestheplug-insitneedstodothejob.Invokingalifecyclephaseautomaticallyinvokesanypreviouslifecyclephasesaswell.Sincethe
lifecyclephasesarelimitedinnumber,easytounderstand,andwellorganized,becomingfamiliarwiththelifecycleofanewMaven2projectiseasy.
生命周期阶段调用它所需的插件来完成任务。调用一个生命周期阶段自动调用所有在它之前的生命周期阶段。由于生命周期阶段在数量上有限,易于理解,组织良好,理解一个使用Maven2的新项目的生命周期是相对容易的事情。
7.4.及物依赖Transitive
dependencies
OneofthehighlightsofMaven2istransitivedependencymanagement.IfyouhaveeverusedatoollikeurpmionaLinuxbox,you'llknowwhattransitivedependenciesare.WithMaven1,youhavetodeclareeachandeveryJARthatwillbeneeded,directlyorindirectly,byyourapplication.Forexample,canyoulisttheJARsneededbyaHibernateapplication?WithMaven
2,youdon'thaveto.YoujusttellMavenwhichlibrariesyouneed,andMavenwilltake
careofthelibrariesthatyourlibrariesneed(andsoon).
Maven2的一大亮点就是及物依赖管理。如果你使用过象Linux平台上的urpmi的工具,你会知道什么是及物依赖。在Maven
1中,你必须声明你所需要的每个应用程序直接或者间接需要的JAR。比如,Hibernate应用程序所需的JARs就有很多。在Maven
2中,你不必如此,你只要告诉Maven你所需要的库,Maven会照顾到你的库所需要的库。
SupposeyouwanttouseHibernateinyourproject.Youwouldsimplyaddanewdependencytothe
dependenciessection
inpom.xml,asfollows:
假设你在项目中需要使用Hibernate,你只要简单的在pom.xml文件的dependencies节中加入一个新的dependency,如下所示:
<dependency> <groupId>hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.0.3</version> <scope>compile</scope> </dependency> |
这就够了!你不必知道运行Hibernate3.0.3需要哪些JARs,以及它们的版本,Maven会为你做这些工作!
TheXMLstructurefordependenciesinMaven2issimilartotheoneusedinMaven1.Themaindifferenceisthe
scopetag,
whichisexplainedinthefollowingsection.
7.5.依赖范围
Inareal-worldenterpriseapplication,youmaynotneedtoincludeallthedependenciesinthedeployedapplication.SomeJARsareneededonlyforunittesting,whileotherswillbeprovidedatruntimebytheapplicationserver.Usingatechniquecalleddependencyscoping,
Maven2letsyouusecertainJARsonlywhenyoureallyneedthemandexcludesthemfromtheclasspathwhenyoudon't.
在实际企业应用程序中,你可能无需包含所有的依赖项到你的部署应用程序中,有些JARs只有单元测试需要,而有些可能由应用服务器提供。使用称为依赖范围的技术,Maven
2让你使用那些你需要的JARs,而在不需要时则从classpath排除它们。
Mavenprovidesfourdependencyscopes:Maven提供了4个依赖范围
compile:Acompile-scopedependencyisavailableinallphases.This
isthedefaultvalue.编译时依赖在所有阶段都可获得,这是默认值。
provided:Aprovideddependencyisusedtocompiletheapplication,
butwillnotbedeployed.YouwouldusethisscopewhenyouexpecttheJDKorapplicationservertoprovidetheJAR.TheservletAPIsareagoodexample.Provided依赖范围用来编译应用程序,但无需部署。若你用到jdk或者应用服务器提供的JAR,则使用此范围,servlet
APIs就属于这个依赖范围。
runtime:Runtime-scopedependenciesarenotneededforcompilation,
onlyforexecution,suchasJDBC(JavaDatabaseConnectivity)drivers.运行依赖范围在编译阶段是不需要的,只有在运行时需要,比如JDBC驱动程序。
test:Test-scopedependenciesareneededonlytocompileandruntests
(JUnit,forexample).测试范围依赖,仅在编译和运行单元测试时需要(比如Junit)。
7.6.项目沟通
Animportantpartofanyprojectisinternalcommunication.Whileitisnotasilverbullet,acentralizedtechnicalprojectWebsitecangoalongwaytowardsimprovingvisibilitywithintheteam.Withminimaleffort,youcanhaveaprofessional-qualityprojectWebsiteupandrunninginverylittletime.
任何项目中,内部沟通都是一个重要部分,一个集中的项目技术网站可以显著提高开发组内的沟通能力。通过Maven2,你能够轻易的创建一个专业级的项目网站。
ThistakesawholenewdimensionwhentheMavensitegenerationisintegratedintoabuildprocessusingcontinuousintegrationorevenautomaticnightlybuilds.
AtypicalMavensitecanpublish,onadailybasis:
当Maven站点产生功能使用持续集成或者自动每日构建,集成到构建过程中后,将会带来一个全新的境界。典型的Maven站点能够每日发布:
Generalprojectinformationsuchassourcerepositories,defecttracking,teammembers,etc.一般项目信息,比如源代码库,缺失跟踪,小组成员等等。
Unittestandtestcoveragereports。单元测试以及测试覆盖报表。
AutomaticcodereviewsandwithCheckstyleandPMD。使用Checkstyle和PMD自动进行代码reviews。
Configurationandversioninginformation。配置和版本信息。
Dependencies。依赖项
Javadoc。
Sourcecodeinindexedandcross-referencedHTMLformat。索引的以及交叉引用的HTML格式的源代码。
Teammemberlist。小组成员列表。
Andmuchmore。以及其他。
Onceagain,anyMaven-savvydeveloperwillimmediatelyknowwheretolooktobecomefamiliarwithanewMaven2project.
再重申一下,任何熟悉Maven的开发人员将能够快速的熟悉另一个新的基于Maven
2的项目。
Maven2介绍(1)
7.Maven
2介绍
MavenisapopularopensourcebuildtoolforenterpriseJavaprojects,designedtotakemuchofthehardworkoutofthebuildprocess.Mavenusesadeclarativeapproach,wheretheprojectstructureandcontentsaredescribed,ratherthenthetask-basedapproachusedinAntorintraditionalmakefiles,forexample.Thishelpsenforcecompany-widedevelopmentstandardsandreducesthetimeneededtowriteandmaintain
buildscripts.
Maven是一个流行的构建企业级java项目的开源构建工具,它的设计目标是将一些构建过程中的繁重工作剥离出来。Maven使用宣告式的方法,项目的结构和内容是被描述的,而不是像使用ant或者传统make文件那样基于任务的方法。这样可以加强企业范围内的开发标准,并且减少编写和维护构建脚本的时间。
Thedeclarative,lifecycle-basedapproachusedbyMaven1is,formany,aradicaldeparturefrommoretraditionalbuildtechniques,andMaven2goesevenfurther
inthisregard.Inthisarticle,IgothroughsomeofthebasicprincipalsbehindMaven2andthenstepthroughaworkingexample.Let'sstartbyreviewingthefundamentalsofMaven2.
宣告式的,基于生命周期的方法在Maven1中就已经得到应用,在这方面,Maven
2走的更远。先来看一下Maven2的基础。
7.1.项目对象模型
TheheartofaMaven2projectistheprojectobjectmodel(orPOMforshort).Itcontainsadetaileddescriptionofyourproject,includinginformationaboutversioningandconfigurationmanagement,dependencies,applicationandtestingresources,teammembersandstructure,andmuchmore.ThePOMtakestheformofanXMLfile(pom.xml),
whichisplacedinyourprojecthomedirectory.Asimplepom.xmlfileisshownhere:
Maven2项目的核心是项目对象模型,缩写为POM。项目对象模型包括对项目的详细描述,包括项目的版本信息、配置管理、依赖、应用和测试资源、开发组成员和结构、以及更多。POM采用XML文件方式(pom.xml),放置在项目的根目录下,下面是一个pom.xml文件的示例:
<projectxmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
7.2.Maven
2项目目录结构
MuchofMaven'spowercomesfromthestandardpracticesitencourages.AdeveloperwhohaspreviouslyworkedonaMavenprojectwillimmediatelyfeelfamiliarwiththestructureandorganizationofanewone.Timeneednotbewastedreinventingdirectorystructures,conventions,andcustomizedAntbuildscriptsforeachproject.Althoughyoucanoverrideanyparticulardirectorylocationforyourownspecificends,you
reallyshouldrespectthestandardMaven2directorystructureasmuchaspossible,forseveralreasons:
Maven的魔力来自于他所鼓励的标准实践。一个经历过使用Maven开发项目的开发人员会马上对新的项目结构和组织感到熟悉。这样就无需浪费对每个项目重新熟悉目录结构,约定以及定制ANT构建脚本的时间。尽管你可以用自己的方式覆盖任何特定的目录位置,你还是应该尽量遵从标准Maven
2的目录结构:
ItmakesyourPOMfilesmallerandsimpler(使你的POM文件简短)
Itmakestheprojecteasiertounderstandandmakeslifeeasierforthepoorguywhomustmaintaintheprojectwhenyouleave(使项目易于理解)
Itmakesiteasiertointegrateplug-ins(易于集成插件)
ThestandardMaven2directorystructureisillustratedinFigure1.IntheprojecthomedirectorygoesthePOM(pom.xml)andtwosubdirectories:srcforallsource
codeandtargetforgeneratedartifacts.
标准的Maven2目录结构如图1所示。项目主目录下有一个POM(pom.xml)以及两个子目录:src放置所有的源代码,target目录放置构建产生的文件。
Figure1.ThestandardMaven2directorylayout |
src目录有一些子目录,每个子目录都有清晰定义的目的:
src/main/java:YourJavasourcecodegoeshere(strangelyenough!)。java源代码目录
src/main/resources:Otherresourcesyourapplicationneeds。应用程序所需的资源目录。
src/main/filters:Resourcefilters,intheformofpropertiesfiles,which
maybeusedtodefinevariablesonlyknownatruntime。资源过滤文件,属性文件格式,用于定义运行时所需得变量。
src/main/config:Configurationfiles。配置文件。
src/main/webapp:TheWebapplicationdirectoryforaWARproject。WAR项目的web应用程序目录
src/test/java:Unittests。单元测试源代码。
src/test/resources:Resourcestobeusedforunittests,butwillnotbedeployed。用于单元测试的资源,不会被部署。
src/test/filters:Resourcesfilterstobeusedforunittests,butwillnot
bedeployed。单元测试所用到的资源过滤,不会被部署。
src/site:FilesusedtogeneratetheMavenprojectWebsite。用于产生maven项目web站点的目录。
MAVEN指南(8)
5.2.parent
artifactId | 父项目的artifact标识符 |
groupId | 父项目的group标识符 |
version | 父项目的版本 |
relativePath | 父项目的pom.xml文件的相对路径。默认值为../pom.xml。maven首先从当前构建项目开始查找父项目的pom文件,然后从本地仓库,最有从远程仓库。RelativePath允许你选择一个不同的位置。 |
5.3.prerequisites
项目的先决条件maven | 构建此项目所需的maven的最低版本 |
5.4.issueManagement
用于管理此项目的发布跟踪(bug跟踪)。system | 构建此项目所需的maven的最低版本 |
url | 此项目使用的发布管理系统的URL。 |
5.5.ciManagement
system | 持续集成系统的名称,比如,continuum。 |
url | 此项目使用的持续集成系统的URL,如果有WEB界面的话。 |
notifiers | 配置用户信息和通知模式,当构建成功通知开发人员/用户, |
5.6.notifier
配置通知方法,当构建中断时通知用户/开发人员。Type | 投递通知的机制 |
SendOnError | 是否发送错误通知 |
SendOnFailure | 是否发送失败通知 |
SendOnSuccess | 是否发送成功通知 |
SendOnWarning | 是否发送警告通知 |
Address | Deprecated。发送通知的地址,通常为email地址 |
Configuration | 附加配置信息 |
5.7.mailingList
此项目的邮件列表。自动产生的站点将引用此信息。Name | 邮件列表的名称 |
Subscribe | 订阅此邮件列表的email地址或连接。如果是email地址,产生文档时会自动生成 |
Unsubscribe | 退订此邮件列表的email地址或连接。如果是email地址,产生文档时会自动生成 |
Post | 可以投递到此邮件列表的email地址或连接。如果是email地址,产生文档时会自动生成 |
Archive | 可以浏览到邮件列表存档信息的URL。 |
OtherArchives | 可选的替代URLs,用于浏览邮件存档信息列表 |
MAVEN指南(7)
5.1.project元素
<project>元素为描述符的根元素,以下列表列出它的所有子元素:parent | 父项目的位置,父项目中的值就是此项目中未指定的值的默认值。父项目的位置由groupID,artifactID和version指定。 |
modelVersion | 声明此POM项目描述符所遵从的版本 |
groupId | 一个全局唯一的项目标识符。通常使用完全限定的包名来和其它项目区隔(比如,org.apache.maven) |
artifactId | 在给定gorupID内唯一的产品标识符。Artifact就是由项目产生的,或者由项目所使用的东西。比如由Maven项目产生的artifacts包括:JARs,source以及二进制分发包和WARs。 |
packaging | 此项目产生的artifact的类型,比如jar,war,earpom。插件能够创建他们自己的packaging,所以,这里并没有列出所有可能的类型。 |
name | 项目的名称 |
version | 此项目产生的artifact的当前版本。 |
description | 此项目的详细描述,被maven用来在需要的地方描述项目,比如web站点。而此元素可以使用CDATA,让description里可以包含HTML标签。 |
url | 项目主页的URL。 |
prerequisites | 描述此项目构建环境的先决条件 |
issueManagement | 描述此项目的发布管理系统信息 |
ciManagement | 此项目的持续集成信息 |
inceptionYear | 此项目开始年份,用四位整数指定。当产生版权信息时使用 |
mailingLists | 项目的邮件列表信息 |
developers | 项目的开发小组 |
contributors | 项目的贡献者,但不属于开发小组 |
licenses | 本项目的许可。这里的许可是对本项目的许可,而不是对依赖项的许可。如果有多个许可,那么用户可以选择其中之一,而不必全部。 |
scm | 指定此项目使用的源代码控制系统,如CVS等等。 |
organization | 描述此项目所属的机构。 |
build | 构建此项目所需的信息。 |
profiles | 项目本地构建文件列表,如果使用,可改变默认构建过程。 |
modules | 作为本项目一部分的模块(子项目)。 |
repositories | 查找发现依赖项和扩展项的远程仓库。 |
pluginRepositories | 查找发现构建和报表所需插件的远程仓库。 |
dependencies | 此项目的所有依赖项列表。这些依赖项在构建过程中用于构造所需的classpath。这些依赖项在构建时会自动下载。 |
reports | Deprecated。Maven现在忽略此元素。 |
reporting | 此元素包含用于在Maven产生的项目站点中产生报表的插件的规格。这些报表在执行mvnsite命令时运行,所有报表出现在导航条上。 |
dependencyManagement | 所有项目的依赖信息继承于此。这一节中的依赖项并不马上解析。事实上,当一个继承于此的POM使用groupId和artifactId定义一个依赖项时,当版本信息和其他信息没有指定时,才会使用这里的依赖项定义。 |
distributionManagement | 此项目的分发信息。 |
properties | 在POM可以用作替代,以及用于资源过滤。 |
MAVEN指南(6)
2.16.配置报表
maven提供了多种报表,你可以把它们加入到你的web站点上,用于显示项目的当前状态。这些报表都通过插件获得。为了给你的站点添加报表,必须在POM的reporting节加入插件。下面的例子显示如何配置标准的项目信息报表。
<project> ... <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </plugin> </plugins> </reporting> ... |
2.17.国际化
在maven中,国际化是非常简单的。为了允许使用多个locales,按如下配置就可以了:
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <configuration> <locales>en,fr</locales> </configuration> </plugin> </plugins> ... |
为了取代默认的翻译内容,可以在站点目录下放置一个以locale的名字为名称的子目录,同时,创建该locale的站点描述符文件,比如:
+-src/ +-site/ +-apt/ |+-index.apt(Defaultversion) +-fr/ |+-apt/ ||+-index.apt(Frenchversion) +-site.xml(Defaultsitedescriptor) +-site_fr.xml(Frenchsitedescriptor) |
2.18.如何构建其它类型的项目?
注意,生命周期将会被应用到任何项目类型。比如,退回到基目录,可以创建一个简单的web应用程序。mvnarchetype:create/ -DgroupId=com.mycompany.app-DartifactId=my-webapp/ -DarchetypeArtifactId=maven-archetype-webapp</source> |
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-webapp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>my-webapp</finalName> </build> </project> |
mvncleanpackage |
2.19.如何一次构建多个项目?
Maven2.0支持多模块的概念。这里,我们显示如何构建上述的war,同时包含以前步骤的jar文件。首先,在两个项目的父目录里,需要创建一个新的父pom.xml文件,如下所示:
+-pom.xml +-my-app |+-pom.xml +-my-webapp |+-pom.xml |
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <version>1.0-SNAPSHOT</version> <artifactId>app</artifactId> <packaging>pom</packaging> <modules> <module>my-app</module> <module>my-webapp</module> </modules> </project> |
. . . <dependencies> <dependency> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> </dependency> . . . |
<project> <parent> <groupId>com.mycompany.app</groupId> <artifactId>app</artifactId> <version>1.0-SNAPSHOT</version> </parent> . . . |
mvncleaninstall |
$jartvfmy-webapp/target/my-webapp-1.0-SNAPSHOT.war 0FriJun2410:59:56EST2005META-INF/ 222FriJun2410:59:54EST2005META-INF/MANIFEST.MF 0FriJun2410:59:56EST2005META-INF/maven/ 0FriJun2410:59:56EST2005META-INF/maven/com.mycompany.app/ 0FriJun2410:59:56EST2005META-INF/maven/com.mycompany.app/my-webapp/ 3239FriJun2410:59:56EST2005META-INF/maven/com.mycompany.app/my-webapp/pom.xml 0FriJun2410:59:56EST2005WEB-INF/ 215FriJun2410:59:56EST2005WEB-INF/web.xml 123FriJun2410:59:56EST2005META-INF/maven/com.mycompany.app/my-webapp/pom.properties 52FriJun2410:59:56EST2005index.jsp 0FriJun2410:59:56EST2005WEB-INF/lib/ 2713FriJun2410:59:56EST2005WEB-INF/lib/my-app-1.0-SNAPSHOT.jar |
接着,我们告诉WAR项目需要my-appjar文件,这样,使此jar出现在了classpath上,war项目中的所有代码都可以使用它。Jar项目总是在war项目之前构建,并且告诉war插件在lib目录包含那个jar文件。
你可能注意到junit-3.8.1.jar也是一个依赖项,但却没有出现war的lib目录,因为scope元素被设为test,只有测试才需要junit-3.8.1.jar。
最后,就是在模块pom.xml文件中包含parent定义。这和maven1.0中的extend元素不同:确保POM总是能够定位,即使项目和其父分布在不同地点,只要可以通过库查找。
你可以在顶层目录,通过以下命令来产生IDEA的工作空间:
mvnidea:idea |
MAVEN指南(5)
2.10.如何把jar文件部署到自己的远程库?
<projectxmlns="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 <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>MavenQuickStartArchetype</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> <version>1.0.4</version> </dependency> </dependencies> <build> <filters> <filter>src/main/filters/filters.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> <!-- | | | --> <distributionManagement> <repository> <id>mycompany-repository</id> <name>MyCompanyRepository</name> <url>scp://repository.mycompany.com/repository/maven2</url> </repository> </distributionManagement> </project> |
<settings> . . <servers> <server> <id>mycompany-repository</id> <username>jvanzyl</username> <!--Defaultvalueis~/.ssh/id_dsa--> <privateKey>/path/to/identity</privateKey>(defaultis~/.ssh/id_dsa) <passphrase>my_key_passphrase</passphrase> </server> </servers> . . </settings> |
2.11.如何创建文档?
为了快速启用maven的文档系统,在项目尚不存在的情况下,可以使用archetype机制为你产生站点。使用如下命令:mvnarchetype:create-DgroupId=com.mycompany.app -DartifactId=my-app-DarchetypeGroupId=org.apache.maven.archetypes-DarchetypeArtifactId=maven-archetype-site |
my-app |--pom.xml `--src |--main ||--filters ||`--filters.properties ||--java ||`--com ||`--mycompany ||`--app ||`--App.java |`--resources |`--META-INF ||--application.properties |`--application.properties~ |--site ||--apt |||--format.apt ||`--index.apt ||--fml ||`--faq.fml ||--fr |||--apt ||||--format.apt |||`--index.apt |||--fml |||`--faq.fml ||`--xdoc ||`--xdoc.xml ||--site.xml ||--site_fr.xml |`--xdoc |`--xdoc.xml `--test |--java |`--com |`--mycompany |`--app |`--AppTest.java `--resources `--test.properties |
Xdoc格式,和maven1.0中一样,只是把navigation.xml杯替换成了站点表述符。
APT格式,基本为文本格式,类似于wiki格式,可以用来编写简单的,结构画的文档。请参考《APT格式》。
FML格式,FAQ格式,在maven1.0中也有。
还有其它格式。
Maven支持几种输出格式,但对于2.0,只支持XHTML格式。
2.12.如何创建站点?
使用如下命令:mvnsite |
2.13.如何部署站点?
为了部署站点,必须首先在pom.xml文件中定义一个分发位置。<distributionManagement> <site> <id>website</id> <url>scp://www.mycompany.com/www/docs/project/</url> </site> </distributionManagement> |
部署站点使用site-deploygoal。
mvnsite-deploy |
2.14.创建站点描述符
site.xml文件用来描述站点的布局。示例:
<?xmlversion="1.0"encoding="ISO-8859-1"?> <projectname="Maven"> <bannerLeft> <name>Maven</name> <src>http://maven.apache.org/images/apache-maven-project.png</src> <href>http://maven.apache.org/</href> </bannerLeft> <bannerRight> <src>http://maven.apache.org/images/maven-small.gif</src> </bannerRight> <body> <links> <itemname="Apache"href="http://www.apache.org/"/> <itemname="Maven1.0"href="http://maven.apache.org/"/> <itemname="Maven2"href="http://maven.apache.org/maven2/"/> </links> <menuname="Maven2.0"> <itemname="Introduction"href="index.html"/> <itemname="Download"href="download.html"/> <itemname="ReleaseNotes"href="release-notes.html"/> <itemname="GeneralInformation"href="about.html"/> <itemname="ForMaven1.0Users"href="maven1.html"/> <itemname="RoadMap"href="roadmap.html"/> </menu> ${reports} ... </body> </project> |
2.15.添加额外的资源
可以在站点的资源目录包含任意的资源,比如增加CSS+-src/ +-site/ +-resources/ +-css/ |+-site.css | +-images/ +-pic1.jpg |
文件pic1.jpg可以在站点中的任何页面引用。
2.8.如何过滤资源文件?
有时候,资源文件中的某些值在构建时才确定,为了在maven中做到这点,使用${<propertyname>}语法将属性引用放到资源文件中。这个属性的值可以在你的pom.xml文件中定义,或者在用户的settings.xml文件中,或者在外部属性文件中,或者系统属性。为了让maven过滤资源,只要在pom.xml的resource节将filtering设为true。
<projectxmlns="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 <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>MavenQuickStartArchetype</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> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project> |
为了引用在pom.xml中定义的属性,属性名称使用XML元素来定义值,“pom”允许作为项目根元素的别名。所以${pom.name}指向项目的名称,而${pom.version}指向项目的版本,${pom.build.finalName}指向最后创建的打包文件的名称,等等。注意,某些POM元素有默认值,所以没有必要在pom.xml文件中显式定义。同样的,在用户settings.xml文件中的值可以通过以“settings”开头的属性名字来引用(比如,${settings.localRepository}指向用户本地库的路径)。
为了继续我们的例子,在application.properties文件中加入一对属性
#application.properties application.name=${pom.name} application.version=${pom.version} |
mvnprocess_resources |
#filter.properties my.filter.value=hello! |
<projectxmlns="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 <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>MavenQuickStartArchetype</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> <build> <filters> <filter>src/main/filters/filter.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project> |
#application.properties application.name=${pom.name} application.version=${pom.version} message=${my.filter.value} |
<projectxmlns="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 <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>MavenQuickStartArchetype</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> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> <properties> <my.filter.value>hello</my.filter.value> </properties> </project> |
#application.properties java.version=${java.version} command.line.prop=${command.line.prop} |
mvnprocess-resources"-Dcommand.line.prop=helloagain" |
2.9.如何使用外部依赖项?
你可能已经注意到在示例中的POM中已经使用了dependencies元素。更全面的介绍,请参考《依赖项管理介绍》。Pom.xml文件中的dependencies节列出了所有构建时需要的外部依赖项(不管是编译时、测试时、运行时)。示例中,目前只用到了Junit。
<projectxmlns="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 <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>MavenQuickStartArchetype</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> |
对于更多的依赖项机制,请参考《依赖项管理》。
通过此dependency信息,maven可以在构建项目时引用此依赖项。那么,maven从哪儿引用此依赖项呢?Maven会去本地库(默认位置:~/.m2/repository)中查找所有依赖项。在上一节,我们把我们项目的产品(my-app-1.0-SNAPSHOT.jar)安装到了本地库中,一旦安装完成,其它项目就可以把此jar文件作为一个依赖项引用:
<project> <groupId>com.mycompany.app</groupId> <artifactId>my-other-app</artifactId> ... <dependencies> ... <dependency> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> </project> |
让我们增加一个依赖项到我们的项目中,假设我们需要日志功能,必须增加log4j作为依赖项。首先,我们需要知道log4j的groupId,artifactId以及version。
我们可以通过浏览ibiblio来查看它,或者使用Google来搜索“site:www.ibiblio.orgmaven2log4j”。这个搜索会显示一个称为“/maven2/log4j/log4j”或者“/pub/packages/maven2/log4j/log4j”的目录。在那个目录中,有一个名为maven-metadata.xml的文件,这里是那个文件的内容:
<metadata> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.1.3</version> <versioning> <versions> <version>1.1.3</version> <version>1.2.4</version> <version>1.2.5</version> <version>1.2.6</version> <version>1.2.7</version> <version>1.2.8</version> <version>1.2.11</version> <version>1.2.9</version> <version>1.2.12</version> </versions> </versioning> </metadata> |
现在,我们知道了所需的信息,可以在我们的pom.xml增加依赖项了,如下所示:
<projectxmlns="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 <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>MavenQuickStartArchetype</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> <scope>compile</scope> </dependency> </dependencies> </project> |
MAVEN指南(3)
2.5.如何创建JAR以及把它安装到本地库中?
执行以下命令:mvnpackage |
现在,你想将产生的jar文件安装到你的本地库中(~/.m2/repository是默认的位置)。想了解库的更多信息,请参考《库介绍》。执行以下命令,就能安装到本地库:
mvninstall |
[INFO]---------------------------------------------------------------------------- [INFO]BuildingMavenQuickStartArchetype [INFO]task-segment:[install] [INFO]---------------------------------------------------------------------------- [INFO][resources:resources] [INFO][compiler:compile] Compiling1sourcefileto<dir>/my-app/target/classes [INFO][resources:testResources] [INFO][compiler:testCompile] Compiling1sourcefileto<dir>/my-app/target/test-classes [INFO][surefire:test] [INFO]Settingreportsdir:<dir>/my-app/target/surefire-reports ------------------------------------------------------- TESTS ------------------------------------------------------- [surefire]Runningcom.mycompany.app.AppTest [surefire]Testsrun:1,Failures:0,Errors:0,Timeelapsed:0.001sec Results: [surefire]Testsrun:1,Failures:0,Errors:0 [INFO][jar:jar] [INFO]Buildingjar:<dir>/my-app/target/my-app-1.0-SNAPSHOT.jar [INFO][install:install] [INFO]Installing<dir>/my-app/target/my-app-1.0-SNAPSHOT.jarto<local-repository>/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.jar [INFO]---------------------------------------------------------------------------- [INFO]BUILDSUCCESSFUL [INFO]---------------------------------------------------------------------------- [INFO]Totaltime:5seconds [INFO]Finishedat:TueOct0413:20:32GMT-05:002005 [INFO]FinalMemory:3M/8M [INFO]---------------------------------------------------------------------------- |
Ø**/*Test.java
Ø**/Test*.java
Ø**/*TestCase.jave
默认情况下,被排除的:
Ø**/Abstract*Test.java
Ø**/Abstract*TestCase.jave
到目前为止,已经经历了安装、构建、测试、打包、安装典型的maven项目等步骤。这可能是大多数项目使用maven需要经历的步骤,而驱动的项目模型文件只有18行内容,对比典型的build文件,它的行数比pom多的多。
不需任何附加工作maven依靠此POM就可以为你的项目产生一个网站,你只须定制你的maven网站,如果时间紧迫,你只须执行以下命令:
mvnsite |
mvnclean |
可能你需要为你的项目产生一个IntelliJIDEA描述符,执行以下命令:
mvnidea:idea |
2.6.如何使用插件?
Maven1.0用户注意:在Maven1.0中,你可能必须加入一些譬如preGoal到maven.xml中,以及project.properties的一些入口中。这里有些不同,比如,我们配置java编译器,以允许编译jdk5.0的源码,这只要简单的在你的POM中加入:. <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> . . |
Configuration元素用来对编译插件的每个goal提供参数。具体请参考《插件列表》、《构建生命周期介绍》。
2.7.如何在jar文件中加入资源?
在示例应用程序中,增加目录${basedir}/src/main/resources,我们把资源文件放入此目录。任何在${basedir}/src/main/resources目录中的文件和目录都将被打包到jar文件中。my-app |--pom.xml `--src |--main ||--java ||`--com ||`--mycompany ||`--app ||`--App.java |`--resources |`--META-INF |`--application.properties `--test `--java `--com `--mycompany `--app `--AppTest.java |
|--META-INF ||--MANIFEST.MF ||--application.properties |`--maven |`--com.mycompany.app |`--my-app ||--pom.properties |`--pom.xml `--com `--mycompany `--app `--App.class |
API来做到,属性文件pom.properties如下所示:
#GeneratedbyMaven #TueOct0415:43:21GMT-05:002005 version=1.0-SNAPSHOT groupId=com.mycompany.app artifactId=my-app |
my-app |--pom.xml `--src |--main ||--java ||`--com ||`--mycompany ||`--app ||`--App.java |`--resources |`--META-INF ||--application.properties `--test |--java |`--com |`--mycompany |`--app |`--AppTest.java `--resources `--test.properties |
... //Retrieveresource InputStreamis=getClass().getResourceAsStream("/test.properties"); //Dosomethingwiththeresource ... |
开始使用Maven
2.1.Maven是什么?
Maven本质上是一个项目管理工具,Maven提供一下特性来提供项目管理:Ø构建
Ø文档
Ø报表
Ø依赖(相关性)
ØSCMs
Ø发布
Ø分发
Maven不只是一个项目构建工具,并且也不只是ant的一个替代品。Maven是和ant完全不同的一个东西。Ant仅仅是一个简单的跨平台的构建工具,而Maven是一个模式应用,为了达到可视性、复用性、维护性和易于理解的一个下部构造(Maven
isabouttheapplicationofpatternsinordertoacheiveaninfrastructurewhichdisplaysthecharacteristicsofvisibility,reusability,maintainability,andcomprehensibility.)。
2.2.如何开始你的第一个Maven项目
这里要使用Maven的achetype机制,一个achetype被定义成一个原始的模式或者模型。在Maven里,archetype是一个项目的模板,通过这个模板,再加上一些用户的输入就产生了一个可以工作的、符合用户需要的Maven项目。下面开始创建第一个项目,执行一下命令行:
Mvnarchetype:create–DgroupId=com.mycompany.app–DartifactId=my-app |
<projectxmlns="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 <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>MavenQuickStartArchetype</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> |
下面是pom中的一些重要元素:
Øproject:pom.xml文件中的顶层元素;
ØmodelVersion:指明POM使用的对象模型的版本。这个值很少改动。
ØgroupId:指明创建项目的组织或者小组的唯一标识。GroupId是项目的关键标识,典型的,此标识以组织的完全限定名来定义。比如,org.apache.maven.plugins是所有Maven插件项目指定的groupId。
ØartifactId:指明此项目产生的主要产品的基本名称。项目的主要产品通常为一个JAR文件。第二,象源代码包通常使用artifactId作为最后名称的一部分。典型的产品名称使用这个格式:<artifactId>-<version>.<extension>(比如:myapp-1.0.jar)。
Øversion:项目产品的版本号。Maven帮助你管理版本,可以经常看到SNAPSHOT这个版本,表明项目处于开发阶段。
Øname:项目的显示名称,通常用于maven产生的文档中。
Øurl:指定项目站点,通常用于maven产生的文档中。
Ødescription:描述此项目,通常用于maven产生的文档中。
POM中所有元素的说明,请参考《POM参考》。
产生了第一个项目的archetype后,你可以发现maven为你创建了如下的目录结构:
my-app |--pom.xml `--src |--main |`--java |`--com |`--mycompany |`--app |`--App.java `--test `--java `--com `--mycompany `--app `--AppTest.java |
如果你想改变这个默认布局,请参考《标准目录布局介绍》。
2.3.如何编译应用程序源码?
执行一下命令编译应用程序源码:mvncompile |
[INFO]---------------------------------------------------------------------------- [INFO]BuildingMavenQuickStartArchetype [INFO]task-segment:[compile] [INFO]---------------------------------------------------------------------------- [INFO]artifactorg.apache.maven.plugins:maven-resources-plugin:checkingforupdatesfromcentral ... [INFO]artifactorg.apache.maven.plugins:maven-compiler-plugin:checkingforupdatesfromcentral ... [INFO][resources:resources] ... [INFO][compiler:compile] Compiling1sourcefileto<dir>/my-app/target/classes [INFO]---------------------------------------------------------------------------- [INFO]BUILDSUCCESSFUL [INFO]---------------------------------------------------------------------------- [INFO]Totaltime:3minutes54seconds [INFO]Finishedat:FriSep2315:48:34GMT-05:002005 [INFO]FinalMemory:2M/6M [INFO]---------------------------------------------------------------------------- |
就像你从输出中看到的那样,编译过的类文件被存放到${basedir}/target/classes目录中,这个也是Maven标准的目录布局。
2.4.如何编译测试源码和运行单元测试?
运行一下命令:mvntest |
[INFO]---------------------------------------------------------------------------- [INFO]BuildingMavenQuickStartArchetype [INFO]task-segment:[test] [INFO]---------------------------------------------------------------------------- [INFO]artifactorg.apache.maven.plugins:maven-surefire-plugin:checkingforupdatesfromcentral ... [INFO][resources:resources] [INFO][compiler:compile] [INFO]Nothingtocompile-allclassesareuptodate [INFO][resources:testResources] [INFO][compiler:testCompile] Compiling1sourcefiletoC:/Test/Maven2/test/my-app/target/test-classes ... [INFO][surefire:test] [INFO]Settingreportsdir:C:/Test/Maven2/test/my-app/target/surefire-reports ------------------------------------------------------- TESTS ------------------------------------------------------- [surefire]Runningcom.mycompany.app.AppTest [surefire]Testsrun:1,Failures:0,Errors:0,Timeelapsed:0sec Results: [surefire]Testsrun:1,Failures:0,Errors:0 [INFO]---------------------------------------------------------------------------- [INFO]BUILDSUCCESSFUL [INFO]---------------------------------------------------------------------------- [INFO]Totaltime:15seconds [INFO]Finishedat:ThuOct0608:12:17MDT2005 [INFO]FinalMemory:2M/8M [INFO]---------------------------------------------------------------------------- |
Ø这个时候,maven会下载更多的依赖项。这些依赖项和插件是运行测试时所需要的。
Ø在编译和执行测试之前,maven按需编译main代码
如果你只须编译测试代码(无需运行测试),执行以下代码:
mvntest-compile |
MAVEN指南(1)
1.下载并安装
目前的版本为2.0.2,从Mirrors | Checksum | Signature | |
Maven2.0.2(tar.bz2) | |||
Maven2.0.2(tar.gz) | |||
Maven2.0.2(zip) | |||
Maven2.0.2TasksforAnt | |||
Maven2.0.2Embedder |
Jdk | 1.4+ |
内存 | |
磁盘空间 | 大概100M |
操作系统 |
在Windows2000/XP下:
Ø解压maven-2.0.2-bin.zip到相应的安装目录,以下假设目录为C:/Program
Files/ApacheSoftwareFoundation/maven-2.0.2。
Ø设置环境变量M2_HOME=
C:/ProgramFiles/ApacheSoftwareFoundation/maven-2.0.2,同时,将MAVEN的bin路径加到path环境变量中,PATH=%PATH%;%M2_HOME%/bin
Ø运行mvn
--version,来验证安装。
在Unix系列操作系统下(Linux,SolarisandMacOSX):
Ø将分发包解压到安装路径下,假设为/usr/local/maven-2.0.2
Ø将bin目录加入到path环境变量,比如:export
PATH=/usr/local/maven-2.0.2y/bin:$PATH
Ø运行mvn
–version来验证安装。
ApacheMaven是一个软件项目管理和综合工具。基于项目对象模型(POM)的概念,Maven可以从一个中心资料片管理项目构建,报告和文件。
本教程将介绍如何使用Maven在Java开发,或任何其他的编程语言的任何项目。
本教程是为初学者准备的,帮助他们了解Maven工具的基本功能。完成本教程后,希望能够熟悉使用ApacheMaven。
假设需要使用Maven来处理企业级Java项目开发。如果有软件开发的知识,如:JavaSE,JavaEE的开发和部署过程经验,对学Maven将有一定的帮助。
Maven概述,Maven是什么?
Maven环境配置
MavenPOM
Maven构建生命周期
Maven构建配置文件
Maven存储库
Maven插件
Maven创建Java项目
使用Maven构建和测试Java项目
Maven外部依赖
Maven项目文档
Maven项目模板
Maven快照
Maven构建自动化-Hudson
Maven依赖管理
Maven自动化部署
MavenWeb应用
EclispeIDE集成Maven
NetBeansIDE集成Maven
另附几个学习Maven不错的网址:
---------------------------------------------------
1、Maven官方网站
2、Maven中文教程
3、ApacheMaven入门
相关文章推荐
- 在Eclipse中使用Maven 2.x指南
- 在Eclipse中使用Maven 2.x指南
- 在Eclipse中使用Maven 2.x指南
- 在Eclipse中使用Maven 2.x指南
- 在Eclipse中使用Maven 2.x指南
- 在Eclipse中使用Maven 2.x指南
- 在Eclipse中使用Maven 2.x指南
- Eclipse快速上手指南之使用JUnit
- 实例教您Eclipse快速上手指南之使用ANT
- Eclipse快速上手指南之使用Ant
- Eclipse快速上手指南之使用ANT - [转贴]
- Eclipse快速上手指南之使用CVS
- Eclipse快速上手指南之使用Ant
- Eclipse快速上手指南之使用CVS
- Eclipse快速上手指南之使用JUnit
- Eclipse快速上手指南之使用ANT
- Eclipse快速上手指南之使用ANT
- Eclipse入门之使用指南及开发Eclipse插件
- Eclipse快速上手指南之使用Ant
- Eclipse入门—使用指南及开发Eclipse插件