Maven——settings.xml配置


settings.xml配置

原文

<?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:/apache-maven-3.5.2/repository
  localRepository>

  

  

  
  <pluginGroups>
    
  pluginGroups>

  
  <proxies>
    
  proxies>

  
  <servers>
    

    
  servers>

  
  <mirrors>
    
    <mirror>         
      <id>alimavenid>
      <name>aliyun mavenname> 
      <url>http://maven.aliyun.com/nexus/content/groups/public/url> 
      <mirrorOf>centralmirrorOf> 
    mirror> 

  mirrors>

  
  <profiles>
    

    
  profiles>

  
settings>

该settings.xml位于maven文件夹中的conf文件夹下

简介

略过第一段注释(全是废话),看第二段注释

如第二段注释所说,配置有2处:用户配置和全局配置

用户配置路径为${user.home}/.m2/settings.xml,该配置需用户自行创建

全局配置路径为${maven.conf}/settings.xml,该配置为maven自带配置,即原文所在位置

当两者都存在时,maven会合并这2个配置;有相同配置参数时,有效值为用户配置中的参数

看完开头的介绍后,可以开始看配置的正文部分了,为方便查看,先将所有注释去掉,保留默认部分,如下

<?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>${user.home}/.m2/repositorylocalRepository>

  <interactiveMode>trueinteractiveMode>

  <offline>falseoffline>

  <pluginGroups>pluginGroups>

  <proxies>proxies>

  <servers>servers>

  <mirrors>mirrors>

  <profiles>profiles>

  <activeProfiles>activeProfiles>

settings>

localRepository

本地仓库路径,默认路径为${user.home}/.m2/repository。为了避免重装系统而被删除,可在maven文件夹下创建个repository文件夹,将该节点改为该repository文件夹的路径即可

interactiveMode

互动模式,默认值为true。该值使用默认值即可,无需更改

offline

离线模式,默认值为false。当因为各种原因无法与远程仓库连接时,可将该值修改为true

pluginGroups

<pluginGroups>
    <pluginGroup>com.your.pluginspluginGroup>
pluginGroups>

插件组,内部由n个pluginGroup节点组成,默认自带了org.apache.maven.plugins和org.codehaus.mojo。若有特殊的需求是可进行扩展

proxies

<proxies>
    <proxy>
      <id>optionalid>
      <active>trueactive>
      <protocol>httpprotocol>
      <username>proxyuserusername>
      <password>proxypasspassword>
      <host>proxy.host.nethost>
      <port>80port>
      <nonProxyHosts>local.net|some.host.comnonProxyHosts>
    proxy>
proxies>

代理设置,内部由n个proxy节点组成。部分公司出于安全考虑会需要员工通过代理才可上网,这时,就需配置该节点的参数了。

参数说明:

id:唯一标识符

active:是否被激活使用,默认为true。当有多个proxy节点存在时,用于选择使用哪个代理,但同一时间仅有一个代理会被被激活

protocol:协议类型

username:用户名,没有可不填

password:密码,没有可不填

host:IP地址

port:端口号

nonProxyHosts:不使用代理的地址

以上参数在通常配置代理的情况都会使用到,一样填上即可,最终的代理地址为protocol://host:port

servers

<servers>
    <server>
      <id>deploymentRepoid>
      <username>repouserusername>
      <password>repopwdpassword>
    server>
    <server>
      <id>siteServerid>
      <privateKey>/path/to/private/keyprivateKey>
      <passphrase>optional; leave empty if not usedpassphrase>
    server>
servers>

服务器,内部由n个server节点组成。有2种配置方式:一种是通过id匹配,username和password进行认证登录;另一种是通过id匹配指向一个privateKey(私钥)和一个passphrase

mirrors

<mirrors>
    <mirror>
      <id>mirrorIdid>
      <mirrorOf>repositoryIdmirrorOf>
      <name>Human Readable Name for this Mirror.name>
      <url>http://my.repository.com/repo/pathurl>
    mirror>
mirrors>

镜像仓库,内部由n个mirror节点组成。用来代替中央仓库(Maven默认的远程仓库),用于提升传输速度。因此此处可以添加一个镜像,以提升我们下载的速度

<mirrors>
    <mirror>         
      <id>alimavenid>
      <name>aliyun mavenname>
      <url>http://maven.aliyun.com/nexus/content/groups/public/url>
      <mirrorOf>centralmirrorOf> 
    mirror> 
mirrors>

profiles

<profiles>
    <profile>
      <id>jdk-1.4id>
      <activation>
        <jdk>1.4jdk>
      activation>
      <repositories>
        <repository>
          <id>jdk14id>
          <name>Repository for JDK 1.4 buildsname>
          <url>http://www.myhost.com/maven/jdk14url>
          <layout>defaultlayout>
          <snapshotPolicy>alwayssnapshotPolicy>
        repository>
      repositories>
    profile>
    <profile>
      <id>env-devid>
      <activation>
        <property>
          <name>target-envname>
          <value>devvalue>
        property>
      activation>
      <properties>
        <tomcatPath>/path/to/tomcat/instancetomcatPath>
      properties>
    profile>
  profiles>

用于覆盖在一个POM或者profiles.xml文件中的任何相同id的profiles节点

activeProfiles

<activeProfiles>
    <activeProfile>alwaysActiveProfileactiveProfile>
    <activeProfile>anotherAlwaysActiveProfileactiveProfile>
activeProfiles>

activeProfile值为profile中所定义的id,在这里定义的activeProfile总是被激活

 总结

一般情况而言,settings.xml无需配置太多的东西,作为优化,可配置localRepository和mirrors

<?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:/apache-maven-3.5.2/repository
  localRepository>

  <pluginGroups>pluginGroups>

  <proxies>proxies>

  <servers>servers>

  <mirrors>
    <mirror>         
      <id>alimavenid>
      <name>aliyun mavenname> 
      <url>http://maven.aliyun.com/nexus/content/groups/public/url> 
      <mirrorOf>centralmirrorOf> 
    mirror> 
  mirrors>

  <profiles>profiles>

settings>

转载至:https://www.cnblogs.com/s1165482267/p/7928275.html