maven · 2020-09-25 0

maven配置仓库和部署jar包及blocked mirror

搭建 nexus3

使用 docker 搭建 nexus3

docker pull sonatype/nexus3:3.56.0
docker run -d --name nexus3_1 -p 8081:8081 -e TZ=Asia/Shanghai sonatype/nexus3:3.56.0

创建仓库

test-releae仓库: hosted类型,Release库

test-snapshot仓库: hosted类,Snapshot库

test-public仓库:group类型,仓库组,配置 test-releae仓库和test-snapshot仓库

快照版本和正式版本的主要区别在于,本地获取这些依赖的机制有所不同。假设你依赖一个库的正式版本,构建的时候构建工具会先在本次仓库中查找是否已经有了这个依赖库,如果没有的话才会去远程仓库中去拉取。

nexus

一、mirror镜像

${user.home}/.m2/settings.xml 文件中配置

mirror 相当于一个拦截器,它会拦截 mavenremote repository 的相关请求。

mirror 定义了两个Repository之间的镜像关系,<mirrorOf></mirrorOf>指向被镜像的镜像Repository ID,如果该镜像仓库需要认证,则配置<server></server>
由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,Maven仍将无法访问被镜像仓库,因而将无法下载构件

配置同一个 repository 多个 mirror 时,相互之间是备份关系,只有当仓库连不上时才会切换到另一个,而如果能连上但是找不到依赖时是不会尝试下一个 mirror 地址的。

  1. <mirrorOf>*</mirrorOf> 匹配所有远程仓库
  2. <mirrorOf>repo1,repo2</mirrorOf> 匹配仓库repo1和repo2,使用逗号分隔多个远程仓库,发送repo1或repo2的请求,转到此Repository
  3. <mirrorOf>*,!repo1</miiroOf> 匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除
<mirrors>
    <!-- 配置阿里镜像 -->
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>https://maven.aliyun.com/repository/public/</url>
        <mirrorOf>*</mirrorOf>
    </mirror>
    <!--
    配置本地私服nexus3
    <mirror>
        <id>nexus</id>
        <name>Nexus Mirror</name>
        <url>http://localhost:8081/repository/test-public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    -->
</mirrors>

二、repository仓库

1.全局配置

${user.home}/.m2/settings.xml 文件中配置

<updatePolicy></updatePolicy> 用于配置对于SNAPSHOT版本向远程仓库中查找的频率。

频率共有四种,分别是always、daily (默认策略)、interval:X(X为整数,单位是分钟)、never。

当本地仓库中存在需要的依赖项目时,always是每次都去远程仓库查看是否有更新,daily是只在第一次的时候查看是否有更新,当天的其它时候则不会查看;interval允许设置一个分钟为单位的间隔时间,在这个间隔时间内只会去远程仓库中查找一次,never是不会去远程仓库中查找(这种就和正式版本的行为一样了)

<profiles>
    <profile>
        <id>jdk-1.8</id>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>

    <!--设置maven私库信息-->
    <profile>
        <id>nexus</id>
        <repositories>
            <repository>
                <id>nexus-test-public</id>
                <name>Nexus</name>
                <url>http://localhost:8081/repository/test-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>nexus-test-public</id>
                <name>Nexus</name>
                <url>http://localhost:8081/repository/test-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>

<!-- 激活jdk-1.8的配置 激活私库信息的配置 -->
<activeProfiles>
    <activeProfile>jdk-1.8</activeProfile>
    <activeProfile>nexus</activeProfile>
</activeProfiles>

另:

对于jdk配置,为了方便项目迁移,也可以在pom文件中配置

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

2.局部配置

pom.xml文件中配置

<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>
</repositories>

三、部署jar包

1.配置验证

${user.home}/.m2/settings.xml

<!--设置私库认证信息-->
<!--
    表示当需要连接到一个私有服务器的时候需要的认证信息
    发布的服务器,发布的位置在POM中配置,以id为关联
-->
<servers>
    <server>
        <id>test-release</id>
        <username>test-release-user</username>
        <password>123456</password>
    </server>
    <server>
        <id>test-snapshot</id>
        <username>test-snapshot-user</username>
        <password>123456</password>
    </server>
</servers>

2.配置部署仓库

