软件编程
位置:首页>> 软件编程>> java编程>> 解读maven配置阿里云镜像问题

解读maven配置阿里云镜像问题

作者:普通网友  发布时间:2023-02-28 11:57:56 

标签:maven,配置,阿里云,镜像

maven配置阿里云镜像

打开maven配置文件,找到标签,添加如下:

<mirrors>

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

</mirrors>

设置全局的jdk,在配置文件配置如下:

<profile>  
    <id>jdk18</id>  
    <activation>  
        <activeByDefault>true</activeByDefault>  
        <jdk>1.8</jdk>  
    </activation>  
    <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>  

设置局部的jdk,在项目的pom,xml文件中添加如下build元素

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

maven配置阿里云镜像仓库不生效

问题

在{MAVEN_HOME}/conf/settings.xml中添加镜像配置:

  <mirrors>
    <mirror>
        <id>aliyunmaven</id>
        <mirrorOf>*</mirrorOf>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>

但是项目更新时仍然会从http://repo.maven.apache.org/maven2下载依赖。

解决方法

在项目的pom.xml中添加如下配置

  <repositories>
    <repository>
      <id>central</id>
      <url>https://maven.aliyun.com/repository/public</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <url>https://maven.aliyun.com/repository/public</url>
    </pluginRepository>
  </pluginRepositories>

原因

项目的pom会继承自super pom,在super pom中指定了从仓库的地址:

<repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
 
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

因此需要在项目中覆盖这一配置。

来源:https://blog.csdn.net/qq_46416934/article/details/124313349

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com