Maven进阶系列-仓库和镜像

Maven进阶系列-仓库和镜像

1. 仓库

1.1 仓库类型

本地仓库、远程中央仓库、公司自己搭建的私有仓库

1.2 寻找jar的基本优先级顺序:

本地仓库 > settings.xml的profile的仓库 > pom.xml的profile的仓库 >pom.xml的仓库 > 中央仓库

maven 寻找得顺序大致可以理解为:
1. 在本地仓库中寻找,如果没有则进入下一步。
2. 在全局应用的私服仓库中寻找,如果没有则进入下一步。
3. 在项目自身的私服仓库中寻找,如果没有则进入下一步。
4. 在中央仓库中寻找,如果没有则终止寻找。

补充:
1. 如果在找寻的过程中,如果发现该仓库有镜像设置,则用镜像的地址代替。
2. 如果仓库的 id 设置成 “central”,则该配置会覆盖 maven 默认的中央仓库配置。

设置仓库的方式有两种,一种是在项目最顶级POM.xml中设置,另一种是在settings.xml中设置。

1.3 仓库优先次序验证示例

在pom.xml中设置

 <repositories>
        <repository>
            <id>pom-repository-first</id>
            <name>pom-repository-first</name>
            <url>http://192.168.100.100:8181/nexus/content/groups/public</url>
        </repository>
        <repository>
            <id>pom-repository-second</id>
            <name>pom-repository-second</name>
            <url>http://192.168.100.100:8182/nexus/content/repositories/thirdparty/</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>Team Nexus Repository</name>
            <url>http://192.168.100.100:8181/nexus/content/groups/public</url>
        </pluginRepository>
    </pluginRepositories>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <profiles>
        <profile>
            <id>pom-profile-first</id>
            <repositories>
                <repository>
                    <id>pom-repository-first</id>
                    <name>pom-repository-first</name>
                    <url>http://192.168.100.100:8084/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <!--配置默认的,配置文件,右侧的maven中,profiles默认选中dev-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    </profiles>

settings.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>D:\Program Files\apache-maven-3.6.1\repository</localRepository>

    <mirrors>
        <!--  设置多个mirrors镜像,镜像只会执行第一个位置mirror。-->
        <!--  配置的多个mirror可以都放着不影响,选取一个镜像下载比较快的放在第一个就行。比如你设置使用自己公司的私有仓库-->
        <!--只有当前一个mirror无法连接的时候,才会去找后一个,类似于备份和容灾。所以当第一个mirror中不存在a.jar的时候,并不会去第二个mirror中查找,甚至于,maven根本不会去其他的mirror地址查询-->
        <mirror>
            <!--当有id为B,A,C的顺序的mirror在mirrors节点中,maven会根据字母排序来指定第一个,所以不管怎么排列,一定会找到A这个mirror来进行查找,当A无法连接,出现意外的情况下,才会去B查询-->
            <id>aliyun</id>
            <name>阿里云仓库地址</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <!--覆盖了Maven自带的central-->
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>


    <profiles>
        <!-- 全局JDK1.8配置 -->
        <profile>
            <id>jdk1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <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>
        <!-- 阿里云配置: 提高国内的jar包下载速度 -->
        <profile>
            <id>settings-profile-aliyun</id>
            <repositories>
                <repository>
                    <id>settings-repository-aliyun</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>settings-profile-first</id>
            <repositories>
                <repository>
                    <id>settings-repository-first</id>
                    <name>settings-repository-first</name>
                    <url>http://192.168.100.100:8183/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>settings-profile-second</id>
            <repositories>
                <repository>
                    <id>settings-repository-second</id>
                    <name>settings-repository-second</name>
                    <url>http://192.168.100.100:8084/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>
    <!--激活仓库配置,拉取依赖会在这些仓库中逐个去找-->
    <activeProfiles>
        <activeProfile>jdk1.8</activeProfile>
        <activeProfile>settings-profile-aliyun</activeProfile>
        <activeProfile>settings-profile-first</activeProfile>
        <activeProfile>settings-profile-second</activeProfile>
    </activeProfiles>

</settings>

2. settings.xml文件

settings 主要由mirrors、servers 和profiles 三部分组成。

2.1 mirrors

mirror相当于一个拦截器,它会拦截 maven 对 remote repository 的相关请求,把请求里的 remote repository 地址,重定向到 mirror 里配置的地址。

2.1.1 没有配置mirror

2.1.2 配置了mirror

上述图中B仓库被称为A仓库的镜像。

2.1.3

<mirrorOf> 标签用于配置镜像仓库的匹配规则,可以指定某个仓库镜像另一个仓库。这样可以加快构建过程中的依赖下载速度,特别是在国内访问外国仓库的情况下。