pom.xml

<build>
    <plugins>
        <!--
            如果需要把源码打包,用maven-source-plugin插件
            打包的时候,会把源码打包成jar文件
            会生成两个jar文件,一个存放class文件的jar包,一个存放java文件的jar包
         -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <attach>true</attach>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<distributionManagement>
    <repository>
        <id>test-release</id>
        <name>test-release</name>
        <url>http://localhost:8081/repository/test-release/</url>
    </repository>

    <snapshotRepository>
        <id>test-snapshot</id>
        <name>test-snapshot</name>
        <url>http://localhost:8081/repository/test-snapshot/</url>
    </snapshotRepository>
</distributionManagement>

四、仓库搜索顺序

maven 仓库分为:本地仓库 (local)、远程仓库 (remote)

远程仓库分为:中央仓库 (central)、私服、其他公共仓库

  1. 中央仓库,这是默认的仓库
  2. 镜像仓库,通过 sttings.xml 中的 settings.mirrors.mirror 配置
  3. 全局profile仓库,通过 settings.xml 中的 settings.repositories.repository 配置
  4. 项目仓库,通过 pom.xml 中的 project.repositories.repository 配置
  5. 项目profile仓库,通过 pom.xml 中的 project.profiles.profile.repositories.repository 配置
  6. 本地仓库

搜索顺序如下:

local_repo > settings_profile_repo > pom_profile_repo > pom_repositories > central

五、Blocked mirror for repositories

从 3.8.1 版本,Maven 引入了一条锁定所有使用 HTTP 协议(不安全连接)仓库的默认配置。

对于 http 请求的仓库,会出现

Could not transfer artifact x from/to maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories: [x, default, releases+snapshots), releases (x, default, releases+snapshots)]

解决办法:

方法一:降级 Maven 版本为 3.6.3
方法二:使仓库支持 HTTPS 协议
方法三:使用 mirror 来拦截该仓库的请求,并配置 <blocked>false</blocked>

1.未配置 <mirror><blocked>

<profiles>
    <profile>
        <id>nexus-profile</id>
        <repositories>
            <repository>
                <id>nexus-public-id</id>
                <name>nexus-name</name>
                <url>http://localhost:8081/repository/test-public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>

            <repository>
                <id>aliyun-public-id</id>
                <name>aliyun-public-name</name>
                <url>https://maven.aliyun.com/repository/public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>

            <repository>
                <id>aliyun-google-id</id>
                <name>aliyun-google-name</name>
                <url>https://maven.aliyun.com/repository/google</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>

        <pluginRepositories>
            <pluginRepository>
                <id>nexus-public-plugin-id</id>
                <name>nexus-public-plugin-name</name>
                <url>http://localhost:8081/repository/test-public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>

            <pluginRepository>
                <id>aliyun-public-plugin-id</id>
                <name>aliyun-public-plugin-name</name>
                <url>https://maven.aliyun.com/repository/public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>

            <pluginRepository>
                <id>aliyun-spring-plug-id</id>
                <name>aliyun-spring-plug-name</name>
                <url>https://maven.aliyun.com/repository/spring-plugin</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>

<activeProfiles>
    <activeProfile>nexus-profile</activeProfile>
</activeProfiles>

<localRepository>/home/zxm/.m2/repository</localRepository>

