gradle学习笔记(二)第一个简单demo

本文介绍了Gradle命令行的基础知识,包括如何执行多个任务、排除特定任务以及在遇到失败时如何选择继续构建。通过示例展示了Gradle命令行的具体用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chapter 4. Using the Gradle Command-Line
使用gradle命令行

This chapter introduces the basics of the Gradle command-line. You run a build using the gradle command, which you have already seen in action in previous chapters.
本篇文章介绍gradle的命令行基础,使用gradle命令开始一个例子,前面文章提到的任意一个。(靠,前面文章是哪里!!?难道我错过了什么?好吧,我是从安装那一步开始看的,貌似现在是文章4了,那好吧,就直接从samples中找一个吧,坑爹!)

4.1. Executing multiple tasks(执行多任务)

You can execute multiple tasks in a single build by listing each of the tasks on the command-line. For example, the command gradle compile test will execute the compile and test tasks. Gradle will execute the tasks in the order that they are listed on the command-line, and will also execute the dependencies for each task. Each task is executed once only, regardless of how it came to be included in the build: whether it was specified on the command-line, or as a dependency of another task, or both. Let’s look at an example.

大概的意思是,你可以通过在单个的build中列举出多个任务来执行多任务。例如,使用gradle开始compile和test任务将会执行编译和测试任务。gradle将会执行这些任务按照他们在命令行中的顺序,也将会执行每个任务的依赖任务(这个看起来和maven、ant差不多的样子)。每个任务会被执行一次,不管它是怎么被包含在build中的:无论是被指定到命令行上,还是作为另一个任务的依赖项,或者两者一起。让我们看一个例子。

Below four tasks are defined. Both dist and test depend on the compile task. Running gradle dist test for this build script results in the compile task being executed only once.

官方图片
下面声明了四个任务,dist和test两个任务都依赖于compile任务。用gradle dist test执行这个build(构建)脚本,结果将会是这个编译任务只执行一次!

Example 4.1. Executing multiple tasks
build.gradle


task compile << {
    println '以java的名义召唤,出来吧,来自地狱的家伙,哥斯拉!^_^`'
}

task compileTest(dependsOn: compile) << {
    println 'compiling unit tests'
}

task test(dependsOn: [compile, compileTest]) << {
    println 'running unit tests'
}

task dist(dependsOn: [compile, test]) << {
    println 'building the distribution'
}

这里需要说明一下,有些童鞋看到这里可能有点郁闷了,这个代码该怎么用,无从下手!特别是基础知识不够扎实的童鞋更会迷茫了。学习过ant或者javac命令的同学应该都知道,执行脚本的时候,需要设置各种环境变量,比如java命令编译java文件的时候,可能需要设置一下classpath,其实也就是让系统知道脚本在哪里,同样的道理,gradle应该也是这样的,那么首先在自己的硬盘上建一个文件夹,然后新建一个txt文本文档,将上方的代码复制下来粘贴进去保存,最后改一下这个txt文档的名字和后缀为build.gradle,然后打开命令提示符用cd命令切到该目录,执行gradle test dist就可以看到跟官网上说的一样的效果了。
这里写图片描述

4.2. Excluding tasks(排除任务)

You can exclude a task from being executed using the -x command-line option and providing the name of the task to exclude. Let’s try this with the sample build file above.

你可以使用-x命令操作将某个制定名字的任务排除。让我们试一下上面的那个例子。
直接命令行输入gradle dist -x test
这里写图片描述

官网上说
You can see from the output of this example, that the test task is not executed, even though it is a dependency of the dist task. You will also notice that the test task’s dependencies, such as compileTest are not executed either. Those dependencies of test that are required by another task, such as compile, are still executed.
可以看到例子的输出结果,test任务没有被执行,即便是它是dist任务的一个依赖任务。你应该也注意到了test任务的依赖任务,比如compileTest也没有被执行了。那些test的依赖任务又被另一个任务所依赖的任务,像compile,仍然被执行了。(真是绕人,简单说就是test任务被忽略了,test任务的依赖项目也会被忽略,但是如果test的某些依赖任务被另外一个没被忽略的任务所依赖,那么这些依赖任务仍然会执行。)

