逻辑表示方式
,而构件的物理表示方式
是文件。~/.m2/respository
目录,用户能够自定义本地仓库目录,在~/.m2/settings.xml
文件中进行修改:web
<localRepository>/Users/john/dev/repository</localRepository>
使用本地项目构件:将本地项目构件安装到maven本地仓库spring
john:demo1 john$ mvn clean install
在maven的超级pom中能够看到默认的中央仓库。apache
${M2_HOME}/lib/maven-model-builder-3.6.3.jar
找到中央仓库的默认配置:maven
<repositories> <repository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <!-- 不从该中央仓库下载快照版本的构件 --> <enabled>false</enabled> </snapshots> </repository> </repositories>
默认的中央仓库:布局
私服的好处:测试
配置信息ui
<project> ... <repositories> <repository> <id>aliyun-repo</id> <name>aliyun repo</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> <!-- updatePolicy 默认值daily --> <updatePolicy>daily</updatePolicy> <checksumPolicy>ignore</checksumPolicy> </releases> <snapshots> <!-- 不下载快照版本构件 --> <enabled>false</enabled> </snapshots> <layout>default</layout> </repository> </repositories> ... </project>
repositories->repository
元素说明url
repositories
元素下可使用repository
子元素声明一个或多个远程仓库。id
:远程仓库标识,id名称惟一。注意:maven自带的中央仓库id为central
,若是其余的仓库声明也使用该id,会覆盖中央仓库的配置。url
:远程仓库地址。releases
:用来控制对于发布版本构件的下载。代理
enabled
属性表示是否开启发布版本的下载支持。updatePolicy
:用来配置maven从远程仓库更新的频率。code
daily
,表示天天检查一次。never
,从不检查更新。always
,每次构建都检查更新。interval: X
,每隔X分钟检查更新一次(X为任意整数)checksumPolicy
:用来配置maven检查校验和文件的策略。
warn
,执行构建时给出警告信息。fail
,遇到校验和错误,就让构建失败。ignore
,让maven彻底忽略校验和错误。snapshots
:用来控制对于快照版本构件的下载。
enabled
属性表示是否开启快照版本的下载支持。-SNAPSHOT
结尾,发布版本没有这个标识。layout
:default
表示仓库的布局是maven2或者maven3的默认布局,而不是maven1的布局。测试远程仓库可否正常拉取依赖
${localRepository}
/org/springframework/spring-web目录文件,此时本地仓库无依赖。/Users/john/Desktop/demo1
,在终端执行mvn compile
查看终端输出。
john:demo1 john$ mvn compile [INFO] Scanning for projects... [INFO] [INFO] ---------------------------< com.john:demo1 >--------------------------- [INFO] Building demo1 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- Downloading from aliyun-repo: https://maven.aliyun.com/repository/public/org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.pom Downloaded from aliyun-repo: https://maven.aliyun.com/repository/public/org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.pom (1.9 kB at 5.0 kB/s) Downloading from aliyun-repo: https://maven.aliyun.com/repository/public/org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.jar Downloaded from aliyun-repo: https://maven.aliyun.com/repository/public/org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.jar (1.4 MB at 6.3 MB/s) [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo1 --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /Users/john/Desktop/demo1/demo1/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo1 --- [INFO] Nothing to compile - all classes are up to date [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.565 s [INFO] Finished at: 2019-12-15T13:38:51+08:00 [INFO] ------------------------------------------------------------------------
Downloading from aliyun-repo
后面就是在pom文件中配置的远程仓库,aliyun-repo
是定义的仓库id。 在本地仓库缺乏依赖后,就会从配置的远程仓库下载依赖。什么是镜像仓库?
配置镜像仓库
<mirrors> <mirror> <id>aliyun-maven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
配置解释
mirrors
元素下包含多个mirror
子元素,每一个mirror元素表示一个远程镜像。id
:镜像id,惟一标识。name
:镜像名称。url
:镜像地址mirrorOf
:指定哪些远程仓库的id使用这个镜像去下载构件,这个对应pom.xml文件中repository元素的id。就是表示这个镜像是给哪些pom.xml文件中的远程仓库使用的,这里面须要列出远程仓库的id,多个之间用逗号隔开。mirrorOf
配置语法:
<mirrorOf> * </mirrorOf>
:匹配全部远程仓库。<mirrorOf>external: * </mirrorOf>
:匹配全部不在本机上的远程仓库。<mirrorOf> repo1, repo2 </mirrorOf>
:匹配仓库repo1和repo2,多个仓库之间使用逗号分割。<mirrorOf> *, !repo1 </mirrorOf>
:匹配全部远程仓库,除了仓库repo1,使用感叹号(!
)将仓库从匹配中排除。举例
项目demo1 pom文件的依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.1.RELEASE</version> </dependency>
查看spring-web的jar包在本地仓库中的位置。
/Users/john/dev/repository/org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.jar
/Users/john/dev/repository
是本地仓库目录。groupId/artifactId/version/
artifactId-version[-classifier].packaging
, packaging默认为jar。maven如何定位构件路径
将groupId中的句点分隔符(.
)转换成路径分隔符(/
),同时在后面追加一个路径分隔符(/
)。
org.springframework
--->org/springframework/
将artifactId拼接在1的路径上, 同时在后面追加一个路径分隔符(/
)。
org/springframework/spring-web/
将version拼接在2的路径上,同时在后面追加一个路径分隔符(/
)。
org/springframework/spring-web/5.2.1.RELEASE/
将构件名称拼接在3的路径上。
org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.jar
依赖解析机制: