gradle in action
1. install
a.前置条件
Gradle 可在所有主流操作系统上运行,需要JDK版本大于等于8即可运行;
Gradle 自带 Groovy 库,因此不需要安装 Groovy。Gradle 会忽略任何现有的 Groovy 安装。
备注:jdk和gradle版本兼容性

b. 用包管理器安装
- SDKMAN! 是一种用于在大多数类 Unix 系统(macOS、Linux、Cygwin、Solaris 和 FreeBSD)上管理多个软件开发工具包的并行版本的工具。
sdk install gradle
- Homebrew macOS默认包管理.
brew install gradle
- MacPorts 是另外的macOS包管理.
sudo port install gradle
c. 手动安装
1.下载最新的 Gradle 发行版
下载地址:https://gradle.org/releases/
- 解压&配置环境变量(macOS为例)
mkdir /opt/gradle
unzip -d /opt/gradle gradle-8.1-bin.zip
ls /opt/gradle/gradle-8.1
export PATH=$PATH:/opt/gradle/gradle-8.1/bin
d. 验证
gradle -v
------------------------------------------------------------
Gradle 8.1
------------------------------------------------------------
Build time: 2023-04-12 12:07:45 UTC
Revision: 40ba32cde9d6daf2b92c39376d2758909dd6b813
Kotlin: 1.8.10
Groovy: 3.0.15
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 1.8.0_333 (Oracle Corporation 25.333-b02)
OS: Mac OS X 13.3.1 x86_64
e. 资料
https://docs.gradle.org/current/userguide/installation.html
https://docs.gradle.org/current/userguide/compatibility.html#compatibility
2. java application demo
a. 创建项目文件夹
Gradle 带有一个名为 init 的内置任务,它会在一个空文件夹中初始化一个新的 Gradle 项目。init任务利用wrapper任务来创建Gradle wrpper脚本,即gradlew。
所以首先为新项目创建一个文件夹并且进入到文件夹中。
mkdir gradle-demo
cd gradle-demo
b. 执行init任务
gradle init
Starting a Gradle Daemon (subsequent builds will be faster)
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 3
Generate multiple subprojects for application? (default: no) [yes, no]
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1
Select test framework:
1: JUnit 4
2: TestNG
3: Spock
4: JUnit Jupiter
Enter selection (default: JUnit Jupiter) [1..4]
Project name (default: gradle-demo):
Source package (default: gradle.demo): com.sma.gradle.demo
Enter target version of Java (min. 7) (default: 8):
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
> Task :init
Get more help with your project: https://docs.gradle.org/8.1/samples/sample_building_java_applications.html
BUILD SUCCESSFUL in 1m 46s
2 actionable tasks: 2 executed
c. 生成的目录结构:
├── app
│ ├── build.gradle #应用的构建脚本
│ └── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── sma
│ │ │ └── gradle
│ │ │ └── demo
│ │ │ └── App.java
│ │ └── resources
│ └── test
│ ├── java
│ │ └── com
│ │ └── sma
│ │ └── gradle
│ │ └── demo
│ │ └── AppTest.java
│ └── resources
├── gradle #为gradle wrapper生成的文件夹
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew # 启动gradle wrapper的脚本
├── gradlew.bat
└── settings.gradle #项目配置文件,用来定义构建名和子项目
18 directories, 8 files
d. 运行
./gradlew run
Task :app:run
Hello World!
BUILD SUCCESSFUL in 6s
2 actionable tasks: 1 executed, 1 up-to-date
e. 构建
./gradlew build
BUILD SUCCESSFUL in 7s
7 actionable tasks: 2 executed, 5 up-to-date
注意:如果出现如下问题,可以通过gradle.properties指定
问题:
> Task :app:compileTestJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileTestJava'.
> Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home contains a valid JDK installation.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 4s
6 actionable tasks: 5 executed, 1 up-to-date
解决:
vi gradle.properties
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_333.jdk/Contents/Home
f. 资料:
https://docs.gradle.org/current/samples/sample_building_java_applications.html
534





