【前言】
最近在项目中用到了maven工具,相信很多第一次接触maven的人都有这样的困惑,maven的文件很简单,就像下图中的结构一样,但是它的功能十分强大,那是怎么做到的呢?配置文件!配置文件里是精华啊!今天小编带大家初步了解maven中的配置以及在项目中的具体实现。
【类型】
maven的配置文件主要有两大类,pom和settings。
pom文件,包括项目的依赖关系、开发遵循的原则等等,而相对来说,settings的配置主要是项目的具体的配置信息,比如仓库的路径、构建信息等。
1、全局的setting文件是对所有项目的配置,他的存在,保证了项目团队开发的一致性设定。
2、而对于项目内的特殊配置,需要使用用户级别的配置文件来实现。
在Windows系统中,通常存在于C盘的用户目录下.m2/settings下,如:C:\Users\YANG\.m2
因为C盘文件并不安全,通常我们会将用户配置拷贝到其他位置。
比如你可能会看到这样的文件结构:
简单来说maven的配置分为三类:项目配置、用户配置和全局配置。根据你的需求进行调整吧。
【配置】
1、pom.xml
pom全程 Project Object Model。当maven工作时,会去项目根目录下读取pom.xml获取所需的配置信息。
pom中的配置信息主要有项目信息,maven构建所需的信息等。
常用的部分配置有:
groupId 组
artifactId 一般采用模块名
Version 版本
下面的pom.xml中添加junit的依赖,就可以使用它进行测试了。
示例:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tgb</groupId>
<artifactId>maven_test</artifactId>
<version>0.0.1-SANPSHOT</version>
<!--添加依赖-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
dependencies
用于添加依赖。当项目比较大时,子模块的pom需要制定父模块的pom。
以itoo为例,itoo-exam-api为子模块,itoo-exam-parent为父模块。在子模块itoo-exam-api中pom文件配置信息为
<parent>
<groupId>com.tgb</groupId>
<artifactId>itoo-exam-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../itoo-exam-parent/pom.xml</relativePath>
</parent>
distributionManagement
该元素定义了开发库
上面提到,在itoo中pom使用的继承关系设计的,所以该元素应该定义在最顶层中,即itoo-root的pom.xml中。在itoo中,我们采用maven+nexus
<!-- 项目部署信息(会被继承)===begin -->
<distributionManagement> <!-- 远程部署管理信息 -->
<repository> <!--部署项目产生的构件到远程仓库需要的信息 -->
<id>releases</id>
<name>Nexus Release Repository</name>
<url>${nexus.url}/releases/</url>
</repository>
<snapshotRepository> <!-- 构件的快照部署到哪里?如果没有配置该元素,默认部署到repository元素配置的仓库 -->
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>${nexus.url}/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<!-- 项目部署信息===end -->
2、settings.xml
settings中的常用配置可以看下面的节点信息。
<span style="font-family:Microsoft YaHei;"><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/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings></span>
localRepository
本地仓库的路径,默认为.m2/repository,存放主要jar包,当然也可以设置为其他路径。
比如在itoo中,小编的本地路径位于E:/maven/repository,那么该节点下就应该配置这个路径。更新maven的时候,jar包就下载到这个路径下。
<?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>E:/maven/repository</localRepository>
servers
在pom中的distributionManagement定义了开发库,而用户名和密码就设置在servers元素下。当然除了username和password,还有privateKey等信息。
例如:
在itoo中,我们采用maven+nexus,因此,这里的用户名和密码就是私服的用户名和密码,不相信的话自己去试啊~~~
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
mirrors
镜像库,指定库的镜像,用于增加其他库。
mirrorOf 该镜像指向的服务id,这里用*
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>Local Repository</name>
<!--私服地址-->
<url>http://192.168.22.246:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
profile
定义一系列配置信息,可以指定其激活条件,以达到不同环境使用不同配置信息的效果。
profile可以定义在pom、settings和全局setting文件中,作用范围不同而已。
在小编项目中,profiles信息配置在settings.xml,是用户级别的配置。
具体配置如下,主要是覆写中央仓库的一些配置。
<profiles>
<profile>
<id>central</id>
<repositories>
<repository>
<id>central</id>
<name>Central</name>
<url>http://*</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>local private nexus</name>
<!--私服地址-->
<url>http://192.168.22.246:8081/nexus/content/groups/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
使用activeProfiles激活信息。
<!--激活上面配置的仓库信息-->
<activeProfiles>
<activeProfile>central</activeProfile>
</activeProfiles>
【小结】
像很多工具、框架一样,配置文件里定义了原理的东西,了解了这些配置,可以帮助我们理解相互间的关联关系。这样看来,是不是在项目中配置也不难理解啊?当然,这些原理在排错和解决问题的时候可以给你很大的帮助。