完整的 settings.xml 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| <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">
<localRepository>/path/to/local/repo</localRepository>
<offline>false</offline>
<pluginGroups> <pluginGroup>com.example.plugins</pluginGroup> </pluginGroups>
<proxies> <proxy> <id>example-proxy</id> <active>true</active> <protocol>http</protocol> <host>proxy.example.com</host> <port>8080</port> <username>proxy-user</username> <password>proxy-password</password> <nonProxyHosts>*.example.com|localhost</nonProxyHosts> </proxy> </proxies>
<servers> <server> <id>central</id> <username>repo-user</username> <password>repo-password</password> </server> <server> <id>thirdparty-repo</id> <username>thirdparty-user</username> <password>thirdparty-password</password> </server> </servers>
<mirrors> <mirror> <id>aliyun-mirror</id> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>central</mirrorOf> </mirror>
<mirror> <id>internal-mirror</id> <url>https://internal-repo.example.com/repo</url> <mirrorOf>thirdparty</mirrorOf> </mirror>
<mirror> <id>fallback-mirror</id> <url>https://fallback.example.com/repo</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors>
<profiles> <profile> <id>default-profile</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository>
<repository> <id>thirdparty-repo</id> <url>https://example.com/repo</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile>
<profile> <id>env-profile</id> <activation> <property> <name>env</name> <value>prod</value> </property> </activation> <properties> <build.dir>target/prod</build.dir> </properties> </profile> </profiles>
<activeProfiles> <activeProfile>default-profile</activeProfile> </activeProfiles> </settings>
|
运行 HTML
配置项说明
- **
<localRepository>**:
- 指定本地仓库的路径。默认路径是
~/.m2/repository。
- **
<offline>**:
- 是否以离线模式运行 Maven。默认为
false。
- **
<pluginGroups>**:
- **
<proxies>**:
- **
<servers>**:
- **
<mirrors>**:
- **
<profiles>**:
- 定义 Maven 的构建配置文件(Profile),可以根据条件激活不同的配置。
- 每个 Profile 可以包含
<repositories>、<pluginRepositories> 和 <properties> 等配置。
- **
<activeProfiles>**:
常见使用场景
加速依赖下载:
配置阿里云镜像仓库:
xml
复制
1 2 3 4 5
| <mirror> <id>aliyun-mirror</id> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>central</mirrorOf> </mirror>
|
运行 HTML
私有仓库认证:
配置私有仓库的用户名和密码:
xml
复制
1 2 3 4 5
| <server> <id>private-repo</id> <username>user</username> <password>password</password> </server>
|
运行 HTML
多环境配置:
根据环境变量激活不同的 Profile:
xml
复制
1 2 3 4 5 6 7 8 9 10 11 12
| <profile> <id>prod-profile</id> <activation> <property> <name>env</name> <value>prod</value> </property> </activation> <properties> <build.dir>target/prod</build.dir> </properties> </profile>
|
运行 HTML
总结
settings.xml 是 Maven 的全局配置文件,用于配置仓库、镜像、代理、认证等信息。
- 通过合理配置
settings.xml,可以优化 Maven 的构建效率,支持多环境构建,并管理私有仓库的访问权限。
- 以上配置模板可以根据实际需求进行调整和扩展。