说明:文章转自http://www.cnblogs.com/JeffreySun/archive/2013/03/14/2960573.html

创建project

先去官方网站下载一个最新版本http://maven.apache.org/download.cgi. 下载后解压,使用之前最好先将maven的bin目录设置到path环境变量里面。

maven无非也就是用来build一个project的,直接先上一个例子,在命令行下输入下面的命令:

mvn archetype:generate DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=myapp

mvn就是maven的命令行程序,archetype:generate中的archetype是plugin的名字,generate是goal的名字,命令行后面的是一些参数。关于archetype和goal以及后面的参数,后面再细说。

如果是第一次运行,这个过程会有点慢,maven需要下载一些依赖项,中间如果有输入提示信息,直接回车使用默认值就可以了。这条命令执行完后,会在你的当前目录下生成一个名为myapp的目录:

注意这个目录结构,src/main/java 和 src/test/java 是不能改动,不然maven会无法找到源文件。下面是maven一个标准的目录结构:

src/main/java Application/Library sources
src/main/resources Application/Library resources
src/main/filters Resource filter files
src/main/assembly Assembly descriptors
src/main/config Configuration files
src/main/scripts Application/Library scripts
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/site Site

另外maven还生成了一个重要的文件pom.xml,maven就是通过这个文件来来管理整个project,可以理解位类似于eclipse的.project文件。默认生成的pom.xml文件的内容如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* 1-1 */
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1.1.0.1</version>
    <packaging>jar</packaging>
    <name>myapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

解释一下这个xml文件的内容:

  • modelVersion: 这个XML文件所使用的POM格式的版本
  • groupId: 相当于这个project的所有者或者机构的一个标识,一般是com.company.xxx这种格式
  • artifactId:  这个project最后所生成的文档(jar、war)的名字,比如对于junit这个开源的project,它的artifactId就是junit
  • packaging: 这个project的打包的类型,一般是war、jar等值
  • version: project的版本
  • name: project的名字,生成文档等内容的时候会用的这个名字

这个project创建好后和普通的project没有什么不同,我们直接往里面放源代码进行开发就可以了,如果有目录想修改的也完全可以。

POM & archetype

archetype就是一个project的模板,上面我们生成的project就是用默认的archetype生成的。如果使用不同的archetype,生成的project结构会有所不同。 一个archetype指明了

  • 1) 项目的目录结构以及什么目录是放source code,哪些是放test source code,哪些目录是放resource的。
  • 2) 一个默认的pom.xml文件,这个默认的pom.xml文件中的groupId,artifactId,version用占位符表示,在创建project的时候通过参数传进来。

pom.xml文件的POM全称是Project Object Model,这个文件对于maven的使用者来说是一个和maven交互的渠道,pom.xml包含了一个maven project的配置,一个project该如何编译打包,project有哪些依赖项等等。

仔细看一下我们前面创建project的命令:mvn archetype:generate DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=myapp

  1. 1) archetype:generate, 这是一个maven的plugin,用来从一个archetype创建一个project,关于plugin后面再说。
  2. 2) DarchetypeGroupId,这个就是指的archetype的groupid,也就是说是用的哪个archetype,或者说用哪个项目模板。
  3. 3) 后面的两个参数,用来放大pom.xml文件里面,作为当前创建的project的描述信息。

Project创建好了,看如何去编译,直接进入的project的目录,在命令行下:

mvn compile

编译完后maven会创建一个target目录去保存编译结果。 我们需要编译成一个什么样的内容,以及要输出到什么地方等等,都是可以在pom.xml文件里面配置的,但是因为我们目前并没有指定这些内容,所以maven会使用默认值。

我们还可以用maven执行test:

mvn test

第一次执行时,maven会去下载一些依赖项。另外要注意的时,如果我们更改了默认的目录结构,maven会因为找bu到test而无法去执行test。如果只需要编译test可以执行:

mvn test-compile

要把项目打包,执行:

mvn package

mvn会根据pom.xml里面的packaging选项打包成相应的文件。

repository & dependency

maven里面有一个repository的概念,当我们的项目依赖于某个jar时,maven会去repository里面去找。repository分两种,一种是远程的,一种是本地的。如果有几个project都用到junit,我们可以把junit放在repository里面,几个project可以公用,节约存储空间而且方便管理,这个repository的位置可以在pom.xml里面设置。

本地的默认的路径是安装用户的目录下的 .m2\repository 文件夹。如果一个依赖项在本地的repository里面没有,那么maven会去他自己的远程的repository http://repo.maven.apache.org/maven2 去下载后放到本地的repository里面。

也就是说,我们如果我们的project需要要引用一个依赖项,我们只需要在pom.xml文件中进行配置,maven会自动帮我们去引用。 我们之前的创建project里面需要写单元测试,引用到了junit,看pom中的配置:

1
2
3
4
5
6
7
8
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