4.3. Continuing the build when a failure occurs(当发生错误时继续build)

By default, Gradle will abort execution and fail the build as soon as any task fails. This allows the build to complete sooner, but hides other failures that would have occurred. In order to discover as many failures as possible in a single build execution, you can use the - -continue option.

默认情况下,一旦任何一个任务发生了错误,gradle将会终止执行正在执行的任务。这种机制能够帮助我们更快的完善整个build过程(毕竟遇到一个错就抛出错误,不会往下执行浪费时间了嘛),但是它会隐藏其他的将会发生的错误(就是下面可能发生的错误)。为了在一个单一的build中尽可能的发现更多的错误,你可以使用 - -continue 操作。(其实就是说,默认情况下一发生错误就会中断执行,如果想一下子把所有错误都列举出来就加上- -continue这个鬼东西)

When executed with - -continue, Gradle will execute every task to be executed where all of the dependencies for that task completed without failure, instead of stopping as soon as the first failure is encountered. Each of the encountered failures will be reported at the end of the build.

当使用–continue执行的时候,gradle将会执行每一个任务、所有的依赖任务,没有错误的完成这个build,代替了一发生第一个错误就停止这种机制。每一个发生的错误都会在这个build的结尾报告出来。

If a task fails, any subsequent tasks that were depending on it will not be executed, as it is not safe to do so. For example, tests will not run if there is a compilation failure in the code under test; because the test task will depend on the compilation task (either directly or indirectly).

如果一个任务出错了,任何后面的任务以及它的依赖任务都不会被执行,因为这样做是不安全的。例如,代码编译的时候出错,测试则不会运行了;因为这个测试任务依赖于编译任务(直接或者间接依赖)(这些外国人就是蛋疼,非要在最后加上这样一段,跟前面一段完全没关系,还是在说,一个任务停止了就不往下执行了。)

