Skip to content

Maven 配置依赖包安装路径

独立于 Eclipse 安装 Maven 工具时

修改 Maven 全局配置文件以更改依赖包安装地址

全局配置文件为 <Maven_安装目录>/conf/setting.xml(安装目录为 bin 目录所在的目录)。

xml
<!-- <maven_install_direcotry>/conf/setting.xml-->
<?xml version="1.0" encoding="UTF-8"?>

<!-- ... -->

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  -->
  <!-- 修改此行即更改依赖包安装路径(此行初始状态时处于上文注释中) -->
  <localRepository>E:\SOFT\programming\package-manage\maven\apache-maven-3.9.6\repository</localRepository>

<!-- ... -->

</settings>

命令行查看配置文件信息

使用 mvn help:effective-settings 命令打印 Maven 有效设置信息。这个命令会考虑所有的配置文件(全局 settings.xml 文件、用户 settings.xml 文件、项目 pom.xml 文件)。

shell
PS maven> mvn help:effective-settings
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- help:3.4.0:effective-settings (default-cli) @ standalone-pom ---
[INFO]
Effective user-specific configuration settings:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin                                         -->
<!-- See: https://maven.apache.org/plugins/maven-help-plugin/               -->
<!--                                                                        -->
<!-- ====================================================================== -->
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
  <localRepository>E:\SOFT\programming\package-manage\maven\apache-maven-3.9.6\repository</localRepository>
  <mirrors>
    <mirror>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
      <id>maven-default-http-blocker</id>
    </mirror>
  </mirrors>
  <pluginGroups>
    <pluginGroup>org.apache.maven.plugins</pluginGroup>
    <pluginGroup>org.codehaus.mojo</pluginGroup>
  </pluginGroups>
</settings>


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.645 s
[INFO] Finished at: 2024-02-20T00:28:45+09:00
[INFO] ------------------------------------------------------------------------

使用 Eclipse 内置 Maven 工具时

菜单栏 > Window > Preferences 打开偏好设置窗口。从窗口侧边栏中找到 Maven > User Settings。修改其中的 Global Settings 或是 User Settings 为自定义的 settings.xml 文件(配置内容可参考 <Maven_安装目录>/conf/setting.xml 文件)。点击 Update Settings 后即会自动更新 Local Repository(不可手动修改)。点击 Apply 后即应用。