每一个需要为每一个 dependency 指明groupId,artifactId,version。scope很简单,意思是说我们需要怎么引用,比如我们上面的例子里面设置的是test,意思是说只在test里面引用junit。 但是我们如何知道groupId,artifactId和version呢? 比如我现在想引用log4j,这个几个值怎么填? 可以去http://mirrors.ibiblio.org/maven2/ 上去查找。比如log4j,我们就在上面这个地址加上log4j,也就是http://mirrors.ibiblio.org/maven2/junit/。进去后会有一个maven-metadata.xml,打开就可以知道这些值了然后添加这个dependency了。

如果要把一个project安装到本地的repository里面,可以执行下面的命令:

mvn install

到这里就说完了创建,编译,测试,打包以及安装,大部分的项目也就是做这些事情。

再介绍几个其它命令:

  1. mvn site : 为你的project创建一个站点
  2. mvn clean: 清除target目录下的所有文件
  3. mvn eclipse:eclipse :为project生成eclipse的工程文件和classpath文件

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-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar
install install:install
deploy deploy: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

Maven使用(转)的更多相关文章

  1. 【分享】标准springMVC+mybatis项目maven搭建最精简教程

    文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...

  2. 理解Maven中的SNAPSHOT版本和正式版本

    Maven中建立的依赖管理方式基本已成为Java语言依赖管理的事实标准,Maven的替代者Gradle也基本沿用了Maven的依赖管理机制.在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个 ...

  3. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  4. 安装eclipse的maven插件

    我们团队用maven来管理项目需要的库文件,其实以前都没听过maven,第一次接触这个,师兄要我直接去装下这个,开始以为还挺简单的,没想到中间遇到了一些小麻烦,现在把我成功安装maven的过程分享下, ...

  5. MAVEN学习-第一个Maven项目的构建

    MAVEN安装成功之后就可以进行项目的构建和管理了: 为什么要用maven进行项目的构建和管理? 对于初学者来说一个最直接的也是最容易里的优点在于JAR包的管理,相对于以前开发一个项目的时候我们需要用 ...

  6. Maven 代理设置

    在maven的安装目录下 %MAVEN_HOME%/conf/setting.xml 中进行设置 <proxies>    <!-- proxy     | Specificatio ...

  7. spring maven pom.xml设置

    spring pom.xml设置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  8. maven依赖查询地址

    http://search.maven.org/#search%7Cga%7C1%7C

  9. maven 中snapshot版本和release版本的区别

    maven中的仓库分为两种,snapshot快照仓库和release发布仓库.snapshot快照仓库用于保存开发过程中的不稳定版本,release正式仓库则是用来保存稳定的发行版本.定义一个组件/模 ...

  10. Maven多模块,Dubbo分布式服务框架,SpringMVC,前后端分离项目,基础搭建,搭建过程出现的问题

    现互联网公司后端架构常用到Spring+SpringMVC+MyBatis,通过Maven来构建.通过学习,我已经掌握了基本的搭建过程,写下基础文章为而后的深入学习奠定基础. 首先说一下这篇文章的主要 ...

随机推荐

  1. 33 Introducing the Go Race Detector

    Introducing the Go Race Detector 26 June 2013 Introduction Race conditions are among the most insidi ...

  2. MongoDB中多表关联查询(

    1.使用aggregate 查看表数据 db.getCollection('reports').aggregate([ { $lookup: { from: "process", ...

  3. CxGrid 表格列内容居中

    首先每一列 Cxgrid 都不知道要当成什么来出来,所以每一列都有个properties 让你来设置,告诉cxgrid 这列的内容是什么,然后根据你给出的内容 再来决定用什么居中方式: 就是说 官方再 ...

  4. 关于json中转义字符/正斜杠的问题。

    1.首先有关转义字符 可以看百度百科: 先不管/是否需要转义,我们去json的官方网站去看看:http://www.json.org/ 可见有这个,那么意思是 json中 又规定建议了一下,意思是虽然 ...

  5. Linux学习笔记:wc查看文件字节数、字数、行数

    Linux系统中的wc(Word Count)命令可以统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 若不指定文件名称,或是所给予的文件名为“-”,则wc指令会从标准输入设备读取数据. 语 ...

  6. git clone 某个分支或者所有分支

    clone 某个分支: git clone -b  dev5   https://git.coding.net/aiyongbao/tradepc.git clone 所有分支:   git   cl ...

  7. PHP问题解决

    1.PHP在上传文件的时候出现错误Internal Server Error Internal Server ErrorThe server encountered an internal error ...

  8. 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) B - Enlarging Enthusiasm dp好题

    B - Enlarging Enthusiasm 感觉做到过好多的dp题都会和单调性结合在一起. 思路:dp[ s ][ pre ][ res ] 表示的是已选择了s,上一个是pre, 还有res 的 ...

  9. django视图函数中 应用装饰器

    from django.shortcuts import render, redirect, HttpResponse from .forms import LoginForm, Registrati ...

  10. linux 下rocketmq安装

    一.解压mq(/data下)tar -zxvf Rocketmq-3.5.8.tar.gz 二.修改配置文件vi /etc/profileexport rocketmq=/data/alibaba-r ...