I want to fly higher
programming Explorer
posts - 114,comments - 263,trackbacks - 0

1.http://maven.apache.org/
     1.下载apache-maven-3.1.1-bin.zip,解压至C盘根目录
     2.添加环境变量至path:C:\apache-maven-3.1.1\bin
     3.cmd命令下输入:mvn -version/mvn-v
        Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 23:22:2
2+0800)
        Maven home: C:\apache-maven-3.1.1\bin\..
        Java version: 1.7.0_51, vendor: Oracle Corporation
        Java home: C:\Program Files\Java\jdk1.7.0_51\jre
        Default locale: zh_CN, platform encoding: GBK
        OS name: "windows xp", version: "5.1", arch: "x86", family: "windows"

2.mvn -help
 usage: mvn [options] [<goal(s)>] [<phase(s)>]

3.maven项目结构
     ${basedir} 存放 pom.xml(Project Object Model)和所有的子目录
     ${basedir}/src/main/java 项目的 java源代码
     ${basedir}/src/main/resources 项目的资源,比如说 property文件
     ${basedir}/src/test/java 项目的测试类,比如说 JUnit代码
     ${basedir}/src/test/resources 测试使用的资源

     编译后 的 classes 会放在 ${basedir}/target/classes 下面, JAR会放在 ${basedir}/target
    -->
惯例优于配置

4.Maven仓库
     1.仓库分类:本地仓库和远程仓库。Maven根据坐标寻找构件的时候,它先会查看本地仓库,如果本地仓库存在构件,则直接使用;如果没有,则从远程仓库查找,找到后,下载到本地
     2.默认情况下,每个用户在自己的用户目录下都有一个路径名为.m2/repository/的仓库目录。可以自定义本地仓库的地址.修改路径时,先将$M2_HOME/conf/settings.xml文件复制到~/.m2/settings.xml,再对后者进行编辑,设置localRepository元素的值为想要的仓库地址
     3.远程中央仓库的地址为 http://repo1.maven.org/
     4.私服/镜像
     5.settings.xml

5.POM
     1.Maven坐标
          1.groupId:组织标识.如com.landon,在m2_repository目录下:com/landon
          2.artifactId:项目名称,如mavs,在m2_repository目录下:com/landon/mavs
          3.version:版本号,如1.0.0,在m2_repository目录下:com/landon/mavs/1.0.0
          4.packaging:打包的格式,如jar,war

     2.依赖关系dependencies(项目中依赖的jar)
          dependency属性
          1.groupId/artifactId/version 依赖的具体工程
          2.scope 依赖范围 compile/test/runtime/provided/system
          3.optional 去除依赖传递
          4.exclusions 排除依赖

     3.聚合关系
          1.通过一个父模块将所有的要构建模块整合起来,将父模块的打包类型声明为 POM,通过 <modules> 将各模块集中到父 POM.<module></module> 中间的内容为子模块工程名的相对路径.
          2.Maven 会首先解析聚合模块的 POM 文件,分析要构建的模块,并通过各模块的依赖关系计算出模块的执行顺序,根据这个潜在的关系依次构建模块。将各子模块聚合到父模块中后,我们就可以对父模块进行一次构建命令来完成全部模块的构建

     4.继承关系
          1.通过构建父模块将子模块共用的依赖,插件等进行统一声明
          2.parent

     5.插件
          1.Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven-compiler-plugin完成的
          2.每个任务对应了一个插件目标(goal),每个插件会有一个或者多个目标,例如maven-compiler-plugin的compile目标用来编译位于src/main/java/目录下的主源码,testCompile目标用来编译位于src/test/java/目录下的测试源码
          3.maven-antrun-plugin能让用户在Maven项目中运行Ant任务
          4.plugins/plugin

     6.构建
          1.build
          2.结合plugins/resources
          3.profile(多环境构建)

