maven配置阿里云镜像的两种方式

maven配置阿里云镜像的两种方式

配置方式

第一种方式(settings.xml文件)

在mirrors节点下加入一个新的mirror节点,配置阿里镜像地址,完整配置如下:

<mirrors>
 	 <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>
  </mirrors>

第二种方式(pom.xml方式)

修改项目pom.xml,在repositories节点下加入repository节点,配置阿里镜像地址,完整配置如下:

此配置参考renren-genertor项目的pom.xml配置,项目网址:https://gitee.com/renrenio/renren-generator

<repositories>
		<repository>
			<id>public</id>
			<name>aliyun nexus</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>public</id>
			<name>aliyun nexus</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

区别

第一种方式是全局的方式配置

第二种方式只能当前项目生效

解决的问题

问题在线

最近公司配置了maven私服仓库,导致我们项目所需要的依赖需要从私服中拉取,我idea配置maven仓库地址也是私服的,加上如果是内网开发,那么自己笔记本又不是只在公司使用,如果自己学习还要切换maven仓库,相当麻烦

示例测试

我们新建一个springboot项目,定义一个本地没有的springboot版本,在外网使用私服仓库地址打包

image-20210917211442108

报错原因:因为我们使用的外网,所以不能拉去私服仓库依赖

解决方案

适应以上的第二种方式(pom.xml方式)

在pom.xml中配置阿里云镜像

pom.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>springboot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.2.5.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
       省略。。。
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.example.springbootdemo.SpringbootDemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
<repositories>
		<repository>
			<id>public</id>
			<name>aliyun nexus</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>public</id>
			<name>aliyun nexus</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

</project>

打包过程:

image-20210917212150941

打包结束

image-20210917212234290

总结

1.如果工作电脑公司和自己两用,我们自己学习可以在pom.xml配置阿里云镜像,不过每个项目都要配置

2.如果电脑只是自己学习使用,我们在settings.xml中配置全局阿里云镜像即可,一劳永逸




个人csdn博客网址:https://blog.csdn.net/shaoming314

jam

个人博客网址:www.shaoming.club

halo
  • 18
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Maven是一个Java项目的自动化构建工具,而Maven仓库是指存储项目所需各种依赖库的集合。在进行Java项目开发时,我们需要依赖一些类库或工具,这些依赖需要通过Maven进行管理。 阿里云Maven仓库是一个基于HTTP协议的仓库,为Maven项目提供了访问速度高、可靠性好、托管服务等方便的条件。阿里云Maven提供了集中式和分布式两种仓库方式,用户可以根据自己的需求选择不同的方式,同时还提供了多种镜像地址选择,用户可以根据自身情况来选择使用不同的镜像地址。 阿里云Maven仓库镜像是指对阿里云Maven仓库进行的复制,并将其放到其他位置,以提高访问速度和稳定性。用户可以使用阿里云Maven仓库的镜像地址获取所需的依赖,从而加快项目的构建速度。使用阿里云Maven仓库镜像的好处是,可以通过镜像快速获取所需依赖,不必每次从原始仓库下载,大大缩短了构建时间和依赖库下载时间,提高了开发效率。同时,阿里云Maven仓库镜像也提供了多种网络环境下的镜像地址选择,可以帮助用户根据实际情况选择最适合自己的镜像地址,保证项目开发的高效性和稳定性。 ### 回答2: Maven 仓库是 Maven 项目所使用的构件存储库,其中包含了各种项目依赖、插件和构建产物。而阿里云提供了可供用户使用的 Maven 仓库镜像。 使用阿里云 Maven 仓库镜像可以带来很多好处。首先,由于阿里云 Maven 仓库镜像分布在全球范围内,可以极大地提高下载速度,减少下载时间。其次,由于该镜像是由阿里云提供的,因此可以保证镜像的更新和安全,确保用户获得的是正确、可靠的构件。 在项目中使用阿里云 Maven 仓库镜像也很简单。只需在项目的 Maven 配置文件中添加阿里云 Maven 仓库地址,然后在项目构建时,Maven 将从该镜像中获取所需的构件。同时,用户也可以通过 Maven 的设置来指定阿里云 Maven 仓库镜像作为默认仓库,方便以后的操作。 总之,使用阿里云 Maven 仓库镜像不仅能够提高项目的构建效率,还能保证依赖的安全性和可靠性,是项目管理中必不可少的一步。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值