ONEKEY——安装Gradle5.6.4
# 1、前提条件
在安装 Gradle 之前,请确保您的系统满足以下条件:
提示
操作系统:Linux(适用于其他操作系统,安装步骤略有不同)
权限要求:需要有 sudo
权限
网络要求:需要网络连接,用于下载 Gradle 安装包
# 2、Gradle 安装步骤
# 2.1 下载 Gradle 安装包
可以从 Gradle 官网或者使用国内镜像源下载 Gradle。以下是国内镜像源链接:
- Gradle 5.6.4 下载链接: Huawei Cloud Gradle 5.6.4 下载链接 (opens new window)
# 2.2 解压 Gradle 安装包
将下载的 Gradle 安装包解压到指定目录:
#!/bin/bash
# 设置变量
GRADLE_FILE_PATH="/opt/modules/gradle-5.6.4-bin.zip"
GRADLE_INSTALL_DIR="/opt/modules/gradle-5.6.4"
# 解压安装包
echo "Extracting Gradle installation package..."
unzip "$GRADLE_FILE_PATH" -d "$GRADLE_INSTALL_DIR"
# 验证 Gradle 是否成功解压
if [ $? -eq 0 ]; then
echo "Gradle installed successfully."
else
echo "Gradle installation failed."
exit 1
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 2.3 配置环境变量
配置 GRADLE_HOME
和 PATH
环境变量,使系统能正确识别 Gradle:
#!/bin/bash
# 配置 GRADLE_HOME 和 PATH
echo "Configuring GRADLE_HOME and PATH..."
echo "export GRADLE_HOME=${GRADLE_INSTALL_DIR}" | sudo tee -a /etc/profile
echo "export PATH=\$GRADLE_HOME/bin:\$PATH" | sudo tee -a /etc/profile
# 使配置生效
source /etc/profile
# 验证环境变量配置
echo "GRADLE_HOME is set to: $GRADLE_HOME"
echo "PATH is set to: $PATH"
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 2.4 验证 Gradle 安装
安装完成后,可以通过以下命令检查 Gradle 是否安装成功:
gradle -v
1
如果安装成功,您将看到类似以下的输出:
------------------------------------------------------------
Gradle 5.6.4
------------------------------------------------------------
Build time: 2019-08-02 10:58:29 UTC
Revision: 72745970958c6d4d0b40bdb327e848605871cc7e
Kotlin DSL: 1.3.61
Kotlin: 1.3.61
Groovy: 2.5.5
Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM: 1.8.0_202 (Oracle Corporation 25.202-b08)
OS: Linux 5.15.153.1-microsoft-standard-wsl2 amd64
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
笔记
如果输出 Gradle 版本信息,说明 Gradle 安装成功!
# 3、配置 update-alternatives
如果您的系统中有多个 Gradle 版本,您可以使用 update-alternatives
来切换版本:
# 安装 Gradle 到 alternatives
sudo update-alternatives --install /usr/bin/gradle gradle ${GRADLE_INSTALL_DIR}/bin/gradle 2
# 配置默认 Gradle 版本
sudo update-alternatives --config gradle
1
2
3
4
5
2
3
4
5
# 3.1 切换 Gradle 版本
执行 update-alternatives
后,您会看到一个菜单,选择需要的 Gradle 版本即可。
# 4、总结
通过以上步骤,您已成功安装并配置了 Gradle。在 Linux 系统中,Gradle 的安装过程相对简单,只需要下载、解压并配置环境变量即可。如果有多个 Gradle 版本,您可以使用 update-alternatives
来进行版本管理。
注意
注意:确保环境变量正确配置,否则可能会导致 Gradle 无法正常运行。
如有任何问题或需要进一步的帮助,请随时提出!
# 一键安装脚本
以下是 Gradle 安装的一键安装脚本,可以自动化完成 Gradle 下载、安装和配置环境变量。
#!/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.
#
# Author: JaneTTR
set -ex
###########GRADLE_INIT start
echo "############## SETUP GRADLE_INIT start #############"
# Gradle version and paths
GRADLE_VERSION="5.6.4"
GRADLE_FILE_PATH="/opt/modules/gradle-${GRADLE_VERSION}-bin.zip"
GRADLE_HOME_PATH="/opt/modules/gradle-${GRADLE_VERSION}"
GRADLE_DOWNLOAD_URL="https://mirrors.huaweicloud.com/gradle/gradle-${GRADLE_VERSION}-bin.zip"
GRADLE_FILE_PATH_LOCK="/scripts/.setup_gradle.lock"
ZIP_LOCK="/scripts/.setup_gradle_zip.lock"
# Function to extract zip files
extract_zip() {
local file_path=$1
local dest_dir=$2
if [ -f "$ZIP_LOCK" ]; then
return
else
touch $ZIP_LOCK
fi
echo "Extracting file $file_path to directory $dest_dir..."
unzip "$file_path" -d "$dest_dir"
if [ $? -eq 0 ]; then
echo "File extracted successfully: $dest_dir"
else
echo "File extraction failed"
exit 1
fi
rm -f $ZIP_LOCK
}
# Function to configure GRADLE_HOME
configure_gradle_home() {
# Update or add GRADLE_HOME variable using sed
if grep -q "^export GRADLE_HOME=" /etc/profile; then
sudo sed -i "s#^export GRADLE_HOME=.*#export GRADLE_HOME=${GRADLE_HOME_PATH}#" /etc/profile
else
echo "export GRADLE_HOME=${GRADLE_HOME_PATH}" | sudo tee -a /etc/profile
fi
# Update PATH variable to include GRADLE_HOME/bin
if ! grep -q "^export PATH=.*\$GRADLE_HOME/bin" /etc/profile; then
echo "export PATH=\$PATH:\$GRADLE_HOME/bin" | sudo tee -a /etc/profile
fi
# Reload /etc/profile to apply changes
source /etc/profile
# Verify GRADLE_HOME setting
echo "GRADLE_HOME is set to: $GRADLE_HOME"
}
# Function to check and download Gradle file
check_and_download_gradle() {
if [ -f "$GRADLE_FILE_PATH" ]; then
echo "Gradle file exists: $GRADLE_FILE_PATH"
elif [ -f "$GRADLE_FILE_PATH_LOCK" ]; then
echo "Other instance downloading..."
else
touch $GRADLE_FILE_PATH_LOCK
echo "Gradle file does not exist, downloading..."
mkdir -p "$(dirname "$GRADLE_FILE_PATH")"
curl -o "$GRADLE_FILE_PATH" "$GRADLE_DOWNLOAD_URL"
if [ $? -eq 0 ]; then
echo "Gradle download success: $GRADLE_FILE_PATH"
extract_zip "$GRADLE_FILE_PATH" "/opt/modules"
else
echo "Gradle download failed!!"
rm -f $GRADLE_FILE_PATH_LOCK
exit 1
fi
rm -f $GRADLE_FILE_PATH_LOCK
fi
while [ -f "$GRADLE_FILE_PATH_LOCK" ]; do
echo "Waiting for the lock to be released..."
sleep 1
done
echo "Lock released. Continuing..."
}
main() {
check_and_download_gradle
if [ -d "$GRADLE_HOME_PATH" ]; then
echo "Gradle home exists: $GRADLE_HOME_PATH"
else
extract_zip "$GR
ADLE_FILE_PATH" "/opt/modules"
fi
configure_gradle_home
}
main
###########GRADLE_INIT end
echo "############## SETUP GRADLE_INIT 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
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
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
# 使用说明:
- 将脚本保存为
install_gradle.sh
。 - 给脚本添加执行权限:
chmod +x install_gradle.sh
。 - 运行脚本进行 Gradle 安装:
./install_gradle.sh
。
# 脚本功能:
- 自动下载 Gradle 安装包:如果本地没有 Gradle 文件,会自动下载并解压。
- 配置 GRADLE_HOME 和 PATH:配置
GRADLE_HOME
环境变量并更新PATH
。 - 一键安装:通过执行一条命令即可完成 Gradle 安装和配置,简化安装过程。