6.maven属性(和ant差不多)
     1.内置属性。这种属性跟 Maven Project 自身有关,比如要引入当前 Project 的版本信 息,那么只需要在使用的位置引用 ${version} 就行了。 
     2.Setting 属性。上文中已经提到 Maven 自身有一个 settings.xml 配置文件,它里面含有包括仓库,代理服务器等一些配置信息,利用 ${settings.somename} 就可以得到文件里相应元素的值。 
     3.POM 属性。这种属性对应 POM 文件中对应元素的值,例如 ${project.groupId} 对应了 <groupId></groupId> 中的值,${project.artifactId} 对应了 <artifactId> </ artifactId > 中的值。
     4.系统环境变量。可以使用 env.${name} 来获得相应 name 对应的环境变量的值,例如 ${env.JAVA_HOME} 得到的就是 JAVA_HOME 的环境变量值。 
     5.用户自定义变量。这种类型的变量是使用最频繁和广泛的变量,完全由用户自己定义。在 POM 文件中加入 <properties> 元素并将自定义属性作为其子元素

7.生命周期和阶段
    Maven有三套相互独立的生命周期,分别是clean、default和site。每个生命周期包含一些阶段(phase),阶段是有顺序的,后面的阶段依赖于前面的阶段。

    a、clean生命周期:清理项目,包含三个phase。

        1)pre-clean:执行清理前需要完成的工作
       2)clean:清理上一次构建生成的文件
       3)post-clean:执行清理后需要完成的工作

    b、default生命周期:构建项目,重要的phase如下。

        1)validate:验证工程是否正确,所有需要的资源是否可用。
        2)compile:编译项目的源代码。  
        3)test:使用合适的单元测试框架来测试已编译的源代码。这些测试不需要已打包和布署。
        4)Package:把已编译的代码打包成可发布的格式,比如jar。
        5)integration-test:如有需要,将包处理和发布到一个能够进行集成测试的环境。
        6)verify:运行所有检查,验证包是否有效且达到质量标准。
        7)install:把包安装到maven本地仓库,可以被其他工程作为依赖来使用。
        8)Deploy:在集成或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享。

    c、site生命周期:建立和发布项目站点,phase如下

    举例如下:

        1、mvn clean
            调用clean生命周期的clean阶段,实际执行pre-clean和clean阶段

        2、mvn test
            调用default生命周期的test阶段,实际执行test以及之前所有阶段

        3、mvn clean install
            调用clean生命周期的clean阶段和default的install阶段,实际执行pre-clean和clean,install以及之前所有阶段

build lifecycle & build phase & goal

maven有一套build的生命周期,是按照一套顺序走下来的,这一套顺序就叫一个生命周期(lifecycle)。maven内置三种生命周期:default, clean 和 site。一个生命周期分为多个build phase,下面是default生命周期全部的build phase:

  • validatevalidate the project is correct and all necessary information is available.
  • initializeinitialize build state, e.g. set properties or create directories.
  • generate-sourcesgenerate any source code for inclusion in compilation.
  • process-sourcesprocess the source code, for example to filter any values.
  • generate-resourcesgenerate resources for inclusion in the package.
  • process-resourcescopy and process the resources into the destination directory, ready for packaging.
  • compilecompile the source code of the project.
  • process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
  • generate-test-sourcesgenerate any test source code for inclusion in compilation.
  • process-test-sourcesprocess the test source code, for example to filter any values.
  • generate-test-resourcescreate resources for testing.
  • process-test-resourcescopy and process the resources into the test destination directory.
  • test-compilecompile the test source code into the test destination directory
  • process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
  • testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
  • prepare-packageperform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
  • packagetake the compiled code and package it in its distributable format, such as a JAR.
  • pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment.
  • integration-testprocess and deploy the package if necessary into an environment where integration tests can be run.
  • post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment.
  • verifyrun any checks to verify the package is valid and meets quality criteria.
  • installinstall the package into the local repository, for use as a dependency in other projects locally.
  • deploydone in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

这些build phase是按照顺序执行的,如果执行后面的build phase,前面的build phase 也会被执行。例如如果执行:

mvn deploy

前面的install、verify一直到validate这些build phase都会执行。

