setting.xml文件配置释义

发布时间 2023-10-10 10:52:11作者: 终不似。少年游

maven下载jar规则

  1. maven下载jar包优先从配置的本地仓库localRepository查找jar,找不到会去配置的远程仓库中下载jar
  2. 配置的远程仓库都有对应的id, 可以根据 标签填对应的仓库的id,代表,从这个仓库下载jar的时候,会走对应的镜像
  3. 如果下载不到jar,会报错
  4. plugin会从配置的pluginRepository中去下载,同上

setting.xml配置解释

server标签的作用

使用mvn install时,会把项目打的包安装到本地maven仓库,
使用mvn deploye时,会把项目打的包部署到远程maven仓库,这样有权限访问远程仓库的人都可以访问你的jar包
通过在pom.xml中使用 distributionManagement 标签,来告知maven 部署的远程仓库地址,例子如下
由于有的远程仓库有密码权限,如果没有在setting.xml中的<server>中进行配置,就会报出401错误,标签的id要和distributionManagement 中 仓库的id一致,详情见下面代码


	<distributionManagement>  
		<!-- 正式仓库 -->
	    <repository>  
	        <id>releases</id>  
	        <name>releases</name>  
			<url>http://nexus.xinfangsheng.info:8081/repository/maven-releases/</url>  
	    </repository> 
	     <!-- 快照仓库 -->
	    <snapshotRepository>
			<id>snapshot</id>  
		    <name>snapshot</name>  
		    <url>http://nexus.xinfangsheng.info:8081/repository/maven-snapshots/</url>  
    </snapshotRepository>  
</distributionManagement>

  <?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">

   <!-- 默认的值是${user.home}/.m2/repository -->
   <localRepository>E:\apache\repository</localRepository>

   <!-- 如果Maven要试图与用户交互来得到输入就设置为true,否则就设置为false,默认为true。 -->
   <interactiveMode>true</interactiveMode>

   <!-- 如果Maven使用${user.home}/.m2/plugin-registry.xml来管理plugin的版本,就设置为true,默认为false。 -->
   <usePluginRegistry>false</usePluginRegistry>

   <!-- 如果构建系统要在离线模式下工作,设置为true,默认为false。 如果构建服务器因为网络故障或者安全问题不能与远程仓库相连,那么这个设置是非常有用的。 -->
   <offline>false</offline>

   <servers>
       <!-- server
        | Specifies the authentication information to use when connecting to a particular server, identified by
        | a unique name within the system (referred to by the 'id' attribute below).
        |
        | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
        |       used together.
        |
       -->
       <!-- server标签的作用 ,如下 -->
       <!-- 使用mvn install时,会把项目打的包安装到本地maven仓库 -->
       <!-- 使用mvn deploye时,会把项目打的包部署到远程maven仓库,这样有权限访问远程仓库的人都可以访问你的jar包 -->
       <!-- 通过在pom.xml中使用 distributionManagement 标签,来告知maven 部署的远程仓库地址,-->

       <server>
           <id>snapshot</id>
           <username>developer</username>
           <password>*******</password>
           <filePermissions>664</filePermissions>
           <directoryPermissions>775</directoryPermissions>
       </server>
        <server>
           <id>releases</id>
           <username>developer</username>
           <password>***********</password>
           <filePermissions>664</filePermissions>
           <directoryPermissions>775</directoryPermissions>
       </server>
   </servers>

   <mirrors>
       <mirror>
           <id>aliyunmaven</id>
           <mirrorOf>*</mirrorOf><!--*代表所有的jar包都到阿里云下载-->
           <!--<mirrorOf>central</mirrorOf>--><!--central代表只有中央仓库的jar包才到阿里云下载-->
           <!-- maven 会有默认的id为 “central” 的中央仓库-->  
         <name>阿里云公共仓库</name>
           <url>https://maven.aliyun.com/repository/public</url>
       </mirror>

       <!--老版阿里云公共仓库-->
       <!--
       <mirror>
           <id>nexus-aliyun</id>
           <mirrorOf>central</mirrorOf>
           <name>Nexus aliyun</name>
           <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
       </mirror>
       -->
       
       <!-- 中央仓库在中国的镜像 -->
       <mirror>
           <id>maven.net.cn</id>
           <name>Mirror from Maven in china</name>
           <url>http://maven.net.cn/content/groups/public/</url>
           <mirrorOf>central</mirrorOf>
       </mirror>
   </mirrors>




   <!-- settings.xml中的profile是pom.xml中的profile的简洁形式。 它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。 
       profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。 如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。 -->
    <!-- 如果setting中配置了 repository,则等于项目的pom中配置了 -->
   <profiles>
       <profile>
         <!-- 指定该 profile的id --> 
           <id>dev</id>
           <!-- 远程仓库-->
           <repositories>
               <!-- 阿里云远程仓库-->
               <repository>
                   <id>aliyun</id>
                   <name>aliyun maven Repository</name>
                   <url>https://maven.aliyun.com/repository/public</url>
                 <!-- 只从该仓库下载 release版本 -->
                   <releases>
                       <enabled>true</enabled>
                   </releases>
                   <snapshots>
                       <enabled>false</enabled>
                   </snapshots>
               </repository>
               <repository>
                   <id>spring-milestone</id>
                   <name>Spring Milestone Repository</name>
                   <url>http://repo.spring.io/milestone</url>
                   <releases>
                       <enabled>true</enabled>
                   </releases>
                   <snapshots>
                       <enabled>false</enabled>
                   </snapshots>
                   <layout>default</layout>
               </repository>
               <repository>
                   <id>spring-snapshot</id>
                   <name>Spring Snapshot Repository</name>
                   <url>http://repo.spring.io/snapshot</url>
                   <releases>
                       <enabled>false</enabled>
                   </releases>
                   <snapshots>
                       <enabled>true</enabled>
                   </snapshots>
                   <layout>default</layout>
               </repository>
           </repositories>
           <!-- 镜像,添加了会先从阿里云仓库下载-->
           <pluginRepositories>
             <!-- 插件仓库。插件从这些仓库下载 -->
               <pluginRepository>
                   <id>aliyun-plugin</id>
                   <url>https://maven.aliyun.com/repository/public</url>
                   <releases>
                       <enabled>true</enabled>
                   </releases>
                   <snapshots>
                       <enabled>false</enabled>
                   </snapshots>
               </pluginRepository>
           </pluginRepositories>
       </profile>
   </profiles>
   <!-- activations是profile的关键,就像POM中的profiles,profile的能力在于它在特定情况下可以修改一些值。 
       而这些情况是通过activation来指定的。 -->
   <!-- <activeProfiles/> -->
 <activeProfiles>
   <activeProfile>dev</activeProfile>
 </activeProfiles>
</settings>