Ivy配置私有镜像仓库
# 1、问题剖析
问题内容: 在执行编译或构建时,常常会遇到下载速度过慢的问题,特别是在从公共 Maven 仓库下载依赖时。慢速下载不仅增加了构建时间,还可能导致构建失败。
产生原因: 这一问题通常是由以下原因导致:
- 网络问题:网络状况不佳或者远程仓库的响应速度慢,特别是在国内访问国外公共仓库时。
- 频繁的依赖下载:每次构建时都从网络上下载相同的依赖,这种重复的过程显著增加了构建时间。
解决办法: 使用私有仓库或国内镜像源,这样不仅可以提高下载速度,还可以更好地控制和管理依赖项。
# 2、解决办法
# 2.1 修改 ivySettings.xml
配置文件
# 配置步骤
以编译 Solr 为例,修改 ivySettings.xml
文件配置私有镜像仓库。通过配置文件来指定私有仓库的地址,并在构建时使用该仓库。
# 示例编译命令:
# 编译命令:
ant -Dversion=8.11.2 \
-Dhadoop.version=3.3.4 \
-Dexclude.from.war=nothing \
-Divy.home=/root/.ivy2 \
-Drepo.maven.org=http://172.20.0.2:8081/repository/maven-public/ \
-Drepo.user=admin \
-Drepo.pass=admin123 \
-Divy_bootstrap_url1=http://172.20.0.2:8081/repository/maven-public/ \
-Divy_install_path=/root/.ant/lib \
-lib /root/.ant/lib \
-Dreactor.repo=file:///root/.m2/repository \
-verbose \
create-package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
命令中的关键部分:
-Drepo.maven.org=http://172.20.0.2:8081/repository/maven-public/ \
-Drepo.user=admin \
-Drepo.pass=admin123 \
1
2
3
4
5
2
3
4
5
这三项配置决定了使用私有 Maven 仓库时的地址、用户名和密码。你需要根据自己的私有仓库的实际情况进行修改。
# 仓库截图
以下截图展示了私有镜像仓库的配置效果:
# 完整的 Solr Ivy 配置文件内容:
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<ivysettings>
<settings defaultResolver="default"/>
<property name="local-maven2-dir" value="${user.home}/.m2/repository/"/>
<include url="${ivy.default.settings.dir}/ivysettings-public.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-main-chain.xml"/>
<caches lockStrategy="${ivy.lock-strategy}" resolutionCacheDir="${ivy.resolution-cache.dir}"/>
<credentials host="${repo.maven.org}" username="${repo.user}" passwd="${repo.pass}"/>
<property name="repo.maven.org" value="${repo.maven.org}"
override="true"/>
<resolvers>
<ibiblio name="nexus" root="${repo.maven.org}" m2compatible="true"/>
<filesystem name="local-maven-2" m2compatible="true" local="true">
<artifact
pattern="${local-maven2-dir}/[organisation]/[module]/[revision]/[module]-[revision].[ext]"/>
<ivy
pattern="${local-maven2-dir}/[organisation]/[module]/[revision]/[module]-[revision].pom"/>
</filesystem>
<chain name="default" returnFirst="true" checkmodified="true" changingPattern=".*SNAPSHOT">
<resolver ref="local"/>
<resolver ref="nexus"/>
</chain>
</resolvers>
</ivysettings>
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
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
# 2.2 配置镜像仓库的优势
# 为什么使用私有镜像仓库?
- 加速下载速度:私有仓库通常部署在公司或本地网络中,避免了远程公共仓库的网络延迟问题。
- 控制依赖版本:通过私有仓库,可以对依赖包的版本进行严格管理,确保每次构建时使用的是稳定的依赖版本。
- 提高稳定性:使用私有仓库可以避免依赖包被公共仓库删除或不可用的情况。
# 如何确保私有仓库的可用性?
- 定期同步公共仓库:确保私有仓库与公共仓库的同步,避免依赖包版本过时。
- 访问控制:为私有仓库配置适当的权限控制,确保只有授权用户才能访问。
# 2.3 懒人脚本-安装
如果你不想手动配置,可以使用以下懒人脚本一键安装并配置 Ivy 和 Ant 环境。
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License
set -e
echo "############## SETUP ANT_IVY start #############"
# 定义变量
ANT_URL="https://mirrors.huaweicloud.com/apache//ant/binaries/apache-ant-1.10.12-bin.tar.gz"
IVY_URL="https://mirrors.huaweicloud.com/apache//ant/ivy/2.5.0/apache-ivy-2.5.0-bin.tar.gz"
ANT_TAR="/opt/modules/apache-ant-1.10.12-bin.tar.gz"
IVY_TAR="/opt/modules/apache-ivy-2.5.0-bin.tar.gz"
ANT_DIR="/opt/modules/apache-ant-1.10.12"
IVY_DIR="/opt/modules/apache-ivy-2.5.0"
# 创建安装目录
sudo mkdir -p /opt/modules
# 下载文件的函数
download_if_not_exists() {
local url=$1
local file=$2
if [ ! -f "$file" ]; then
echo "正在下载 $file..."
wget -q "$url" -O "$file"
else
echo "$file 已存在,跳过下载。"
fi
}
# 解压文件的函数
extract_if_not_exists() {
local file=$1
local dir=$2
if [ ! -d "$
dir" ]; then
echo "正在解压 $file..."
sudo tar -xzf "$file" -C /opt/modules
else
echo "$dir 已存在,跳过解压。"
fi
}
# 配置环境变量的函数
configure_env_variable() {
local var_name=$1
local var_value=$2
local profile_file="/etc/profile"
if ! grep -q "export $var_name=" "$profile_file"; then
echo "正在配置 $var_name 到 $profile_file..."
echo "export $var_name=$var_value" | sudo tee -a "$profile_file"
echo 'export PATH=$PATH:$'"$var_name"'/bin' | sudo tee -a "$profile_file"
else
echo "$var_name 已配置在 $profile_file 中。"
fi
}
# 下载 Ant 和 Ivy
download_if_not_exists "$ANT_URL" "$ANT_TAR"
download_if_not_exists "$IVY_URL" "$IVY_TAR"
# 解压 Ant 和 Ivy
extract_if_not_exists "$ANT_TAR" "$ANT_DIR"
extract_if_not_exists "$IVY_TAR" "$IVY_DIR"
# 配置环境变量
configure_env_variable "ANT_HOME" "$ANT_DIR"
configure_env_variable "IVY_HOME" "$IVY_DIR"
# 重新加载 /etc/profile
source /etc/profile
echo "############## SETUP ANT_IVY end #############"
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
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