Maven--setting.xml详解

Maven配置详解
本文介绍了Maven中的settings.xml配置文件的详细内容,包括全局配置、本地仓库设置、服务器认证、镜像配置、代理配置及配置文件中的概要信息。同时探讨了如何通过配置文件管理依赖仓库、插件版本及环境变量。

基本上是对官网的翻译和简化 http://maven.apache.org/settings.html

Introduction

  • settings.xml是maven的全局配置文件
  • pom.xml文件是所在项目的局部配置
  • settings.xml文件位置
    1. global settings : ${maven.home}/conf/settings.xml
    2. user settings : ~/.m2/settings.xml //~表示${user.home} 下同
  • 优先级: pom.xml > user settings > global settings

Settings Details

top elements & simple value

<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">
      <!-- 本地仓库。默认值:~/.m2/repository -->
      <localRepository/> 
      <!-- 与用户交互以获得输入。默认false -->
      <interactiveMode/>
      <!-- 使用~/.m2/plugin-registry.xml来管理插件版本。默认false -->
      <usePluginRegistry/>
      <!-- 离线模式。默认false。理解不了 -->
      <offline/>
      <!-- -->
      <pluginGroups/>
      <!-- -->
      <servers/>
      <!-- -->
      <mirrors/>
      <!-- -->
      <proxies/>
      <!-- -->
      <profiles/>
      <!-- -->
      <activeProfiles/>
    </settings>

Plugin Groups

<!-- 给命令行下运行的插件,提供默认的groudId。
默认已经包含:org.apache.maven.plugins,org.codehaus.mojo -->
<pluginGroups>
    <!-- mvn jetty:run 等效于 org.mortbay.jetty:jetty-maven-plugin:run-->
    <pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>

servers

<!-- The repositories for download and deployment are defined by the repositories and distributionManagement elements of the POM. -->
<servers>
    <server>
    <!-- This is the ID of the server (not of the user to login as) .
    it matches the id element of the repository/mirror -->
      <id>server001</id>
      <username>my_login</username>
      <password>my_password</password>
      <privateKey>${user.home}/.ssh/id_dsa</privateKey>
      <passphrase>some_passphrase</passphrase>
      <filePermissions>664</filePermissions>
      <directoryPermissions>775</directoryPermissions>
      <configuration></configuration>
    </server>
</servers>

Mirrors

  <mirrors>
  <!--仓库的下载镜像 -->
    <mirror>
      <!-- 镜像的id -->
      <id>planetmirror.com</id>
      <!-- 镜像的名称 -->
      <name>PlanetMirror Australia</name>
      <!-- 镜像的url -->
      <url>http://downloads.planetmirror.com/pub/maven2</url>
      <!-- 被镜像仓库的id -->
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>

Proxies

  <proxies>
  <!-- 解决网路问题?可以翻墙? -->
    <proxy>
      <id>myproxy</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.somewhere.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
    </proxy>
  </proxies>

Profiles

  <profiles>
    <profile>
      <id>test</id>
      <activation>
      <!-- 也可以在命令行中指定:-P test -->
        <activeByDefault>false</activeByDefault>
        <jdk>1.5</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version>
        </os>
        <property>
          <name>mavenVersion</name>
          <value>2.0.3</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
      ...
    </profile>
  </profiles>

Properties

  1. env.X: the shell’s environment variable. ${env.PATH}
  2. project.x:pom.xml中对应元素值。${project.version}.
  3. settings.x: settings.xml中对应元素的值。${settings.offline}
  4. Java System Properties: java.lang.System.getProperties的值。${java.home}
  5. x: Set within a properties element or an external files, the value may be used as ${someVar}.
      <properties>
          <!-- ${user.install} is accessible from a POM if this profile is active. -->
        <user.install>${user.home}/our-project</user.install>
      </properties>

Profiles.repositories & pluginRepositories

  <profiles>
    <profile>
      ...
      <repositories>
        <repository>
          <id>codehausSnapshots</id>
          <name>Codehaus Snapshots</name>
          <releases>
            <enabled>false</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>fail</checksumPolicy>
          </snapshots>
          <url>http://snapshots.maven.codehaus.org/maven2</url>
          <layout>default</layout>
        </repository>
      </repositories>
      <pluginRepositories>
        ...
      </pluginRepositories>
      ...
    </profile>
  </profiles>

Active Profiles

  <activeProfiles>
    <activeProfile>env-test</activeProfile>
  </activeProfiles>


