管理项目构建整个过程的工具,使得项目的构建只需一条命令即可自动化完成。
哲学:Convention over configuration。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/webapp | Web application sources |
src/test/java | Test sources |
src/test/resources | Test resources |
src/test/filters | Test resource filter files |
src/site | Site |
LICENSE.txt | Project's license |
NOTICE.txt | Notices and attributions required by libraries that the project depends on |
README.txt | Project's readme |
Maven把整个构建过程称为Lifecycle,Lifecycle由一系列Stage构成(即构建的主要阶段),每个Stage内可以包含一些tasks/goals。 而goal是由plugin来完成的,每个plugin可以完成一些goals。
1. Maven中已经内置了3种Lifecycle---default, clean and site,如 default Lifecycle包括以下Stages:
- validate - validate the project is correct and all necessary information is available
- compile - compile the source code of the project
- test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
- package - take the compiled code and package it in its distributable format, such as a JAR.
- integration-test - process and deploy the package if necessary into an environment where integration tests can be run
- 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 install,就会执行所有从validate到install的stage。
2. 如何将task绑定到stage中去:
使用package:POM element
<packaging> Some of the valid packaging values are
jar,
war,
ear
and
pom. If no packaging value has been specified, it will default to
jar. 每种package就定义了在各个stage中绑定哪些task。
Default Lifecycle Bindings - Packaging ejb / ejb3 / jar / par / rar / war
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war |
install | install:install |
deploy | deploy:deploy |
使用plugin来单独配置:POM in the
<plugins>
3. pom.xml文件:
定义该project的配置。Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified.
自己定义的pom文件将继承Maven定义好的Super POM 文件,该Super POM中定义了很多默认的设置。
4. Repository:
在pom文件中申明的dependency,Maven首先查看local repository中有没有该依赖的包,如果没有,则到一个remote repository中去下载(可以设置,默认是Maven的一个远程repository,可以配置为公司内部的一个服务器),然后放到local repository中。