GithubHelp home page GithubHelp logo

gradle-example's Introduction

tutorial

教程参考官方gradle tutorial,建议使用sdkman安装gradle和配套的jdk。

本工程是使用gradle init创建的multiple subprojects。

使用方法:

./gradlew run

当然,也可以在idea里点侧边栏里相应的gradle任务,不过idea所使用的gradle不要从wrapper修改为本地的gradle。

gradle

gradle相关的文件有很多:

  • 需要排除的:
    • .gradle/:gradle的cache,需要exclude;
    • build/:子项目下编译后的产物,类似maven的target,需要exclude;
  • 不需要排除的:
    • gradle wrapper

      These scripts allow you to run a Gradle build without requiring that Gradle be installed on your system. It also helps ensure that the same version of Gradle is used for builds by different developers and between local and CI machines.

      From now on, you will never invoke Gradle directly; instead, you will use the Gradle wrapper.

    • settings.gradle存在一个项目的root下。主要是定义它的子项目;
    • build.gradle每个子项目都有一个,定义gradle task;
    • gradle.properties:可有可无,进一步定义gradle的行为。比如要不要verbose输出、是否开启缓存;

本工程的主体是父子项目,其中root项目由app、utilities、list三个子项目组成。除此之外,还有一个root项目buildSrc,用于定义一些插件,约束其他项目的构建行为

单一项目。所以settings.gradlebuild.gradle都定义在它下面。 它存在的意义是共享子项目之间的构建逻辑。详情参考buildSrc/README.md

父子项目

父子项目,子项目由root下的settings.gradle定义:

include("app", "list", "utilities")

具体参考app/README.md

task

task是gradle最基本的构建逻辑,相当于maven的plugin的一个goal。maven的plugin是goal的集合,而gradle的plugin就是task的集合。

比如列出所有的task:

./gradlew tasks

或者针对子项目app执行task任务:

./gradlew :app:tasks

或者执行app下的welcome自定义任务:

./gradlew :app:welcome

task也有参数:

./gradlew tasks --all

task可以定义在build.gradle,也可以来自plugin。所以task其实就是maven插件的命令,不过更方便的地方在于,我们可以自定义一个task,更加灵活。

这是优于maven的地方,可以定义很多强大的功能,比如check README.md是否存在。但是要辩证看待所谓“强大”,因为过于灵活会增加对于别人项目的理解成本,maven则相对更好理解一些,因为更“死板”

task其实就是为了复用build的逻辑。

tasks变量

tasks是一个预定义的变量,代表了构建中所有任务的集合。通过tasks 变量,可以访问、配置和操作构建中的各种任务。这个变量允许你对任务进行各种操作,比如配置任务的行为、依赖关系和执行顺序等。

比如注册任务:

tasks.register("hello") {
    println("Hello!")
}

通过名称和类型访问任务,并设置任务细节:

tasks.named<Test>("test") {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}

通过名称访问任务,并设置细节(环境变量、属性等):

tasks.named("test").configure {
  systemProperty 'es.insecure_network_trace_enabled', 'true'
  environment 'LANGUAGE', 'en_US'
}

gradle-example's People

Contributors

puppylpg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.