知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

    • 扩展

      • Theory:Units of information
      • Theory:IDE
      • Theory:IDEA
      • Theory:Build tools
      • Theory:Operating systems
      • Theory:Gradle basics
      • Theory:Basic project with Gradle
        • The key concepts of Gradle
        • Initializing a basic project managed by Gradle
        • Modifying the build file
        • The list of all the tasks
        • Conclusion
      • Theory:Building apps using Gradle
      • Theory:Dependency management
      • Theory:Formatted output
      • Theory:Libraries
      • Theory:Frameworks
      • Theory:Modules
      • Theory:Introduction to software architecture
      • Theory:Class Diagrams
      • Theory:Text blocks
      • Theory:YAML
      • Theory:XML
      • Theory:JSON
    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • 扩展
Jim
2022-06-30
目录

Theory:Basic project with Gradle

In this topic, you will learn how to create a simple Gradle project and how Gradle manages it. We assume that you have already installed Gradle on your computer. Otherwise, follow the installation instructions (opens new window). To verify that the installation has been successful, run the gradle -v command. If you get errors, try to google them, read the docs, or write us a comment describing the issue.

# The key concepts of Gradle

Let's start with an introduction to the key concepts in Gradle: projects and tasks.

  • A project might represent either something to be built (e.g. a JAR file or ZIP archive) or a thing to do (e.g. deploying the application). Every Gradle build contains one or more projects.
  • A task is a single piece of work that a build performs. This can include compiling classes, running tests, generating docs, and so on. Every project is essentially a collection of one or several tasks.

The following picture illustrates the relationships between these concepts:

img

In simple cases, a build will contain only a single project with several tasks. This will be a common situation in your learning process. Do not worry, if the concepts look a bit abstract. We will study a more specific example soon.

# Initializing a basic project managed by Gradle

Let's initialize a new project with Gradle using a terminal in your OS.

In the future, you most likely will not have to do this manually since modern IDEs can do this for you automatically.

  1. Create a new directory to store files of your project and go to it.
mkdir gradle-demo
cd gradle-demo
1
2
  1. Invoke the gradle init command to generate a simple project. Modern versions of Gradle will ask you to fill several parameters in a dialogue form. To get acquainted with the process just choose basic as the type of project and Groovy as the build script DSL.

This command will produce the following output:

> Task :init

BUILD SUCCESSFUL in 10s
2 actionable tasks: 2 executed
1
2
3
4

Gradle performed some tasks for you and now there is a simple project with the most basic structure:

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
1
2
3
4
5
6
7
8
9

Here is brief info about all the generated files:

  • The build.gradle file is a primary file that specifies the Gradle's project, including its tasks and external libraries. For now, this file doesn't contain anything useful, but in real projects it is often updated with new information.
  • The files gradle-wrapper.jar, gradle-wrapper.properties, gradlew and gradlew.bat belong to Gradle Wrapper which allows you to run Gradle without its manual installation.
  • The settings.gradle file specifies which projects to include in your build. This file is optional for a build that has only one project, but it is mandatory for a multi-project build.

Let's build our project invoking the gradle build command from the same location where build.gradle resides. It will produce an output like this:

> Task :buildEnvironment

------------------------------------------------------------
Root project
------------------------------------------------------------

...

BUILD SUCCESSFUL in 725ms
1 actionable task: 1 executed
1
2
3
4
5
6
7
8
9
10

So, the project was successfully built with one executed task.

You can also invoke build and other commands like ./gradlew build for Unix-based systems and gradlew.bat build for Windows. It will automatically download Gradle and run the specified command. Using wrappers allows developers to start working with a Gradle-based project without having to install it manually.

# Modifying the build file

Let's make our build more interesting by adding some properties and one task to the build.gradle file using Groovy DSL.

description = "A basic Gradle project"

task helloGradle {
    doLast {
        println 'Hello, Gradle!'
    }
}
1
2
3
4
5
6
7

Here, we set the description property and define a simple task that prints a 'hello' message. There is an output after executing the task with the gradle -q helloGradle command:

> Task :buildEnvironment

------------------------------------------------------------
Root project - A basic Gradle project
------------------------------------------------------------

...

> Task :helloGradle
Hello, Gradle!

BUILD SUCCESSFUL in 831ms
2 actionable tasks: 2 executed
1
2
3
4
5
6
7
8
9
10
11
12
13

This build was completed with 2 tasks executed. Our new task printed the Hello, Gradle! message. In addition, we modified the description of the project in the build. The -q argument just simplifies the command output.

You can also use Kotlin as DSL inside the build file. To allow it, you need to specify Kotlin as DSL when creating a project. In this case, the name of the file will be build.gradle.kts.

# The list of all the tasks

If you would like to see all the possible Gradle tasks to perform, just run the gradle tasks --all command. The list will include our tasks as well:

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project - A basic Gradle project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle-demo'.
...

Other tasks
-----------
helloGradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

In a real project, the list of tasks will be much larger, because, in addition to standard tasks, it will contain a lot of tasks from various plugins (like Java or Kotlin plugin).

We've considered all Gradle-related files from the generated simple project in isolation from any source code files.

# Conclusion

You've learned the key concepts of Gradle projects and studied all the files from a simple generated project in isolation from any source code files. Now it's time to combine Gradle together with your favorite programming language!

编辑 (opens new window)
#Gradle#Build tools#Tool Guide
上次更新: 2022/09/26, 16:55:15
Theory:Gradle basics
Theory:Building apps using Gradle

← Theory:Gradle basics Theory:Building apps using Gradle→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式