gradle-demo.zip 文件夹 PATH 列表 卷序列号为 4E8D-6931 C:. │ .classpath │ .gitignore │ .project │ .txt │ build.gradlegradlew │ gradlew.bat │ settings.gradle │ ├─.gradle │ ├─4.8.1 │ │ ├─fileChanges │ │ │ last-build.bin │ │ │ │ │ ├─fileContent │ │ │ annotation-processors.bin │ │ │ fileContent.lock │ │ │ │ │ ├─fileHashes │ │ │ fileHashes.bin │ │ │ fileHashes.lock │ │ │ resourceHashesCache.bin │ │ │ │ │ └─taskHistory │ │ taskHistory.bin │ │ taskHistory.lock │ │ │ ├─buildOutputCleanup │ │ buildOutputCleanup.lock │ │ cache.properties │ │ outputFiles.bin │ │ │ └─vcsWorkingDirs │ gc.properties │ ├─.settings │ org.eclipse.buildship.core.prefs │ org.eclipse.core.resources.prefs │ org.eclipse.jdt.core.prefs │ ├─bin │ ├─main │ │ │ application.properties │ │ │ │ │ ├─com │ │ │ └─dream │ │ │ └─gradledemo │ │ │ │ GradleDemoApplication.class │ │ │ │ │ │ │ └─controller │ │ │ DemoController.class │ │ │ │ │ └─templates │ │ hello.html │ │ │ └─test │ └─com │ └─dream │ └─gradledemoGradleDemoApplicationTests.class │ ├─build │ ├─classes │ │ └─java │ │ └─main │ │ └─com │ │ └─dream │ │ └─gradledemo │ │ │ GradleDemoApplication.class │ │ │ │ │ └─controller │ │ DemoController.class │ │ │ ├─resources │ │ └─main │ │ │ application.properties │ │ │ │ │ ├─static │ │ └─templates │ │ hello.html │ │ │ └─tmp │ └─compileJava ├─gradle │ └─wrapper │ gradle-wrapper.jar │ gradle-wrapper.properties │ └─src ├─main │ ├─java │ │ └─com │ │ └─dream │ │ └─gradledemo │ │ │ GradleDemoApplication.java │ │ │ │ │ └─controller │ │ DemoController.java │ │ │ └─resources │ │ application.properties │ │ │ ├─static │ └─templates │ hello.html │ └─test └─java └─com └─dream └─gradledemo GradleDemoApplicationTests.java
### 关于 Gradle 的使用教程和配置笔记 #### 一、Gradle 基本概念 Gradle 是一种基于 Groovy 或 Kotlin 脚本语言的现代构建工具,支持依赖管理和多项目构建自动化。它通过声明式的 DSL(领域特定语言)简化了复杂的构建流程[^4]。 --- #### Gradle 安装与配置 1. **下载并解压** - 下载地址:可以从官方站点获取最新版本的 Gradle 发行包。 - 解压完成后,默认路径下会有一个 `bin` 文件夹,其中包含了可执行文件 `gradle` 和其他必要的脚本[^1]。 2. **设置环境变量** - 将 `bin` 目录路径添加到系统的 PATH 环境变量中。例如: ```bash export PATH=$PATH:/path/to/gradle/bin/ ``` - 验证安装成功可以运行命令: ```bash gradle --version ``` 3. **IDE 中集成 Gradle** - 对于 IntelliJ IDEA 用户: - 打开菜单栏中的 File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle。 - 如果报错 “The specified Gradle distribution does not exist”,则需手动指定本地已安装好的 Gradle 版本位置[^2]。 ```java // Example of specifying a custom Gradle path in IDE settings. String gradlePath = "/Users/exampleUser/.gradle/wrapper/dists"; System.out.println(gradlePath); ``` 4. **Eclipse 插件支持** - Eclipse 用户需要额外安装 Buildship 插件才能更好地管理 Gradle 项目。插件更新站点为: ``` http://download.eclipse.org/buildship/updates/e46/releases/2.x/ ``` --- #### 三、核心配置文件解析 1. **build.gradle** - 这是项目的主构建脚本,定义了所有的任务以及依赖关系。通常包括以下几个部分: - 应用插件 (Plugins): ```groovy plugins { id 'java' id 'org.springframework.boot' version '2.7.0' } ``` - 设置仓库源 (Repositories): ```groovy repositories { mavenCentral() jcenter() { enabled = false } // Deprecated but still usable } ``` - 添加依赖项 (Dependencies): ```groovy dependencies { implementation 'com.example:library:1.0.0' testImplementation 'junit:junit:4.13.2' } ``` 2. **settings.gradle** - 此文件用于描述当前工作空间内的模块结构及其相互之间的关联情况。如果存在多个子项目,则可以通过 include 方法引入它们: ```groovy rootProject.name = 'my-parent-project' include ':module-a', ':module-b' ``` --- #### 四、常见问题及解决方案 | 错误信息 | 可能原因 | 推荐修复方法 | |----------|-----------|---------------| | Cannot find symbol class... | 缺少某些库或者未正确定义依赖 | 检查 build.gradle 是否遗漏必要 dependency;同步 project 后重新编译 | | Could not resolve all files for configuration... | Maven Central/JCenter 不可用 | 替换为中国区镜像服务器<br>如阿里云:<br>`maven { url "https://maven.aliyun.com/repository/public" }` | --- ### 示例代码片段 以下是创建一个简单的 Java 工程所需的最小化 `build.gradle` 文件模板: ```groovy plugins { id 'java' } group = 'com.example' version = '1.0-SNAPSHOT' repositories { mavenCentral() } dependencies { testImplementation 'junit:junit:4.13.2' } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值