Maven的settings.xml配置阿里云及私服,拿来即用,不需修改!

Maven的settings.xml文件标准实用配置

默认JDK环境

默认jdk采用jdk1.8


idea配置maven

配置阿里云镜像和私服镜像, 并且先从阿里云下载, 下载不到的再去私服下载

<?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>${user.home}/.m2/repository</localRepository>
  
    <!-- Apache Maven 配置 -->
    <pluginGroups/>
    <proxies/>

    <!-- 私服发布的用户名密码 -->
    <servers>
        <server>
            <id>releases</id>
            <username>deployment</username>
            <password>He2019</password>
        </server>
        <server>
            <id>snapshots</id>
            <username>deployment</username>
            <password>He2019</password>
        </server>
    </servers>
    
    <!-- 阿里云镜像 -->
    <mirrors>
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <!-- https://maven.aliyun.com/repository/public/ -->
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>

    <!-- 配置: java8, 先从阿里云下载, 没有再去私服下载  -->
    <!-- 20190929 hepengju 测试结果: 影响下载顺序的是profiles标签的配置顺序(后面配置的ali仓库先下载), 而不是activeProfiles的顺序 -->
    <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>

        
        <!-- Nexus私服配置: 第三方jar包下载, 比如oracle的jdbc驱动等 -->
        <profile>
            <id>dev</id>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>public</id>
                    <name>Public Repositories</name>
                    <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>
        
        <!-- 阿里云配置: 提高国内的jar包下载速度 -->
        <profile>
            <id>ali</id>
            <repositories>
                <repository>
                    <id>alimaven</id>
                    <name>aliyun maven</name>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>alimaven</id>
                    <name>aliyun maven</name>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>

    </profiles>
    
    <!-- 激活配置 --> 
    <activeProfiles>
        <activeProfile>jdk1.8</activeProfile>
        <activeProfile>dev</activeProfile>
        <activeProfile>ali</activeProfile>
    </activeProfiles>
</settings>

