gradle的下载、解压、环境变量配置以及命令行基本用法

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

父目录 Android 开发入门 - wuyujin1997

文章目录

Intro

关于Java项目的依赖、编译流程等管理有三代的解决方案。

  1. Ant
  2. Maven
  3. Gradle

可以想像这三代工具必定是各有特定但整理功能性一定是逐渐增强的。
Gradle号称极强不过目前大多数项目还是基于Maven来做项目的【依赖管理、编译流程管理】等。
不巧我需要接触到 Gradle 所以在这里记录一下对其的使用过程。

下载

在下载页https://gradle.org/releases/ 下载一个合适的版本。
我下载的是https://downloads.gradle.org/distributions/gradle-8.2-bin.zip

解压

.zip格式的压缩包在macOS中可在 Finder/访达 中直接双击即可解压。
而我放置gradle的位置是/Users/wuyujin1997/Dev/gradle-8.2
在这里插入图片描述

配置环境变量

在上一步你已经看到gradle目录中的bin/文件夹下有两个脚本。
第一个gradle是shell脚本可执行于类unix系统(包含Linux与macOS)
而另一个gradle.bat是批处理脚本可执行于Windows系统中。

我们要做的就是最终使得这个gradle的bin/目录包含在Path环境变量中(这样以后再想执行这个目录下的命令脚本就可以不用带全路径)。

# 先进入用户目录
wuyujin1997@mac11 ~ % cd ~


# 看一下系统默认使用的是那个shellmacOS一般默认使用zshlinux一般默认使用bash(这决定我们即将要把环境变量配置到哪个配置文件中)。
wuyujin1997@mac11 ~ % echo $SHELL
/bin/zsh

# 查看用户目录下的zsh相关配置
wuyujin1997@mac11 ~ % ls -alhG ~ | grep z
-rw-r--r--     1 wuyujin1997  staff   145B  4  1  2022 .zprofile
-rw-------     1 wuyujin1997  staff    24K  7 16 07:20 .zsh_history
drwx------    16 wuyujin1997  staff   512B  7 16 07:20 .zsh_sessions
-rw-r--r--     1 wuyujin1997  staff   106B  3 19 16:33 .zshrc
drwxr-xr-x     3 wuyujin1997  staff    96B  3 23 00:05 Virtual Machines.localized

# 编辑zsh的 run command 配置文件(新增的配置内容见后)
wuyujin1997@mac11 ~ % vi ~/.zshrc 

# 查看最新的配置文件
wuyujin1997@mac11 ~ % more ~/.zshrc 

export SPRING_HOME=/Users/wuyujin1997/Desktop/Programs/spring-3.0.4
export PATH=$SPRING_HOME/bin:$PATH

# 这两行就是本次为 gradle 添加的配置
export GRADLE_HOME=/Users/wuyujin1997/Dev/gradle-8.2
export PATH=$GRADLE_HOME/bin:$PATH

# 用 source 重新加载最新的、更新过后的配置文件
wuyujin1997@mac11 ~ % source ~/.zshrc 

wuyujin1997@mac11 ~ % 

环境变量测试

如何校验我们的 gradle 环境变量是否配置成功呢

# 打印一下刚才配置好的 GRADLE_HOME
wuyujin1997@mac11 ~ % echo $GRADLE_HOME
/Users/wuyujin1997/Dev/gradle-8.2


# 测试 gradle 命令是否在 PATH 环境变量中(用 which 可以看出其完全路径在windows中没有which命令可以用where命令)
wuyujin1997@mac11 ~ % which gradle
/Users/wuyujin1997/Dev/gradle-8.2/bin/gradle

# 既然 gradle 已经正确配置到了 PATH 环境变量下那我们看一下 gradle 的版本
wuyujin1997@mac11 ~ % gradle --version

Welcome to Gradle 8.2!

Here are the highlights of this release:
 - Kotlin DSL: new reference documentation, assignment syntax by default
 - Kotlin DSL is now the default with Gradle init
 - Improved suggestions to resolve errors in console output

For more details see https://docs.gradle.org/8.2/release-notes.html


------------------------------------------------------------
Gradle 8.2
------------------------------------------------------------

Build time:   2023-06-30 18:02:30 UTC
Revision:     5f4a070a62a31a17438ac998c2b849f4f6892877

Kotlin:       1.8.20
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          11.0.1 (Oracle Corporation 11.0.1+13-LTS)
OS:           Mac OS X 10.16 x86_64

wuyujin1997@mac11 ~ % 

命令行

命令行基本用法

gradle --version
gradle help
gradle tasks

要注意的是 gradle helpgradle tasks 这样的命令需要你在一个含有gradle.build 文件的目录下执行否则只会有一些错误提示信息。
build.gradle 文件之于 gradle就好像 pom.xml 之于maven 都是用于管理项目依赖、编译流程等的核心配置文件。

wuyujin1997@mac11 ~ % gradle help
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :help

Welcome to Gradle 8.2.

Directory '/Users/wuyujin1997' does not contain a Gradle build.

To create a new build in this directory, run gradle init

For more detail on the 'init' task, see https://docs.gradle.org/8.2/userguide/build_init_plugin.html

For more detail on creating a Gradle build, see https://docs.gradle.org/8.2/userguide/tutorial_using_tasks.html

To see a list of command-line options, run gradle --help

For more detail on using Gradle, see https://docs.gradle.org/8.2/userguide/command_line_interface.html

For troubleshooting, visit https://help.gradle.org

BUILD SUCCESSFUL in 9s
1 actionable task: 1 executed


wuyujin1997@mac11 ~ % gradle tasks

FAILURE: Build failed with an exception.

* What went wrong:
Directory '/Users/wuyujin1997' does not contain a Gradle build.

A Gradle build should contain a 'settings.gradle' or 'settings.gradle.kts' file in its root directory. It may also contain a 'build.gradle' or 'build.gradle.kts' file.

To create a new Gradle build in this directory run 'gradle init'

For more information about the 'init' task, please refer to https://docs.gradle.org/8.2/userguide/build_init_plugin.html in the Gradle documentation.

For more details on creating a Gradle build, please refer to https://docs.gradle.org/8.2/userguide/tutorial_using_tasks.html in the Gradle documentation.

* Try:
> Run gradle init to create a new Gradle build in this directory.
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Get more help at https://help.gradle.org.

BUILD FAILED in 1s
wuyujin1997@mac11 ~ % 

命令行更多用法

后续我也许会整理在这里。
你也可以自己完成这一步。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6