Pom中有三个主要元素
Groupid,artifactid,version
goupid 是一个组织唯一的标识 例如 com.ibm.www
artifactid 是一个工程呢ID ibm-project
version 代表一个版本 例如 com.ibm.www.ibm-project.1.0
maven执行一个目标(goal)有以下几个步骤
prepare-resources 资源的拷贝
compile 源代码拷贝阶段
package 创建jar/war包阶段
install 安装包到本地或者远程库中
mvn clean dependency:copy-dependencies package
clean 首先被执行,
然后dependency:copy-dependencies被执行
然后是package被执行
clean 的 Lifecycle
pre-clean
clean
post-clean
默认 build Lifecycle
validate 验证工程是否正确,所有必要的信息是否有效
Lifecycle Phase | Description |
---|---|
validate | Validates whether project is correct and all necessary information is available to complete the build process. |
initialize | Initializes build state, for example set properties |
generate-sources | Generate any source code to be included in compilation phase. |
process-sources | Process the source code, for example, filter any value. |
generate-resources | Generate resources to be included in the package. |
process-resources | Copy and process the resources into the destination directory, ready for packaging phase. |
compile | Compile the source code of the project. |
process-classes | Post-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes. |
generate-test-sources | Generate any test source code to be included in compilation phase. |
process-test-sources | Process the test source code, for example, filter any values. |
test-compile | Compile the test source code into the test destination directory. |
process-test-classes | Process the generated files from test code file compilation. |
test | Run tests using a suitable unit testing framework(Junit is one). |
prepare-package | Perform any operations necessary to prepare a package before the actual packaging. |
package | Take the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file. |
pre-integration-test | Perform actions required before integration tests are executed. For example, 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. For example, cleaning up the environment. |
verify | Run any check-ups to verify the package is valid and meets quality criterias. |
install | Install the package into the local repository, which can be used as a dependency in other projects locally. |
deploy | Copies the final package to the remote repository for sharing with other developers and projects. |
local Repository 本地库
可以在settings文件中设置本地库位置
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>C:/MyLocalRepository</localRepository>
</settings>
Central Repository 中心库
maven在本地库中不能找到依赖的时候,就回去中心库中寻找
http://repo1.maven.org/maven2/
Remote Repository远程库
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.companyname.common-lib</groupId>
<artifactId>common-lib</artifactId>
<version>1.0.0</version>
</dependency>
<dependencies>
<repositories>
<repository>
<id>companyname.lib1</id>
<url>http://download.companyname.org/maven2/lib1</url>
</repository>
<repository>
<id>companyname.lib2</id>
<url>http://download.companyname.org/maven2/lib2</url>
</repository>
</repositories>
</project>
使用archetype插件创建工程
C:\MVN>mvn archetype:generate
-DgroupId=com.companyname.bank
-DartifactId=consumerBanking
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
参考自:http://www.tutorialspoint.com/maven/maven_quick_guide.htm