拿来即用,复制进去即可。

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Mavensettings.xml文件是用来配置Maven的各种设置的,其中包括配置私服的设置。配置私服可以让我们在本地构建项目时,使用本地的私服来下载依赖包,从而提高构建速度。 在settings.xml文件中,我们需要添加一个<mirrors>标签,用来配置私服的镜像。具体配置如下: ``` <mirrors> <mirror> <id>my-mirror</id> <url>http://my-private-repo.com/maven2</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> ``` 其中,<id>标签用来指定镜像的ID,<url>标签用来指定私服的地址,<mirrorOf>标签用来指定需要被镜像的仓库。在这个例子中,我们使用了通配符“*”,表示所有的仓库都需要被镜像。 除了<mirrors>标签外,我们还需要添加一个<servers>标签,用来配置私服的认证信息。具体配置如下: ``` <servers> <server> <id>my-private-repo</id> <username>my-username</username> <password>my-password</password> </server> </servers> ``` 其中,<id>标签用来指定私服的ID,<username>标签用来指定私服的用户名,<password>标签用来指定私服的密码。 配置settings.xml文件后,我们就可以在本地构建项目时,使用私服来下载依赖包了。 ### 回答2: Maven是一个流行的构建工具,它可以帮助我们管理和构建项目。一个常见的情况是,我们需要将一些私有库存储在一个私服上,而不是让 Maven 去下载。 这时候,我们需要在 Maven 环境中配置私服。一个常见的配置方式是修改 Mavensettings.xml 文件。 settings.xml 文件是 Maven配置文件,它通常存储在用户主目录下的 .m2 目录中。在 settings.xml 文件中,我们可以定义一些 Maven 的全局配置,例如源代码库和私服。我们可以通过编辑这个文件,为 Maven 配置私服。 首先,我们需要找到 settings.xml 文件。如果你在使用 Maven 命令行工具,可以直接在命令行中输入以下命令: ```shell $ mvn help:effective-settings ``` 这会告诉你有效的 settings.xml 文件的位置。在这个文件中,我们可以添加以下内容来配置私服: ```xml <settings> ... <servers> <server> <id>my-nexus-repo</id> <username>your_username</username> <password>your_password</password> </server> </servers> ... <mirrors> <mirror> <id>my-nexus-repo</id> <name>my-nexus-repo</name> <url>http://your.prvate.repo/nexus/content/groups/public/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> ... </settings> ``` 在这个 XML 块中,我们配置了一个 <servers> 标签,其中添加了一个 <server> 块,定义了私服的 ID、用户名和密码。 接下来,我们在 <mirrors> 中添加一个 <mirror> 块来定义镜像。在这种情况下,我们定义了一个 ID 和 Name,并指定了我们私服的 URL。通过这个设置,Maven 将会从我们配置私服中查找库。 当我们配置settings.xml 后,我们需要在 pom.xml 文件中添加以下 XML 块来使用这个私服: ```xml <project> ... <repositories> <repository> <id>my-nexus-repo</id> <url>http://your.private.repo/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> ... </project> ``` 在这个 XML 块中,我们定义了一个 <repositories> 标记,它定义了一个 ID 和 URL,这些信息与我们之前在 settings.xml 文件中定义的一致。同时,我们设置了两个布尔值,用来启用 Maven 使用我们私服的 release 和 snapshot 库。 通过这个配置,我们就能在使用 Maven 命令下载依赖库时,从我们配置私服中查找库了。 ### 回答3: MavenJava开发中非常流行的构建工具之一。在使用Maven构建项目时,往往需要从外部库中下载相关的依赖包。但是有些依赖包可能因为网络问题或者版本问题无法下载,这个时候就需要搭建本地私服。下面就是介绍如何在Maven配置settings.xml文件来使用私服。 一、准备工作 首先需要准备好一个私服,可以使用Sonatype Neco等开源的私服,也可以使用商业类的JFrog Artifactory等。在安装配置私服后,需要在私服中添加需要的依赖包,以供自己的项目使用。 二、编辑settings.xml配置文件 Maven配置文件位于Maven安装目录下的conf文件夹中,名为settings.xml。我们可以通过编辑该文件,来配置Maven使用私服。 1.添加<mirrors>节点 <mirrors>节点是私服配置的第一部分,它可以用来指定镜像节点。由于Maven中央库的下载速度较慢,因此我们可以通过指定镜像节点来加快下载速度。配置如下: ``` <mirrors>     <mirror>       <id>nexus</id>       <mirrorOf>*</mirrorOf>       <url>http://your-host:8081/repository/maven-public/</url>     </mirror> </mirrors> ``` 2.添加<settingsProfiles>节点 <settingsProfiles>节点是用于添加配置项的节点,它包含多个<profile>节点。我们可以通过在<profiles>节点下添加<profile>节点来指定需要使用的的私服信息。配置如下: ``` <profiles>   <profile>     <id>myprofile</id>     <repositories>       <repository>         <id>nexus</id>         <url>http://your-host:8081/repository/maven-releases/</url>         <releases>           <enabled>true</enabled>           <checksumPolicy>fail</checksumPolicy>         </releases>         <snapshots>           <enabled>true</enabled>           <checksumPolicy>warn</checksumPolicy>         </snapshots>       </repository>     </repositories>   </profile> </profiles> ``` 其中,id用来指定profile的ID,repositories节点配置了具体的私服信息,id节点指定了私服的ID,url节点指定了私服的地址,releases和snapshots节点分别指定了私服是否支持发布和快照版本。 3.添加<settingsActiveProfiles>节点 最后,我们需要添加<settingsActiveProfiles>节点来激活我们指定的profile,配置如下: ``` <activeProfiles>   <activeProfile>myprofile</activeProfile> </activeProfiles> ``` 这样配置文件就编辑完毕了,保存之后就可以使用该私服了。 三、总结 通过配置settings.xml文件,可以有效的管理本地私服,提高Maven的构建效率。同样,在多人开发或者不同项目使用不同私服时,可以根据需要修改settings.xml文件中的镜像节点和<profile>节点,灵活进行管理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿呆攻防

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值