具体来说,<mirrorOf> 标签用于指定那些仓库将会被当前配置的镜像仓库所代理。比如,如果你在 settings.xml 中配置了一个阿里云的镜像仓库,你可以通过 <mirrorOf> 标签指定这个镜像仓库将代理中央仓库(Central Repository)的某些或全部内容。

例如,以下是一个 Maven settings.xml 配置文件中镜像仓库的示例:

<mirror>
    <id>nexus</id>
    <url>http://nexus/content/groups/public</url>
    <mirrorOf>central</mirrorOf>
</mirror>

在这个示例中,通过 <mirrorOf> 标签配置,表示 naxus 镜像仓库将代理中央仓库(Central Repository),这意味着所有指向中央仓库的请求都会被重定向到 nexus 镜像仓库。

Maven 还支持更高级的镜像配置:

  • <mirrorOf>*</mirrorOf> :匹配所有远程仓库
  • <mirrorOf>repo1,repo2</mirrorOf>:匹配仓库 repo1 和 repo2,使用逗号分隔多个远程仓库。
  • <mirrorOf>*,!repo1</miiroOf>: 匹配所有远程仓库,repo1 除外,使用感叹号将仓库从匹配中排除。

2.2 servers

在Maven的配置文件(settings.xml)中,标签用于配置 Maven 与远程仓库的认证信息,以便于 Maven 在构建或发布时进行认证。

例如,我们要发布 Maven 项目到 Nexus 私服,可以在 标签中配置私服的地址和认证凭证(用户名、密码等)。这样,当 Maven 在构建或发布时访问私服仓库时,会自动使用这里配置的凭证进行认证。以下是一个 标签的示例配置:

<servers>
  <server>
    <id>nexus-releases</id>
    <username>myusername</username>
    <password>mypassword</password>
  </server>
</servers>

其中, 标签指定了私服的 ID,这个 ID 通常是 Nexus 中仓库的名称,例如 “nexus-releases”。 和 分别是私服的访问用户名和密码,用于 Maven 认证访问私服。

这样配置后,在 Maven 执行构建或发布时,就会自动使用这里配置的凭证去访问私服,确保 Maven 在发布时具有足够的权力和权限进行发布操作。

2.3 profiles

在 Maven 的配置文件(settings.xml 或 pom.xml)中, 标签用于配置不同环境下的不同构建选项。例如,有些项目会在开发环境下使用 H2 数据库,而在生产环境下使用 MySQL 数据库,这时就可以使用 标签定义两个不同环境。

每个 标签可以包含独立的一组配置,例如 、、、 等,这些配置将仅在该 激活时生效。可以使用 标签来定义激活该 的条件,例如指定操作系统、Maven 版本、当前构建环境等等。

以下是一个 标签的示例配置:

<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <database.driver>org.h2.Driver</database.driver>
      <database.url>jdbc:h2:mem:testdb</database.url>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <database.driver>com.mysql.jdbc.Driver</database.driver>
      <database.url>jdbc:mysql://localhost/mydb</database.url>
    </properties>
  </profile>
</profiles>

在上述示例中,定义了两个 ,一个 ID 为“dev”,另一个为“prod”。 中设置了 “dev” 是默认激活的配置。在 中配置了不同环境下的数据库驱动和数据库连接 URL。在 Maven 的命令执行中,可以选择使用不同的 Profile 从而达到构建不同的环境的目的。例如:

# 构建 dev 环境
mvn -Pdev clean package

# 构建 prod 环境
mvn -Pprod clean package

通过 标签的使用,可以方便地管理多环境下的不同构建配置,提高了项目的灵活性和可维护性,同时也可以更好地符合软件开发的标准流程。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Maven插件是一种可插拔的工具,可以在Maven构建过程中执行特定的任务。其中,maven-dependency-plugin和maven-surefire-plugin是两个常用的插件。 maven-dependency-plugin插件可以用来管理项目依赖,可以帮助我们列出项目中的依赖关系,复制依赖文件到指定目录,解压依赖文件等。常用的配置包括: - list:列出项目依赖 - copy-dependencies:将所有依赖文件复制到指定目录 - unpack:解压指定的依赖文件 maven-surefire-plugin插件则是用来执行项目的单元测试的。它可以在Maven构建过程中自动执行单元测试,并生成测试报告。常用的配置包括: - includes/excludes:指定要执行的测试类或排除的测试类 - parallel:指定测试是否并行执行 - reportsDirectory:指定测试报告生成的目录 在POM文件中配置这两个插件,可以通过以下方式: ``` <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <includes> <include>**/*Test.java</include> </includes> <parallel>methods</parallel> <threadCount>10</threadCount> <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> </configuration> </plugin> </plugins> </build> ``` 以上是一个简单的POM文件中Maven插件配置maven-dependency-plugin和maven-surefire-plugin的示例,其中maven-dependency-plugin在package阶段执行复制依赖文件的任务,maven-surefire-plugin在test阶段执行单元测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值