仓库配置
配置的仓库有:nexus-test-public、aliyun、google
maven 仓库的更新策略指的是更新 maven-metadata-[repository].xml
,即何时触发从远程仓库读取最新的 maven-metadata-[repository].xml
的策略
<updatePolicy></updatePolicy>
用于配置对于 SNAPSHOT 版本向远程仓库中查找的频率,对 RELEASE 版本无效
频率共有四种,分别是always、daily (默认策略)、interval:X(X为整数,单位是分钟)
、never
使用 mvn -U
强制更新快照版本,对发布版本无效
<repositories>
<repository>
<id>nexus-test-public</id>
<name>test-public</name>
<url>http://localhost:8081/repository/test-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>google</id>
<url>https://maven.aliyun.com/repository/google</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-plugin</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
一、snapshot 版本
1. 依赖jar 包
有 snapshot 版本的 jar包,为:
<dependency>
<groupId>org.example</groupId>
<artifactId>util-demo</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
2. deploy 包
可以 docker 启动 maven,与主机的 maven 仓库隔离
docker run -it --rm --name my-maven-project -u 1000:1000 -v /data/var/maven/.m2:/var/maven/.m2 -e MAVEN_CONFIG=/var/maven/.m2 -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.8.6-openjdk-8 mvn -Duser.home=/var/maven clean deploy
maven deploy 到仓库的信息:
1.从远程仓库下载 util-demo/1.1-SNAPSHOT/maven-metadata.xml
文件
2.上传 util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.jar
文件
上传 util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.pom
文件
3.从远程仓库下载 util-demo/maven-metadata.xml
文件
4.上传 util-demo/1.1-SNAPSHOT/maven-metadata.xml
文件
上传 util-demo/maven-metadata.xml
文件
[INFO] --- maven-install-plugin:2.4:install (default-install) @ util-demo ---
[INFO] Installing /usr/src/mymaven/target/util-demo-1.1-SNAPSHOT.jar to /var/maven/.m2/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-SNAPSHOT.jar
[INFO] Installing /usr/src/mymaven/pom.xml to /var/maven/.m2/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ util-demo ---
Downloading from test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/1.1-SNAPSHOT/maven-metadata.xml
Downloaded from test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/1.1-SNAPSHOT/maven-metadata.xml (764 B at 15 kB/s)
Uploading to test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.jar
Uploaded to test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.jar (3.0 kB at 47 kB/s)
Uploading to test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.pom
Uploaded to test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.pom (2.7 kB at 130 kB/s)
Downloading from test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/maven-metadata.xml
Downloaded from test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/maven-metadata.xml (316 B at 29 kB/s)
Uploading to test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/1.1-SNAPSHOT/maven-metadata.xml
Uploaded to test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/1.1-SNAPSHOT/maven-metadata.xml (764 B at 14 kB/s)
Uploading to test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/maven-metadata.xml
Uploaded to test-snapshot: http://172.17.0.1:8081/repository/test-snapshot/org/example/util-demo/maven-metadata.xml (316 B at 6.2 kB/s)
3. 拉取 jar 包
maven 拉取 jar 包信息如下:
下载文件
1.util-demo/1.1-SNAPSHOT/maven-metadata.xml
2.util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.pom
3.util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.jar
下载 maven-metadata.xml
后,如果发现快照版本是新的,则不会再从远程仓库拉取 jar 包
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1-SNAPSHOT/maven-metadata.xml
Downloaded from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1-SNAPSHOT/maven-metadata.xml (764 B at 6.8 kB/s)
Downloading from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.pom
Downloaded from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.pom (2.7 kB at 80 kB/s)
Downloading from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.jar
Downloaded from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1-SNAPSHOT/util-demo-1.1-20230628.062719-4.jar (3.0 kB at 275 kB/s)
4. 本地仓库
每次构建,会自动拉取 snapshot 版本
本地仓库文件有:
zxm@zxm-pc:~/.m2/repository/org/example/util-demo$ pwd
/home/zxm/.m2/repository/org/example/util-demo
zxm@zxm-pc:~/.m2/repository/org/example/util-demo$ ls
1.1-SNAPSHOT
zxm@zxm-pc:~/.m2/repository/org/example/util-demo$ cd 1.1-SNAPSHOT/
zxm@zxm-pc:~/.m2/repository/org/example/util-demo/1.1-SNAPSHOT$ ls -lh
总用量 72K
-rw-rw-r-- 1 zxm zxm 764 6月 28 14:37 maven-metadata-nexus-test-public.xml
-rw-rw-r-- 1 zxm zxm 40 6月 28 14:37 maven-metadata-nexus-test-public.xml.sha1
-rw-rw-r-- 1 zxm zxm 470 6月 28 14:37 _remote.repositories
-rw-rw-r-- 1 zxm zxm 373 6月 28 14:37 resolver-status.properties
-rw-rw-r-- 1 zxm zxm 3.0K 6月 28 14:18 util-demo-1.1-20230628.061541-2.jar
-rw-rw-r-- 1 zxm zxm 40 6月 28 14:18 util-demo-1.1-20230628.061541-2.jar.sha1
-rw-rw-r-- 1 zxm zxm 2.7K 6月 28 14:18 util-demo-1.1-20230628.061541-2.pom
-rw-rw-r-- 1 zxm zxm 40 6月 28 14:18 util-demo-1.1-20230628.061541-2.pom.sha1
-rw-rw-r-- 1 zxm zxm 3.0K 6月 28 14:25 util-demo-1.1-20230628.062215-3.jar
-rw-rw-r-- 1 zxm zxm 40 6月 28 14:25 util-demo-1.1-20230628.062215-3.jar.sha1
-rw-rw-r-- 1 zxm zxm 2.7K 6月 28 14:25 util-demo-1.1-20230628.062215-3.pom
-rw-rw-r-- 1 zxm zxm 40 6月 28 14:25 util-demo-1.1-20230628.062215-3.pom.sha1
-rw-rw-r-- 1 zxm zxm 3.0K 6月 28 14:37 util-demo-1.1-20230628.062719-4.jar
-rw-rw-r-- 1 zxm zxm 40 6月 28 14:37 util-demo-1.1-20230628.062719-4.jar.sha1
-rw-rw-r-- 1 zxm zxm 2.7K 6月 28 14:37 util-demo-1.1-20230628.062719-4.pom
-rw-rw-r-- 1 zxm zxm 40 6月 28 14:37 util-demo-1.1-20230628.062719-4.pom.sha1
-rw-rw-r-- 1 zxm zxm 3.0K 6月 28 14:37 util-demo-1.1-SNAPSHOT.jar
-rw-rw-r-- 1 zxm zxm 2.7K 6月 28 14:37 util-demo-1.1-SNAPSHOT.pom
可以看出,每次 jar 包内容有变动(非版本号),会从远程仓库,拉取新的 jar 包,根据文件更新时间,可以看出每次多出了哪些文件
_remote.repositories
文件内容如下:
zxm@zxm-pc:~/.m2/repository/org/example/util-demo/1.1-SNAPSHOT$ cat _remote.repositories
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Wed Jun 28 14:37:44 CST 2023
util-demo-1.1-20230628.061541-2.jar>nexus-test-public=
util-demo-1.1-20230628.062719-4.jar>nexus-test-public=
util-demo-1.1-20230628.062215-3.jar>nexus-test-public=
util-demo-1.1-20230628.061541-2.pom>nexus-test-public=
util-demo-1.1-20230628.062215-3.pom>nexus-test-public=
util-demo-1.1-20230628.062719-4.pom>nexus-test-public=
resolver-status.properties
文件内容如下:
时间戳 1687933099721
是 2023-06-28 14:18:19
时间戳 1687934264284
是 2023-06-28 14:37:44
zxm@zxm-pc:~/.m2/repository/org/example/util-demo/1.1-SNAPSHOT$ cat resolver-status.properties
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Wed Jun 28 14:37:44 CST 2023
maven-metadata-google.xml.lastUpdated=1687933099721
maven-metadata-aliyun.xml.error=
maven-metadata-aliyun.xml.lastUpdated=1687933099721
maven-metadata-nexus-test-public.xml.lastUpdated=1687934264284
maven-metadata-google.xml.error=
maven-metadata-nexus-test-public.xml
文件内容如下:
zxm@zxm-pc:~/.m2/repository/org/example/util-demo/1.1-SNAPSHOT$ cat maven-metadata-nexus-test-public.xml
<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1.0">
<groupId>org.example</groupId>
<artifactId>util-demo</artifactId>
<version>1.1-SNAPSHOT</version>
<versioning>
<snapshot>
<timestamp>20230628.062719</timestamp>
<buildNumber>4</buildNumber>
</snapshot>
<lastUpdated>20230628062719</lastUpdated>
<snapshotVersions>
<snapshotVersion>
<extension>jar</extension>
<value>1.1-20230628.062719-4</value>
<updated>20230628062719</updated>
</snapshotVersion>
<snapshotVersion>
<extension>pom</extension>
<value>1.1-20230628.062719-4</value>
<updated>20230628062719</updated>
</snapshotVersion>
</snapshotVersions>
</versioning>
</metadata>
二、release 版本
1. 依赖jar 包
<dependency>
<groupId>org.example</groupId>
<artifactId>util-demo</artifactId>
<version>1.1</version>
</dependency>
2. deploy 包
maven deploy 到仓库的信息:
1.上传 util-demo/1.1/util-demo-1.1.jar
文件
上传 util-demo/1.1/util-demo-1.1.pom
文件
2.下载 util-demo/maven-metadata.xml
文件
3.上传 util-demo/maven-metadata.xml
文件
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ util-demo ---
Uploading to test-release: http://172.17.0.1:8081/repository/test-release/org/example/util-demo/1.1/util-demo-1.1.jar
Uploaded to test-release: http://172.17.0.1:8081/repository/test-release/org/example/util-demo/1.1/util-demo-1.1.jar (3.0 kB at 23 kB/s)
Uploading to test-release: http://172.17.0.1:8081/repository/test-release/org/example/util-demo/1.1/util-demo-1.1.pom
Uploaded to test-release: http://172.17.0.1:8081/repository/test-release/org/example/util-demo/1.1/util-demo-1.1.pom (2.7 kB at 105 kB/s)
Downloading from test-release: http://172.17.0.1:8081/repository/test-release/org/example/util-demo/maven-metadata.xml
Downloaded from test-release: http://172.17.0.1:8081/repository/test-release/org/example/util-demo/maven-metadata.xml (325 B at 16 kB/s)
Uploading to test-release: http://172.17.0.1:8081/repository/test-release/org/example/util-demo/maven-metadata.xml
Uploaded to test-release: http://172.17.0.1:8081/repository/test-release/org/example/util-demo/maven-metadata.xml (325 B at 6.9 kB/s)
3. 拉取 jar 包
maven 拉取 jar 包信息如下:
下载文件
1.util-demo/1.1/util-demo-1.1.pom
2.util-demo/1.1/util-demo-1.1.jar
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1/util-demo-1.1.pom
Downloaded from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1/util-demo-1.1.pom (2.7 kB at 46 kB/s)
Downloading from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1/util-demo-1.1.jar
Downloaded from nexus-test-public: http://localhost:8081/repository/test-public/org/example/util-demo/1.1/util-demo-1.1.jar (3.0 kB at 300 kB/s)
4. 本地仓库
本地仓库文件有:
zxm@zxm-pc:~/.m2/repository/org/example/util-demo/1.1$ pwd
/home/zxm/.m2/repository/org/example/util-demo/1.1
zxm@zxm-pc:~/.m2/repository/org/example/util-demo/1.1$ ls -lh
总用量 20K
-rw-rw-r-- 1 zxm zxm 214 6月 28 16:38 _remote.repositories
-rw-rw-r-- 1 zxm zxm 3.0K 6月 28 16:38 util-demo-1.1.jar
-rw-rw-r-- 1 zxm zxm 40 6月 28 16:38 util-demo-1.1.jar.sha1
-rw-rw-r-- 1 zxm zxm 2.7K 6月 28 16:38 util-demo-1.1.pom
-rw-rw-r-- 1 zxm zxm 40 6月 28 16:38 util-demo-1.1.pom.sha1
_remote.repositories
文件内容如下:
zxm@zxm-pc:~/.m2/repository/org/example/util-demo/1.1$ cat _remote.repositories
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Wed Jun 28 16:38:59 CST 2023
util-demo-1.1.jar>nexus-test-public=
util-demo-1.1.pom>nexus-test-public=
三、上传和下载源码
maven-source-plugin
可在打包的时候,多出一个 util-demo-1.2-sources.jar
后缀的 jar 包,deploy 的时候会把此 jar 自动上传到服务器
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
使用命令,可以下载源码 jar 包,命令如下:
mvn dependency:sources