每一个build phase是由goal组成的,一个goal其实就是一个任务,一个goal可以关联到一个build phase也可以不关联到任何build phase 。 不关联到任何phase的goal是可以独立执行的,例如:

mvn clean dependency:copy-dependencies package

上面的命令会导致先执行clean这个phase,然后拷贝依赖项,最后打包。注意,这里clean这个goal是clean这个lifecycle里面的一个goal,所以可以看到不同lifecycle的build phase和goal是可以混合在一起执行的。 如果一个goal被绑定到多个phase上,那么goal就会被执行多次。

phase的顺序是已经固定的,如果一个phase没有绑定到任何goal,那么phase就不会被执行。 一个goal可以通过两种方式绑定到一个phase,一个是指定packaging,另一个就是plugin。

 

packaging&plugin

plugin就是用来向maven提供goal的。一个plugin里面可以有多个goal,这就是为什么我们在指明goal时,前面会用一个冒号与plugin的名字。

一个plugin自己可以指定自己的goal绑定到哪个lifecycle的哪一个Phase上,另外也可以配置一个goal绑定到哪个phase上。可以在pom.xml里面配置。 看两个配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugin>
   <groupId>org.codehaus.modello</groupId>
   <artifactId>modello-maven-plugin</artifactId>
   <version>1.4</version>
   <executions>
     <execution>
       <configuration>
         <models>
           <model>src/main/mdo/maven.mdo</model>
         </models>
         <version>4.0.0</version>
       </configuration>
       <goals>
         <goal>java</goal>
       </goals>
     </execution>
   </executions>
 </plugin>

这个就在当前的lifecycle里面添加了一个名字叫java的goal,这goal会根据自己的配置去绑定到一个phase,在phase执行的时候这个goal会执行。并且在这个配置里面,可以指定多个execution来让这个goal执行多次。

看另一个示例配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
   <groupId>com.mycompany.example</groupId>
   <artifactId>display-maven-plugin</artifactId>
   <version>1.0</version>
   <executions>
     <execution>
       <phase>process-test-resources</phase>
       <goals>
         <goal>time</goal>
       </goals>
     </execution>
   </executions>
 </plugin>

这个名为time的goal把自己绑定到了process-test-resource这个phase上。

在默认情况下,并不是所有的phase都绑定了goal,比如clean这个lifecycle是有三个phase的,但是只有其中的一个名为clean的phase默认绑定了一个clean:clean goal,其它两个phase默认没有绑定任何goal。

之前已经提到过packaging,在pom.xml可以指定packaging,每种packaging都设定了一组phase和goal之间的绑定关系。在default lifecycle下,当packaging为 ejb/ejb3/jar/par/rar/war 其中之一的值的时候,只有以下的phase绑定了goal,具体如下:

process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packagejar:jar
installinstall:install
deploydeploy:deploy

 

总结

首先搞清楚maven的project的目录结构,然后理解maven的lifecycle,lifecycle是由build phase组成,每一个build phase会绑定到goal。goal是由plugin提供的。 每一种packaging的值都表明了一定的phase和goal之间的绑定关系。

另外一个很重要的就是dependency,我们要在项目中引用一个依赖,只需要在pom.xml指定依赖的名字和版本,maven会自动去远程的repository下载,然后放到本地的repository里面,这样以后所有的project都可以共用

其它细节可以参考http://maven.apache.org/guides/index.html

8.查找依赖jar
     http://search.maven.org/
     你懂得

9.参考:
 1.http://maven.apache.org/settings.html
 2.http://maven.apache.org/pom.html
 3.http://maven.apache.org/guides/index.html

posted on 2014-02-03 00:02 landon 阅读(2170) 评论(2)  编辑  收藏 所属分类: Utils

FeedBack:
# re: Apache Maven基础
2014-02-03 13:37 | Unmi
同时学习 Maven 和 Ant 啊,那时候一提到 XP 就会想到 Ant
我概念中的 Ant 是面向过程的,Maven 是面向对象的。  回复  更多评论
  
# re: Apache Maven基础
2014-02-03 14:34 | landon
下一篇就是Gradle@Unmi
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: