(1)什么是maven仓库
假如当前有几十个项目,这些项目中大部分都用到了spring。如果没有maven,那么每个项目中都要复制一份jar包,这样实际上是非常浪费磁盘空间的。
实际情况是,在不适用maven的项目中,我们往往会命名为lib,各个项目lib目录中就有大量重复。
实际maven项目不再各自存储其依赖文件,他们只声明这些依赖的坐标,在需要的时候,如编译的时候需要将依赖加入到classpath中
(2)maven仓库布局
也就是groupId/artifactId/version/artifactId-version.packaging
比如log4j:log4j:1.2.15这个依赖,仓库路径为log4j/log4j/1.2.15/log4j-1.2.15.jar(3)maven本地仓库、私有仓库、中央仓库
1.本地仓库
1)本地仓库路径设置
可以在setting.xml中进行设置
<?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: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:/mavenRepo</localRepository>
</settings>
2)安装构建到本地仓库
使用mvn clean install命令
2.远程仓库
1)添加远程仓库
默认情况下使用的是中央仓库,但是有时候中央仓库中缺少某个jar,所以就需要配置其他的远程仓库
<profiles>
<profile>
<id>cloudhopper</id>
<repositories>
<repository>
<id>cloudhopper</id>
<name>Repository for Cloudhopper</name>
<url>http://maven.cloudhopper.com/repos/third-party/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>cloudhopper</activeProfile>
</activeProfiles>
repository中的id表示远程仓库的id,中央仓库的id为central,如果定义其他id为central就会覆盖中央仓库
name指定了这个远程仓库的名称
url:远程仓库的地址,一般都是使用http协议
releases设置为true,snapshots设置为false表示从这个远程仓库中下载release版本的jar但是不下载snapshot版本的
2)远程仓库认证
一般的远程仓库不需要认证就能登录,但是在公司中为了安全,通常会先通过认证才能登录远程仓库,认证信息配置有两种
<1>使用用户名和密码登录
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
<2>使用私钥
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
注意,不管哪种方式,server中的id必须和配置的远程仓库id一致
3)部署到远程仓库
一般部署到远程仓库需要先通过验证,就是上面的配置方式
因为部署release版本和snapshot版本的远程仓库可能不一样,所以需要在pom.xml中进行配置
<distributionManagement>
<repository>
<id>release-deploy-respository</id>
<name>cdc-plugin-respository</name>
<url>ftp://192.168.2.177/home/mfguser/Maven 2 release Repository</url>
</repository>
<snapshotRepository>
<id>snapshot-deploy-respository</id>
<name>cdc-plugin-respository</name>
<url>ftp://192.168.2.177/home/mfguser/Maven 2 snapshot Repository</url>
</snapshotRepository>
</distributionManagement>
然后执行
mvn clean deploy即可部署