资源描述
Maven
1. Maven主要功能:
Ø 构建项目(Builds)
Ø 文档编制(Documentation)
Ø 报告(Reporting)
Ø 依赖管理(Dependencies)
Ø 配置管理(SCMs)
Ø 发布管理(Releases)
2. Maven基本命令
Ø 创建Maven目录
mvn archetype:create -DgroupId=com.yourCompanyName -DartifactId=yourAppName
Ø 测试
mvn test
Ø 编译类
mvn compile
Ø 打包
mvn package
Ø 生成Site
mvn site
Ø 清除
mvn clean
以上命令都是在command窗口运行的.它们还可以混合运行,如mvn test compile package
3. Maven的生命周期及其与之对应的命令:
validate:验证工程是否正确,所有需要的资源是否可用。
compile:编译项目的源代码。
test-compile:编译项目测试代码。
test:使用已编译的测试代码,测试已编译的源代码。
package:已发布的格式,如jar,将已编译的源代码打包。
integration-test:在集成测试可以运行的环境中处理和发布包。
verify:运行任何检查,验证包是否有效且达到质量标准。
install:把包安装在本地的repository中,可以被其他工程作为依赖来使用
deploy:在整合或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享。
generate-sources:产生应用需要的任何额外的源代码,如xdoclet。、
4. pom.xml的结构
上面就是前次HelloMaven工程的pom.xml文件,我们可以通过修改其中的元素使工程按照我们的意图运行,对于接触Maven的人,主要就是和pom.xml文件在打交道.
pom.xml文件基本节点介绍:
<project>:文件的根节点.
<modelversion>:pom.xml使用的对象模型版本.
<groupId>:创建项目的组织或团体的唯一Id.
<artifactId>:项目的唯一Id,可视为项目名.
<packaging>:打包物的扩展名,一般有JAR,WAR,EAR等
<version>:产品的版本号.
<name>:项目的显示名,常用于Maven生成的文档。
<url>:组织的站点,常用于Maven生成的文档。
<description>:项目的描述,常用于Maven生成的文档。
<repositories>: 资源地址,所有的依赖包将从次地址下载,其中如果snapshot为资源快照,相对不稳定,
而release为稳定版本
<pluginRepositories>: 插件地址,因为maven的所有功能都是使用插件来实现功能的,因此需要
从特定的地址下载插件包
<build>:功能集标签,在此标签下面可以定义一系列的插件以实现功能,常用插件有以下两种:
· maven-surefire-plugin:单元测试的插件,在此插件下面可以设置一些列的参数
· maven-compiler-plugin:代码编译插件,在用此插件的时候一定要设置source的版本,默认的是JDK1.3.
<dependencies>: 项目需要的所有依赖的包
5. 完整pom.xml示例
6. <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/maven-v4_0_0.xsd">
<!-- The Basics -->
<modelVersion>4.0.0</modelVersion>
<groupId>com.citigroup.junglesong</groupId>
<artifactId>MavenResource</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>MavenResource</name>
<url>http://maven.apache.org</url>
<!-- dependent Libs -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Build Settings -->
<reporting>
<plugins>
<plugin>
<artifactId>
maven-project-info-reports-plugin
</artifactId>
<reportSets>
<reportSet>
<id>sunlink</id>
<reports>
<report>javadoc</report>
</reports>
<inherited>true</inherited>
<configuration>
<links>
<link>
</link>
</links>
</configuration>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId><!--Generate Java Doc -->
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId><!-- Code Check -->
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>
config/sun_checks.xml
</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId><!-- Test Report -->
<artifactId>surefire-report-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId><!-- HTMLize Source Code -->
<artifactId>jxr-maven-plugin</artifactId>
</plugin>
<plugin><!-- Find Bugs -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
<!-- More Project Information -->
<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<organization>
<name>SitInSpring</name>
<url>
</organization>
<developers>
<developer>
<id>sitinsprng</id>
<name>SitInSpring</name>
<email>SitInSpring@</email>
<url>
<organization>UFO</organization>
<organizationUrl>http://www.UFO.org</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>+8</timezone>
<properties>
<picUrl>
</properties>
</developer>
<developer>
<id>junglesong</id>
<name>Junglesong</name>
<email>junglesong@</email>
<url>
<organization>UFO</organization>
<organizationUrl>http://www.UFO.org</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>+8</timezone>
<properties>
<picUrl>
</picUrl>
</properties>
</developer>
</developers>
<contributors>
<contributor>
<name>Alien</name>
<email>Alien@</email>
<url>http://A</url>
<organization>AlienOrganization</organization>
<organizationUrl>
http://AlienO
</organizationUrl>
<roles>
<role>tester</role>
</roles>
<timezone>-5</timezone>
<properties>
<gtalk>some.name@</gtalk>
</properties>
</contributor>
</contributors>
<repositories>
<repository>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
<!-- Environment Settings -->
<mailingLists>
<mailingList>
<name>User List</name>
<subscribe>user-subscribe@127.0.0.1</subscribe>
<unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe>
<post>user@127.0.0.1</post>
<archive>http://127.0.0.1/user/</archive>
<otherArchives>
<otherArchive>
</otherArchive>
</otherArchives>
</mailingList>
</mailingLists>
</project>
7. Settings.xml结构
settings.xml基本结构如下:
<settings 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/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
简单介绍一下几个主要的配置因素:
localRepository:表示本地库的保存位置,也就是maven2主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。
offline:如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。
Servers
在POM中的 distributionManagement元素定义了开发库。然而,特定的username和pwd不能使用于pom.xml,所以通过此配置来保存server信息
<servers>
<server>
<id>server001</id>
<username>my_login</username>
<password>my_password</password>
<privateKey>${usr.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</servers>
•id:server 的id,用于匹配distributionManagement库id,比较重要。
•username, password:用于登陆此服务器的用户名和密码
•privateKey, passphrase:设置private key,以及passphrase
•filePermissions, directoryPermissions:当库文件或者目录创建后,需要使用权限进行访问。参照unix文件许可,如664和775
Mirrors
表示镜像库,指定库的镜像,用于增加其他库
<mirrors>
<mirror>
<id></id>
<name>PlanetMirror Australia</name>
<url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
•id,name:唯一的标志,用于区别镜像
•url:镜像的url
•mirrorOf:此镜像指向的服务id
Proxies
此设置,主要用于无法直接访问中心的库用户配置。
<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host></host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>*|ibiblio.org</nonProxyHosts>
</proxy>
</proxies>
•id:代理的标志
•active:是否激活代理
•protocol, host, port:protocol://host:port 代理
•username, password:用户名和密码
•nonProxyHosts: 不需要代理的host
Profiles
类似于pom.xml中的profile元素,主要包括activation,repositories,pluginRepositories 和properties元素
刚开始接触的时候,可能会比较迷惑,其实profiles是maven2中比较强大的功能。从字面上来说,就是个性配置。
单独定义profile后,并不会生效,需要通过满足条件来激活。
repositories 和pluginRepositories
定义其他开发库和插件开发库。对于团队来说,肯定有自己的开发库。可以通过此配置来定义。
如下的配置,定义了本地开发库,用于release 发布。
<repositories>
<repository>
<id>repo-local</id>
<name>Internal 开发库</name>
<url>http://192.168.0.2:8082/repo-local</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo-local</id>
<name>Internal 开发库</name>
<url>http://192.168.0.2:8082/repo-local</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>
releases, snapshots:每个产品的版本的Release或者snapshot(注:release和snapshot的区别,release一般是比较稳定的版本,而snapshot基本上不稳定,只是作为快照)
properties
maven 的properties作为placeholder值,如ant的properties。
包括以下的5种类型值:
1.env.X:返回当前的环境变量
2.project.x:返回pom中定义的元素值,如project.version
3.settings.x:返回settings.xml中定义的元素
4.java 系统属性:所有经过java.lang.System.getProperties()返回的值
5.x:用户自己设定的值
Activation
用于激活此profile
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>mavenVersion</name>
<value>2.0.3</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
•jdk:如果匹配指定的jdk版本,将会激活
•os:操作系统
•property:如果maven能检测到相应的属性
•file: 用于判断文件是否存在或者不存在
除了使用activation来激活profile,同样可以通过activeProfiles来激活
Active Profiles
表示激活的profile,通过profile id来指定。
<activeProfiles>
<activeProfile>env-test</activeProfile> 指定的profile id
</activeProfiles>
展开阅读全文