zhangs1986
  在实际项目中会存在多个仓库,包括我们自建的Nexus私有仓库和阿里仓,这里就需要设置多仓的顺序,防止jar包不在其中一个仓库时会自动从另外一个仓库中拉取。
  Maven的Setting配置中有mirror和repository,它们的作用都是配置远程maven仓库的地址。repository就是直接配置站点地址,mirror则是作为站点的镜像,代理某个或某几个站点的请求,实现对repository的完全代替。
   
  有两种形式可以配置多个repository, 配置多个profile或者在同一个profile中配置多个repository.配置多profile时,还需要配置activeProfiles使配置生效。
  下载依赖时,maven会按照配置从上到下的顺序,依次尝试从各个地址下载,成功下载为止。
  无论是配置国内的maven仓库,还是配置nexus之类私服,都可以直接配置成repository, 这样即使配置的这些仓库有些问题导致一些包下不下来,也可以继续用别的仓库尝试。
 
  <repository>时<id>似乎也没什么用,如果你只是在pom.xml中配置个仓库,这个id是没什么用的,可以随便写。这个id是配合上面讲的mirror一块使用的,还记得mirrorOf吗,我们配置mirrorOf为<mirrorOf>central</mirrorOf>是,mirror中的url会将默认的central仓库的url给覆盖了,所以这里的<repository>标签下的id是给mirrorOf用的。当repository中的id与mirrorOf一致时,mirrorOf中的url就会覆盖repository中的url地址。
 
  在Mirrors部分可以配置多个镜像仓库,但是在该部分配置多个仓库,并不能提供自动查询多个仓库的功能,默认还是取第一个仓库进行查询。
  我们这里采用设置profiles的方式来达到想要的目的,直接上配置:
<?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:/m2/repository</localRepository>
  <pluginGroups>
    <pluginGroup>org.mortbay.jetty</pluginGroup>
  </pluginGroups>
  <proxies>
  </proxies>
  <servers>
    <server>
      <id>nexus-releases</id>
      <username>ali</username>
      <password>123456</password>
    </server>
  </servers>
  <mirrors>
  </mirrors>
  <profiles>
    <profile>
      <id>jdk-1.8</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>
    <profile>
      <id>downloadSources</id>
      <properties>
        <downloadSources>true</downloadSources>
        <downloadJavadocs>true</downloadJavadocs>
      </properties>
    </profile>
    <profile>
      <id>aliyun</id> 
      <repositories>
        <repository>
          <id>aliyun</id> 
          <url>https://maven.aliyun.com/repository/public</url> 
          <releases>
            <enabled>true</enabled>
          </releases> 
          <snapshots>
            <enabled>true</enabled> 
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>
    <profile>
      <id>nexus-releases</id>
      <repositories>
        <repository>
          <id>nexus-releases</id>
          <url>http://10.3.87.5:8082/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>
  <activeProfiles>
   <activeProfile>aliyun</activeProfile>
   <activeProfile>nexus-releases</activeProfile>
   </activeProfiles>
</settings>

 注意:这里面配置了maven.compiler.source,当环境中有jdk1.8以下版本时用maven打包时会报jdk低版本的提示,需要统一打包时jdk的版本,因此需要在此指定

 

相关文章: