Maven 使用介绍html
建立projectjava
先去官方网站下载一个最新版本http://maven.apache.org/download.cgi. 下载后解压,使用以前最好先将maven的bin目录设置到path环境变量里面。web
maven无非也就是用来build一个project的,直接先上一个例子,在命令行下输入下面的命令:apache
mvn archetype:generate DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=myappapp
mvn就是maven的命令行程序,archetype:generate中的archetype是plugin的名字,generate是goal的名字,命令行后面的是一些参数。关于archetype和goal以及后面的参数,后面再细说。eclipse
若是是第一次运行,这个过程会有点慢,maven须要下载一些依赖项,中间若是有输入提示信息,直接回车使用默认值就能够了。这条命令执行完后,会在你的当前目录下生成一个名为myapp的目录:webapp
注意这个目录结构,src/main/java 和 src/test/java 是不能改动,否则maven会没法找到源文件。下面是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文件的内容以下:ide
<
project
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
post
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<
modelVersion
>4.0.0</
modelVersion
>
<
groupId
>com.mycompany.app</
groupId
>
<
artifactId
>my-app</
artifactId
>
<
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文件的内容:
这个project建立好后和普通的project没有什么不一样,咱们直接往里面放源代码进行开发就能够了,若是有目录想修改的也彻底能够。
POM & archetype
archetype就是一个project的模板,上面咱们生成的project就是用默认的archetype生成的。若是使用不一样的archetype,生成的project结构会有所不一样。 一个archetype指明了
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
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中的配置:
<
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
到这里就说完了建立,编译,测试,打包以及安装,大部分的项目也就是作这些事情。
再介绍几个其它命令:
build lifecycle & build phase & goal
maven有一套build的生命周期,是按照一套顺序走下来的,这一套顺序就叫一个生命周期 (lifecycle)。maven内置三种生命周期:default, clean 和 site。一个生命周期分为多个build phase,下面是default生命周期所有的build phase:
- validate:validate the project is correct and all necessary information is available.
- initialize:initialize build state, e.g. set properties or create directories.
- generate-sources:generate any source code for inclusion in compilation.
- process-sources:process the source code, for example to filter any values.
- generate-resources:generate resources for inclusion in the package.
- process-resources:copy and process the resources into the destination directory, ready for packaging.
- compile:compile the source code of the project.
- process-classes:post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
- generate-test-sources:generate any test source code for inclusion in compilation.
- process-test-sources:process the test source code, for example to filter any values.
- generate-test-resources:create resources for testing.
- process-test-resources:copy and process the resources into the test destination directory.
- test-compile:compile the test source code into the test destination directory
- process-test-classes:post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
- test:run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
- prepare-package:perform 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)
- package:take the compiled code and package it in its distributable format, such as a JAR.
- pre-integration-test:perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
- integration-test:process and deploy the package if necessary into an environment where integration tests can be run.
- post-integration-test:perform actions required after integration tests have been executed. This may including cleaning up the environment.
- verify:run any checks to verify the package is valid and meets quality criteria.
- install:install the package into the local repository, for use as a dependency in other projects locally.
- deploy:done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
这些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里面配置。 看两个配置:
<
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执行屡次。
看另外一个示例配置:
<
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均可以共用