1. 程式人生 > >maven私有庫神坑之:“Downloading: http://repo.maven.apache.org/maven2/”

maven私有庫神坑之:“Downloading: http://repo.maven.apache.org/maven2/”

現象:

即使你配置了私有庫,並且在maven setting.xml中配置了mirror,但是,經常會遇到執行mvn命令的時候,會提醒:
Downloading: http://repo.maven.apache.org/maven2/

原因:

所有自定義pom.xml都是繼承自super pom:
http://maven.apache.org/ref/3.0.4/maven-model-builder/super-pom.html
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>

因此,當maven專案需要下載一些metadata、pom、jar的時候,會優先去中央倉庫下載,導致內網使用者各種報錯!

解決辦法:

在專案pom.xml中新增如下配置:
<repositories>
    <repository>
        <id>central</id>
        <url>http://host:port/content/groups/public</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>central</id>
        <url>http://host:port/content/groups/public</url>
    </pluginRepository>
</pluginRepositories>

參考:https://blog.sonatype.com/using-nexus-3-as-your-repository-part-1-maven-artifacts

https://stackoverflow.com/questions/12789488/how-do-you-stop-maven-from-trying-to-access-http-repo-maven-apache-org