maven 安装:
1.下载maven的压缩文件:http://maven.apache.org/download.cgi
2.解压到想要安装的目录,可以是任意的位置
3.新建一个maven的repository的文件夹(可以是任意位置)【用来存放jar包的仓库】
4.修改maven软件中的setting.xml文件,添加<localRepository>标签
5.添加环境maven的位置到环境变量
maven和myeclipse结合
开始建立 maven工程
今天创建一个maven项目,
在命令行:
mvn archetype:create -DgroupId=com.jt.xiaoyang -DartifactId=xiaoyang-app -DarchetypeArtifactId=maven-archetype-webapp
直接报错:maven-archeype-plugin:2.3:create (default-cli) @ standalone-pom
解决办法:把create命令,换为generate
mvn archetype:generate -DgroupId=com.jt.xiaoyang -DartifactId=xiaoyang-app -DarchetypeArtifactId=maven-archetype-webapp
总结,因为用的是maven3.3.3版本,所以命令做了调整吧!
以上是使用命令行操作,其实很少用,主要还是用eclipse来操作
========================maven的一些概念====================
maven有一套build的生命周期,是按照一套顺序走下来的,这一套顺序叫一个生命周期(lifecycle).
maven有 三种生命周期:[一个生命周期分为多个build phase.]
1.default 2.clean 3.site
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.
如 执行:mvn deploy
则前面的verify, install......都会被执行
clean 生命周期的build phase;
【每一个phase是由goal【任务】组成,一个goal可以关联到一个build phase,也可以不关联到任何build phase,不关联到任何phase的goal可以独立执行,
如:mvn clean depedency:copy-dependencies package】
上面的命令会导致先执行clean这个phase,然后拷贝依赖项,最后打包
如果一个goal被绑定到多个phase上,那么goal会被执行多次。
phase的顺序是已经固定的,如果一个phase没有绑定任何goal,那么phase就不会被执行。
一个goal可以通过两种方式绑定到一个phase,一个是指定packaging,另一个是plugin
每一个build phase会绑定到goal。goal是由plugin提供的。
每一种packaging的值都表明了一定的phase和goal之间的绑定关系。
site 生命周期的build phase:
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 |
总结:
另外一个很重要的就是dependency,我们要在项目中引用一个依赖,只需要在pom.xml指定依赖的名字和版本,maven会自动去远程的repository下载,然后放到本地的repository里面,这样以后所有的project都可以共用