Gradle 初体验-ApiHug准备-工具篇-002

    🤗 ApiHug × {Postman|Swagger|Api...} = 快↑ 准√ 省↓

  1. GitHub - apihug/apihug.com: All abou the Apihug   
  2. apihug.com: 有爱,有温度,有质量,有信任
  3. ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace

ApiHug 整个工具链基于 Gradle, 使用 ApiHug 准备工作最先需要学习的就是 gradle. 工欲善其事,必先利其器

初始化脚本

>gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 1

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]

Project name (default: 001_gradle_example):


> Task :init
Get more help with your project: Learn more about Gradle by exploring our samples at https://docs.gradle.org/7.4.2/samples

BUILD SUCCESSFUL in 1m 26s
2 actionable tasks: 2 executed

目录结构如下:

\---001_gradle_example
    |   .gitattributes
    |   .gitignore
    |   build.gradle // 重要, 构建脚本文件,主要的构建配置都在这里写
    |   gradlew // gradle wrapper 执行脚本文件,用来在没有安装 gradle 的情况下执行 gradle 命令。当然,第一次执行时会下载 gradle。
    |   gradlew.bat // gradle wrapper 执行脚本文件的 windows 版
    |   settings.gradle // 项目配置,指明根项目名字和引入的 module
    |
    +---.gradle
    \---gradle // 重要, 描述了 gradlew 使用的 gradle 版本
        \---wrapper
                gradle-wrapper.jar
                gradle-wrapper.properties

#Hello world

修改 build.gradle 添加如下:

task hello {
    doLast {
        println 'Hello world!'
    }
}

运行: gradle -q hello --info, 带不带 -q 或者


 Task :hello
Caching disabled for task ':hello' because:
  Build cache is disabled
Task ':hello' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
Hello world!

检查下, 默认gradle 带的 gradle tasks :


------------------------------------------------------------
Tasks runnable from root project '001_gradle_example'
------------------------------------------------------------

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 '001_gradle_example'.
dependencies - Displays all dependencies declared in root project '001_gradle_example'.
dependencyInsight - Displays the insight into a specific dependency in root project '001_gradle_example'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
outgoingVariants - Displays the outgoing variants of root project '001_gradle_example'.
projects - Displays the sub-projects of root project '001_gradle_example'.
properties - Displays the properties of root project '001_gradle_example'.
tasks - Displays the tasks runnable from root project '001_gradle_example'.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL in 1s

#来点面向对象


task myTask {
    ext.myProperty = "dearxue is better"
}

task printTaskProperties  {
    println myTask.myProperty
}

然后执行 gradle printTaskProperties :

gradle printTaskProperties

> Configure project :
dearxue is better

BUILD SUCCESSFUL in 974ms

#DS

到此我们可以理解的 Gradle 尚未和我们组织和编译我们的java工程没有任何的关系, 可以看到Gradle 就是在grovvy/koltin 的一个封装, 带了很多上下文也就是有自己一套DSL规范。

上下文: DSL 基本概念open in new window 参考,Gradle Basicopen in new window 在基础技术上又提升的一层, 大部分情况我们需要了了解DSL 本身, 或者整个Build 全流程, 我们只需要知道 java/spring 的 gradle 插件,但是了解 DSL + 基本理论基础非常有帮助理解为什么那么玩。

#参考

  1. Sampleopen in new window
  2. Guideopen in new window
  3. DSL
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值