在 maven 项目上执行 mvn -X package,可得到部分日志如下:

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/apache-maven-3.8.6
Java version: 1.8.0_341, vendor: Oracle Corporation, runtime: /opt/jdk1.8.0_341/jre
...
[DEBUG] Reading global settings from /opt/apache-maven-3.8.6/conf/settings.xml
[DEBUG] Reading user settings from /home/zxm/.m2/settings.xml
[DEBUG] Reading global toolchains from /opt/apache-maven-3.8.6/conf/toolchains.xml
[DEBUG] Reading user toolchains from /home/zxm/.m2/toolchains.xml
[DEBUG] Using local repository at /home/zxm/.m2/repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for /home/zxm/.m2/repository
...
INFO] Scanning for projects...
[DEBUG] Using mirror maven-default-http-blocker (http://0.0.0.0/) for nexus-public-id (http://localhost:8081/repository/test-public).
...
[INFO] 
[INFO] -----------------------< org.example:java-demo >------------------------
[INFO] Building java-demo 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
...
[DEBUG] === PROJECT BUILD PLAN ================================================
[DEBUG] Project:       org.example:java-demo:1.0.0
[DEBUG] Dependencies (collect): []
[DEBUG] Dependencies (resolve): [compile, runtime, test]
[DEBUG] Repositories (dependencies): [maven-default-http-blocker (http://0.0.0.0/, default, releases+snapshots, blocked), aliyun-public-id (https://maven.aliyun.com/repository/public, default, releases+snapshots), aliyun-google-id (https://maven.aliyun.com/repository/google, default, releases+snapshots), central (https://repo.maven.apache.org/maven2, default, releases)]
[DEBUG] Repositories (plugins)     : [maven-default-http-blocker (http://0.0.0.0/, default, releases+snapshots, blocked), aliyun-public-plugin-id (https://maven.aliyun.com/repository/public, default, releases+snapshots), aliyun-spring-plug-id (https://maven.aliyun.com/repository/spring-plugin, default, releases+snapshots), central (https://repo.maven.apache.org/maven2, default, releases)]
...
[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.apache.maven.plugins:maven-jar-plugin:2.4:jar (default-jar)
...
[DEBUG] Resolving artifact org.apache.commons:commons-lang3:pom:3.12.0 from [maven-default-http-blocker (http://0.0.0.0/, default, releases+snapshots, blocked), aliyun-public-id (https://maven.aliyun.com/repository/public, default, releases+snapshots), aliyun-google-id (https://maven.aliyun.com/repository/google, default, releases+snapshots), central (https://repo.maven.apache.org/maven2, default, releases)]

根据日志,可看出,查询仓库的顺序为:

  1. maven-default-http-blocker (http://0.0.0.0/, default, releases+snapshots, blocked)
  2. aliyun-public-id (https://maven.aliyun.com/repository/public, default, releases+snapshots)
  3. aliyun-google-id (https://maven.aliyun.com/repository/google, default, releases+snapshots)
  4. central (https://repo.maven.apache.org/maven2, default, releases)

仓库 nexus-public-id 没有生效,maven 使用 maven-default-http-blocker 代替了 nexus-public-id。

jar 包来自 aliyun-public-id

zxm@zxm-pc:~/.m2/repository/org/apache/commons/commons-lang3/3.12.0$ cat _remote.repositories 
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Wed Feb 28 18:13:31 CST 2024
commons-lang3-3.12.0.jar>aliyun-public-id=
commons-lang3-3.12.0.pom>aliyun-public-id=

2.配置 <mirror><blocked>

<mirrors>
    <mirror>
        <id>aliyun-mirror-id</id>
        <name>aliyun-mirror-name</name>
        <url>https://maven.aliyun.com/repository/public</url>
        <mirrorOf>central</mirrorOf>
    </mirror>

    <mirror>
        <id>nexus-mirror-id</id>
        <name>nexus-mirror-name</name>
        <url>http://localhost:8081/repository/test-public</url>
        <mirrorOf>nexus-public-id</mirrorOf>
        <blocked>false</blocked>
    </mirror>

    <mirror>
        <id>nexus-mirror-plugin-id</id>
        <name>nexus-mirror-plugin-name</name>
        <url>http://localhost:8081/repository/test-public</url>
        <mirrorOf>nexus-public-plugin-id</mirrorOf>
        <blocked>false</blocked>
    </mirror>
</mirrors>

<profiles>
    <profile>
        <id>nexus-profile</id>
        <repositories>
            <repository>
                <id>nexus-public-id</id>
                <name>nexus-name</name>
                <url>http://localhost:8081/repository/test-public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>

            <repository>
                <id>aliyun-public-id</id>
                <name>aliyun-public-name</name>
                <url>https://maven.aliyun.com/repository/public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>

            <repository>
                <id>aliyun-google-id</id>
                <name>aliyun-google-name</name>
                <url>https://maven.aliyun.com/repository/google</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>

        <pluginRepositories>
            <pluginRepository>
                <id>nexus-public-plugin-id</id>
                <name>nexus-public-plugin-name</name>
                <url>http://localhost:8081/repository/test-public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>

            <pluginRepository>
                <id>aliyun-public-plugin-id</id>
                <name>aliyun-public-plugin-name</name>
                <url>https://maven.aliyun.com/repository/public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>

            <pluginRepository>
                <id>aliyun-spring-plug-id</id>
                <name>aliyun-spring-plug-name</name>
                <url>https://maven.aliyun.com/repository/spring-plugin</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>

<activeProfiles>
    <activeProfile>nexus-profile</activeProfile>
</activeProfiles>

<localRepository>/home/zxm/.m2/repository</localRepository>

在 maven 项目上执行 mvn -X package,可得到部分日志如下:

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/apache-maven-3.8.6
Java version: 1.8.0_341, vendor: Oracle Corporation, runtime: /opt/jdk1.8.0_341/jre
...
[DEBUG] Reading global settings from /opt/apache-maven-3.8.6/conf/settings.xml
[DEBUG] Reading user settings from /home/zxm/.m2/settings.xml
[DEBUG] Reading global toolchains from /opt/apache-maven-3.8.6/conf/toolchains.xml
[DEBUG] Reading user toolchains from /home/zxm/.m2/toolchains.xml
[DEBUG] Using local repository at /home/zxm/.m2/repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for /home/zxm/.m2/repository
...
[INFO] Scanning for projects...
[DEBUG] Using mirror nexus-mirror-id (http://localhost:8081/repository/test-public) for nexus-public-id (http://localhost:8081/repository/test-public).
[DEBUG] Using mirror aliyun-mirror-id (https://maven.aliyun.com/repository/public) for central (https://repo.maven.apache.org/maven2).
...
[INFO] 
[INFO] -----------------------< org.example:java-demo >------------------------
[INFO] Building java-demo 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
...
[DEBUG] === PROJECT BUILD PLAN ================================================
[DEBUG] Project:       org.example:java-demo:1.0.0
[DEBUG] Dependencies (collect): []
[DEBUG] Dependencies (resolve): [compile, runtime, test]
[DEBUG] Repositories (dependencies): [nexus-mirror-id (http://localhost:8081/repository/test-public, default, releases+snapshots), aliyun-public-id (https://maven.aliyun.com/repository/public, default, releases+snapshots), aliyun-google-id (https://maven.aliyun.com/repository/google, default, releases+snapshots), aliyun-mirror-id (https://maven.aliyun.com/repository/public, default, releases)]
[DEBUG] Repositories (plugins)     : [nexus-mirror-plugin-id (http://localhost:8081/repository/test-public, default, releases+snapshots), aliyun-public-plugin-id (https://maven.aliyun.com/repository/public, default, releases+snapshots), aliyun-spring-plug-id (https://maven.aliyun.com/repository/spring-plugin, default, releases+snapshots), aliyun-mirror-id (https://maven.aliyun.com/repository/public, default, releases)]
...
[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.apache.maven.plugins:maven-jar-plugin:2.4:jar (default-jar)
...
[DEBUG] Resolving artifact org.apache.commons:commons-lang3:pom:3.12.0 from [nexus-mirror-id (http://localhost:8081/repository/test-public, default, releases+snapshots), aliyun-public-id (https://maven.aliyun.com/repository/public, default, releases+snapshots), aliyun-google-id (https://maven.aliyun.com/repository/google, default, releases+snapshots), aliyun-mirror-id (https://maven.aliyun.com/repository/public, default, releases)]

根据日志,可看出,查询仓库的顺序为:

  1. nexus-mirror-id (http://localhost:8081/repository/test-public, default, releases+snapshots)
  2. aliyun-public-id (https://maven.aliyun.com/repository/public, default, releases+snapshots)
  3. aliyun-google-id (https://maven.aliyun.com/repository/google, default, releases+snapshots)
  4. aliyun-mirror-id (https://maven.aliyun.com/repository/public, default, releases)

jar 包来自 nexus-mirror-id

zxm@zxm-pc:~/.m2/repository/org/apache/commons/commons-lang3/3.12.0$ cat _remote.repositories 
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Wed Feb 28 18:20:14 CST 2024
commons-lang3-3.12.0.pom>nexus-mirror-id=
commons-lang3-3.12.0.jar>nexus-mirror-id=