setting.xml
主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件
所在目录
1. ${M2_HOME}/conf/settings.xml
2. ${user.home}/.m2/settings.xml //需自1处拷贝
样例:
<?xml version="1.0" encoding="UTF-8"?>
<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
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository-->
<!--本地仓库,该值表示构建系统本地仓库的路径。其默认值为${user.home}/.m2/repository。 -->
<localRepository>usr/local/maven</localRepository>
<!--为仓库配置的下载镜像列表 -->
<mirrors>
<!--给定仓库的下载镜像。 -->
<mirror>
<!--该镜像的唯一标识符。id用来区分不同的mirror元素。 -->
<id>planetmirror.com</id>
<!--镜像名称 -->
<name>PlanetMirror Australia</name>
<!--该镜像的URL。构建系统会优先考虑使用该URL,而非使用默认的服务器URL。 -->
<url>http://downloads.planetmirror.com/pub/maven2</url>
<!--被镜像的服务器的id。例如,如果我们要设置一个Maven中央仓库(http://repo1.maven.org/maven2)的镜像,-->
<!--就需要将该元素设置成central。这必须和中央仓库的id central完全一致。 -->
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<!--根据环境参数来调整构建配置的列表。settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。-->
<profiles>
<!--根据环境参数来调整的构件的配置 -->
<profile>
<!--该配置的唯一标识符。 -->
<id>dev</id>
<!--远程仓库列表,它是Maven用来填充构建系统本地仓库所使用的一组远程项目。 -->
<repositories>
<!--包含需要连接到远程仓库的信息 -->
<repository>
<!--远程仓库唯一标识 -->
<id>snapshots</id>
<!--远程仓库名称 -->
<name>lib-snapshots</name>
<!--远程仓库URL,按protocol://hostname/path形式 -->
<url>http://artifactory.xxx.com/libs-snapshots</url>
<!--如何处理远程仓库里发布版本的下载 -->
<releases>
<!--true或者false表示该仓库是否为下载某种类型构件(发布版,快照版)开启。 -->
<enabled>false</enabled>
<!--该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。这里的选项是:-->
<!--always(一直),daily(默认,每日),interval:X(这里X是以分钟为单位的时间间隔),或者never(从不)。 -->
<updatePolicy>always</updatePolicy>
<!--当Maven验证构件校验文件失败时该怎么做:-->
<!--ignore(忽略),fail(失败),或者warn(警告)。 -->
<checksumPolicy>warn</checksumPolicy>
</releases>
<!--如何处理远程仓库里快照版本的下载。有了releases和snapshots这两组配置,POM就可以在每个单独的仓库中,为每种类型的构件采取不同的策略。-->
<!--例如,可能有人会决定只为开发目的开启对快照版本下载的支持。参见repositories/repository/releases元素 -->
<snapshots>
<enabled />
<updatePolicy />
<checksumPolicy />
</snapshots>
</repository>
</repositories>
<!--发现插件的远程仓库列表。仓库是两种主要构件的家。第一种构件被用作其它构件的依赖。这是中央仓库中存储的大部分构件类型。另外一种构件类型是插件。-->
<!--Maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories元素的结构和repositories元素的结构类似。-->
<!--每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。 -->
<pluginRepositories>
<!--包含需要连接到远程插件仓库的信息.参见profiles/profile/repositories/repository元素的说明 -->
<pluginRepository>
<releases>
<enabled />
<updatePolicy />
<checksumPolicy />
</releases>
<snapshots>
<enabled />
<updatePolicy />
<checksumPolicy />
</snapshots>
<id />
<name />
<url />
<layout />
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!--手动激活profiles的列表,按照profile被应用的顺序定义activeProfile。 该元素包含了一组activeProfile元素,每个activeProfile都含有一个profile id。-->
<!--任何在activeProfile中定义的profile id,不论环境设置如何,其对应的 profile都会被激活。-->
<!--如果没有匹配的profile,则什么都不会发生。例如,development是一个activeProfile,则在pom.xml(或者profile.xml)中对应id的profile会被激活。-->
<!--如果运行过程中找不到这样一个profile,Maven则会像往常一样运行。 -->
<activeProfiles>
<activeProfile>development</activeProfile>
</activeProfiles>
<!--配置服务端的一些设置。一些设置如安全证书不应该和pom.xml一起分发。这种类型的信息应该存在于构建服务器上的settings.xml文件中。 -->
<servers>
<!--服务器元素包含配置服务器时需要的信息 -->
<server>
<!--这是server的id(注意不是用户登陆的id),该id与distributionManagement中repository元素的id相匹配。 -->
<id>xx-central</id>
<!--鉴权用户名。鉴权用户名和鉴权密码表示服务器认证所需要的登录名和密码。 -->
<username>my_login</username>
<!--鉴权密码 。鉴权用户名和鉴权密码表示服务器认证所需要的登录名和密码。 -->
<password>my_password</password>
</server>
</servers>
</settings>
pom.xml
主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和licenses,以及其他所有的项目相关因素,是项目级别的配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 模型版本。maven2.0必须是这样写,现在是maven2唯一支持的版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 公司或者组织的唯一标志,并且配置时生成的路径也是由此生成, 如com.xxx.mall,maven会将该项目打成的jar包放本地路径:/com/xxx/mall -->
<groupId>com.xxx.mall</groupId>
<!-- 本项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的 -->
<artifactId>mall</artifactId>
<!-- 本项目目前所处的版本号 -->
<version>1.0.0</version>
<!-- 打包的机制,如pom,jar, maven-plugin, ejb, war, ear, rar, par,默认为jar -->
<!-- 当包含子项目时,packaging必须为pom -->
<packaging>pom</packaging>
<!-- 声明了该项目的直接子模块 -->
<modules>
<!-- 这里配置的不是artifactId,而是这个模块的目录名称-->
<module>mall-service</module>
</modules>
<!-- 为pom定义一些常量,在pom中的其它地方可以直接引用 使用方式 如下 :${jdk.version} -->
<properties>
<jdk.version>1.6</jdk.version>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
<!-- 定义本项目的依赖关系 -->
<!-- 该节点下的依赖会被子项目自动全部继承 -->
<dependencies>
<!-- 每个dependency都对应这一个jar包 -->
<dependency>
<!--一般情况下,maven是通过groupId、artifactId、version这三个元素值(俗称坐标)来检索该构件, 然后引入你的工程。如果别人想引用你现在开发的这个项目(前提是已开发完毕并发布到了远程仓库),-->
<!--就需要在他的pom文件中新建一个dependency节点,将本项目的groupId、artifactId、version写入, maven就会把你上传的jar包下载到他的本地 -->
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.7</version>
<!-- maven认为,程序对外部的依赖会随着程序的所处阶段和应用场景而变化,所以maven中的依赖关系有作用域(scope)的限制。 -->
<!--scope包含如下的取值:compile(编译范围)、provided(已提供范围)、runtime(运行时范围)、test(测试范围)、system(系统范围) -->
<scope>test</scope>
<!-- type一般不用配置,默认是jar。当type为pom时,代表引用关系,会将指定artifactId下的所有jar包导入依赖库 -->
<type>jar</type>
<!-- 设置指依赖是否可选,默认为false,即子项目默认都继承:为true,则子项目必需显示的引入,与dependencyManagement里定义的依赖类似 -->
<optional>false</optional>
<!-- 屏蔽依赖关系。 比如项目中使用的libA依赖某个库的1.0版,libB依赖某个库的2.0版,现在想统一使用2.0版,就应该屏蔽掉对1.0版的依赖 -->
<exclusions>
<exclusion>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- 该节点下的依赖关系只是为了统一版本号,不会被子项目自动继承,-->
<!--除非子项目主动引用,好处是子项目可以不用写版本号 -->
<dependencyManagement>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!--使用的插件列表 。 -->
<plugins>
<plugin>
<groupId></groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.5.5</version>
<!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。 -->
<executions>
<execution>
<!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标 -->
<id>attach-sources</id>
<!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段 -->
<phase>package</phase>
<!--配置的执行目标 -->
<goals>
<goal>jar</goal>
</goals>
<!--配置是否被传播到子POM -->
<inherited>false</inherited>
</execution>
</executions>
<!--作为DOM对象的配置,配置项因插件而异 -->
<configuration>
<!-- <finalName>${finalName}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<skipTests>true</skipTests>
<encoding>utf-8</encoding>
<descriptor>assembly.xml</descriptor> -->
<attach>true</attach>
</configuration>
<!--是否从该插件下载Maven扩展(例如打包和类型处理器), -->
<!--由于性能原因,只有在真需要下载时,该元素才被设置成true。 -->
<extensions>false</extensions>
<!--项目引入插件所需要的额外依赖 -->
<dependencies>
<dependency>...</dependency>
</dependencies>
<!--任何配置是否被传播到子项目 -->
<inherited>true</inherited>
</plugin>
</plugins>
<!--主要定义插件的共同元素、扩展元素集合,类似于dependencyManagement, -->
<!--所有继承于此项目的子项目都能使用。该插件配置项直到被引用时才会被解析或绑定到生命周期。 -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>pre-process-classes</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>pre-process</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- war插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
</plugin>
<!-- 配置构建时忽略测试用例 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
如果项目存在继承或聚合关系,则在子项目pom中,可配置如下:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx.mall</groupId>
<!-- 可以仅配置artifactId -->
<artifactId>mall-service</artifactId>
<version>1.0.0</version>
<!-- 声明父项目的坐标 -->
<parent>
<groupId>com.xxx.mall</groupId>
<artifactId>mall</artifactId>
<version>1.0.0</version>
<!-- 父项目的pom.xml文件的相对路径,默认不配置。相对路径允许你选择一个不同的路径。 -->
<!-- 默认值是../pom.xml。Maven首先在构建当前项目的地方寻找父项目的pom, -->
<!-- 其次在文件系统的这个位置(relativePath位置), -->
<!-- 然后在本地仓库,最后在远程仓库寻找父项目的pom。 -->
<relativePath>../parent-project/pom.xml</relativePath>
</parent>
<!-- 声明父项目dependencyManagement的依赖,不用写版本号 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>
<!-- 声明父项目pluginManagement的依赖,不用写版本号 -->
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
...
</build>
</project>
用树形图来表达 pom.xml,如下:
Lifecycle 是项目构建的生命周期,它包括 9 个 Phase(阶段):
阶段 | 插件 | 作用 |
---|---|---|
clean | clean | 清理自动生成的文件,也就是 target 目录 |
validate | 由 Maven 核心负责 | 验证 Maven 描述文件是否有效 |
compile | compiler、resources | 编译 Java 源码 |
test | compiler、surefire、resources | 运行测试代码 |
package | war | 项目打包,就是生成构件包,也就是打 war 包 |
verify | 由 Maven 核心负责 | 验证构件包是否有效 |
install | install | 将构件包安装到本地仓库 |
site | site | 生成项目站点,就是一堆静态网页文件,包括 JavaDoc |
deploy | deploy | 将构件包部署到远程仓库 |