Maven设置文件的最佳教程 | 设置.xml示例

552 阅读3分钟

Maven是一个用于java项目的构建工具。现在大多数开发者都使用maven而不是ant,因为maven可以解决他们的依赖关系。

什么是maven settings.xml文件?

settings.xml 该文件用于以多种方式配置maven的执行。它包含以下设置配置

  • 启用安全的本地和远程资源库
  • 代理配置和服务器凭证。
  • maven配置文件和活动配置文件
  • 这些设置是特定于配置Maven的用户的。

用户和全局设置xml文件位置

根据这些文件的位置,有两种类型的settings.xml:

  • 全局设置.xml
  • 用户设置.xml

每个maven项目都使用其中一个来自用户和全局设置文件。

默认的global settings 位置是${M2_HOME}//conf//settings.xml

M2_HOME是maven的安装位置

user settings 在你的机器上的位置是 ~/.m2/settings.xml~ 代表用户家的位置目录。

什么是Maven中的资源库

如你所知,在一个java应用程序中,有很多依赖性的东西,如log4jApache 库。这些被称为dependencies ,用于你的项目。

你必须在pom.xml中定义所有的依赖项。

这些库在apache资源库中可用。因此,当我们想在项目中使用maven时,你必须下载所有这些文件。
如果我们在项目中配置了依赖项,这些库(jar文件)就会被下载到本地仓库,然后再下载到公司主机。

本地资源库包含从不同库中下载的java库。

什么是maven的settings.xml中的镜像?


<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<mirrors>
  <mirror>
    <id>Asia</id>
    <name>Asia Central</name>
    <url>http://asia.maven.org/maven2</url>
    <mirrorOf>central</mirrorOf>
  </mirror>
</mirrors>

在上面的例子中,如果有依赖项试图从中央仓库下载,它就会点击镜像网址而不是maven中央仓库。

这使我们能够阻止maven尝试从maven中央仓库访问。

Settings.xml 示例文件

settings.xml文件包含以下项目

  • profiles:配置dev、test和stage等环境的特定配置文件
  • servers :配置应用服务器的详细信息
  • Repositories :远程版本库位置,如maven中央版本库
  • plugin repositories:插件库的远程位置
  • activeProfile配置的活动配置文件
  • proxies 配置http和https代理。

下面是settings.xml模板的例子

 
 <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"> 
  <profiles>
    <profile>
        <repositories>
        <repository>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
         </pluginRepository>
      </pluginRepositories>
</profile> 
</profiles>

<server>
        <id>Websphere</id>
        <username>username</username>
        <password>password</password>
    </server>
  <proxies>  
    <proxy>  
      <id>userspecificproxy</id>  
      <active>true</active>  
      <protocol>http</protocol>  
      <host>proxy.com</host>  
      <port>8080</port>  
      <username>usernameofproxy</username>  
      <password>passwordofproxy</password>  
      <nonProxyHosts>configure here</nonProxyHosts>  
    </proxy>  
  </proxies>  
    <activeProfiles>
    <activeProfile>env-dev</activeProfile>
  </activeProfiles>

</settings>  

如何找到哪个settings.xml文件被使用?

我们可以在全局和本地位置配置settings.xml

如何确定应用程序中使用的是哪个settings.xml文件?

没有直接的maven命令来查找用户的settings.xml。

如果你在mvn命令中启用调试日志,我们可以找到该位置。

然后输出:

[DEBUG] Reading global settings from A:\Java\apache-maven-3.3.9\bin\..\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\Kiran\.m2\settings.xml
[DEBUG] Reading global toolchains from A:\Java\apache-maven-3.3.9\bin\..\conf\toolchains.xml
[DEBUG] Reading user toolchains from C:\Users\Kiran\.m2\toolchains.xml
[DEBUG] Using local repository at C:\Users\Kiran\.m2\repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\Kiran\.m2\repository

它将给出全局设置和用户设置位置文件的详细信息。

如何在java项目中读取maven设置文件?

有一个maven命令help:effective-settings 来获取设置xml内容。

mvn help:effective-settings

该命令解决了一些问题

  • 如何获得maven本地仓库的位置
  • 如何找到maven中央仓库的位置
Effective user-specific configuration settings:

<?xml version="1.0" encoding="Cp1252"?>
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin on 2021-06-15T15:12:25+05:30            -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
<!--                                                                        -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective Settings for 'Kiran' on 'Kiran'                              -->
<!--                                                                        -->
<!-- ====================================================================== -->
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
  <localRepository>C:\Users\Kiran\.m2\repository</localRepository>
  <pluginGroups>
    <pluginGroup>org.apache.maven.plugins</pluginGroup>
    <pluginGroup>org.codehaus.mojo</pluginGroup>
  </pluginGroups>
</settings>


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.076 s
[INFO] Finished at: 2021-06-15T15:12:25+05:30
[INFO] Final Memory: 17M/207M
[INFO] ------------------------------------------------------------------------

settings.xml 包含proxies标签,可以配置多个代理配置。

以下是配置http和https代理的示例settings.xml 。其中包含协议、用户名和密码以及主机细节。

<proxies>
  <!-- Http Proxy setting configuration-->
  <proxy>
    <id>optional</id>
    <active>true</active>
    <protocol>http</protocol>
    <username>proxyuser</username>
    <password>proxypass</password>
    <host>proxy-hostname.com</host>
    <port>80</port>
    <nonProxyHosts>otherhosts.com</nonProxyHosts>
  </proxy>
  <!-- HTTPS Proxy configuration details -->
  <proxy>
    <id>optional</id>
    <active>true</active>
    <protocol>https</protocol>
    <username>username</username>
    <password>password</password>
    <host>proxy-hostname.com</host>
    <port>80</port>
    <nonProxyHosts>otherhosts.com</nonProxyHosts>
  </proxy>
</proxies>

如何告诉maven采用特定的settings.xml文件?

这可以通过mvn命令行实现

您可以通过-s 选项覆盖用户设置文件

全局设置可以通过-gs 选项覆盖。

mvn -gs c://users/adming/settings.xml

这在maven3.6.3 或最新版本中有效。