<think> 我们需要使用指定的settings.xml文件和本地仓库路径来执行mvn install:install-file命令,将D:\Tools\icbc\lib下的jar文件安装到D:\Tools\yuanshuRepository仓库中。 命令格式如下: mvn install:install-file -s <settings_file> -Dmaven.repo.local=<local_repo_path> ...其他参数... 具体步骤: 1. 使用-s参数指定settings.xml文件:C:\Users\hongy\.m2\settingsyuanshu.xml 2. 使用-Dmaven.repo.local参数指定本地仓库路径:D:\Tools\yuanshuRepository 3. 为每个jar文件执行安装命令,指定groupId、artifactId、version和packaging。 注意:由于我们使用自定义的settings.xml和本地仓库位置,需要确保这些路径的正确性。 以下是安装各个jar文件的命令(在Windows PowerShell中执行): </think> ### 解决方案:使用指定 settings.xml 和本地仓库路径安装 JAR 文件 要使用自定义的 settings.xml (`C:\Users\hongy\.m2\settingsyuanshu.xml`) 和自定义本地仓库路径 (`D:\Tools\yuanshuRepository`) 安装 JAR 文件,需要组合使用 `-s` 和 `-Dmaven.repo.local` 参数。以下是完整的操作流程: #### 1. 安装单个 JAR 文件的通用命令模板 ```powershell mvn -s "C:\Users\hongy\.m2\settingsyuanshu.xml" ` install:install-file ` -Dmaven.repo.local=D:\Tools\yuanshuRepository ` -Dfile=<JAR文件路径> ` -DgroupId=<组ID> ` -DartifactId=<项目ID> ` -Dversion=<版本号> ` -Dpackaging=jar ``` #### 2. 具体安装脚本(Windows PowerShell) ```powershell # hsm-software-share-1.0.5.jar mvn -s "C:\Users\hongy\.m2\settingsyuanshu.xml" ` install:install-file ` -Dmaven.repo.local=D:\Tools\yuanshuRepository ` -Dfile=D:\Tools\icbc\lib\hsm-software-share-1.0.5.jar ` -DgroupId=com.icbc ` -DartifactId=hsm-software-share ` -Dversion=1.0.5 ` -Dpackaging=jar # icbc-api-sdk-cop.jar mvn -s "C:\Users\hongy\.m2\settingsyuanshu.xml" ` install:install-file ` -Dmaven.repo.local=D:\Tools\yuanshuRepository ` -Dfile=D:\Tools\icbc\lib\icbc-api-sdk-cop.jar ` -DgroupId=com.icbc ` -DartifactId=icbc-api-sdk-cop ` -Dversion=1.0.0 ` -Dpackaging=jar # icbc-api-sdk-cop-io.jar mvn -s "C:\Users\hongy\.m2\settingsyuanshu.xml" ` install:install-file ` -Dmaven.repo.local=D:\Tools\yuanshuRepository ` -Dfile=D:\Tools\icbc\lib\icbc-api-sdk-cop-io.jar ` -DgroupId=com.icbc ` -DartifactId=icbc-api-sdk-cop-io ` -Dversion=1.0.0 ` -Dpackaging=jar # icbc-ca.jar mvn -s "C:\Users\hongy\.m2\settingsyuanshu.xml" ` install:install-file ` -Dmaven.repo.local=D:\Tools\yuanshuRepository ` -Dfile=D:\Tools\icbc\lib\icbc-ca.jar ` -DgroupId=com.icbc ` -DartifactId=icbc-ca ` -Dversion=1.0.0 ` -Dpackaging=jar ``` #### 3. 关键参数详解- `-s "C:\Users\hongy\.m2\settingsyuanshu.xml"` 指定自定义 Maven 配置文件(替代默认的 settings.xml- `-Dmaven.repo.local=D:\Tools\yuanshuRepository` 设置自定义本地仓库路径(替代默认的 `~/.m2/repository`) - 其他参数与标准安装命令相同(file、groupId、artifactId、version) #### 4. 验证安装结果: 检查自定义仓库目录结构(以 hsm-software-share 为例): ``` D:\Tools\yuanshuRepository └── com └── icbc └── hsm-software-share └── 1.0.5 ├── hsm-software-share-1.0.5.jar ├── hsm-software-share-1.0.5.pom └── _remote.repositories ``` #### 5. 项目引用配置示例(pom.xml): ```xml <repositories> <!-- 指向自定义本地仓库 --> <repository> <id>local-yuanshu</id> <url>file:///D:/Tools/yuanshuRepository</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.icbc</groupId> <artifactId>hsm-software-share</artifactId> <version>1.0.5</version> </dependency> <!-- 其他依赖同理 --> </dependencies> ``` ### 注意事项: 1. **路径格式**: - Windows 文件路径使用反斜杠 `\` 或正斜杠 `/` - Maven 的 `file:///` URL 必须使用正斜杠 `/`(如 `file:///D:/Tools/...`) 2. **权限问题**: - 确保运行命令的用户对 `D:\Tools\yuanshuRepository` 有写入权限 - 使用管理员权限运行 PowerShell(可选) 3. **配置文件有效性**: - 验证 `settingsyuanshu.xml` 是否包含有效的 Maven 配置 - 检查是否配置了代理(如访问外部仓库需要) 4. **批量安装优化**: 可编写批处理脚本(.bat)自动遍历 lib 目录: ```bat @echo off set SETTINGS="C:\Users\hongy\.m2\settingsyuanshu.xml" set REPO="D:\Tools\yuanshuRepository" set LIB_DIR="D:\Tools\icbc\lib" for %%f in (%LIB_DIR%\*.jar) do ( mvn -s %SETTINGS% install:install-file ^ -Dmaven.repo.local=%REPO% ^ -Dfile=%%f ^ -DgroupId=com.icbc ^ -DartifactId=%%~nf ^ -Dversion=1.0.0 ^ -Dpackaging=jar ) ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值