Gradle custom task which runs multiple tasks

前端 未结 8 900
不知归路
不知归路 2020-12-07 17:16

I wanna run multiple gradle tasks as one. So instead of

./gradlew clean build publish

I want to have a custom task

./gradl         


        
相关标签:
8条回答
  • 2020-12-07 17:44

    Try adding defaultTasks in build.gradle. For eg. defaultTasks 'clean', 'build', 'publish'

    0 讨论(0)
  • 2020-12-07 17:45

    You can also use the task base class called GradleBuild

    Here how you can do that with GradleBuild

    task cleanBuildPublish(type: GradleBuild) {
         tasks = ['clean', 'build', 'publish']
    }
    
    0 讨论(0)
  • 2020-12-07 17:49

    If you need to execute some tasks in predefined order, then you need to not only set dependsOn, but also to set mustRunAfter property for this tasks, like in the following code:

    task cleanBuildPublish {
        dependsOn 'clean'
        dependsOn 'build'
        dependsOn 'publish'
        tasks.findByName('build').mustRunAfter 'clean'
        tasks.findByName('publish').mustRunAfter 'build'
    }
    

    dependsOn doesn't define an order of tasks execution, it just make one task dependent from another, while mustRunAfter does.

    0 讨论(0)
  • 2020-12-07 17:58

    If publish task is in a sub project named subProjectName,

    ...
    tasks.findByPath(':subProjectName:publish').mustRunAfter 'build'
    ...
    
    0 讨论(0)
  • 2020-12-07 17:58

    Here is how I did it, with Kotlin scripting, using both dependsOn and mustRunAfter. Here is an example of running two tasks, one (custom registered "importUnicodeFiles" task) that is in "this" project and one (predefined "run" task) that is in a sibling project named ":unicode":

    tasks.register("rebuildUnicodeFiles") {
        description = "Force the rebuild of the `./src/main/resources/text` data"
        val make = project(":unicode").tasks["run"]
        val copy = tasks["importUnicodeFiles"]
        dependsOn(make)
        dependsOn(copy)
        copy.mustRunAfter(make)
    }
    

    The Gradle developers generally advise against this approach (they say that forcing ordering is bad, and that executing tasks from other projects is bad), and are working on a way to publish results between projects; see: https://docs.gradle.org/current/userguide/cross_project_publications.html

    0 讨论(0)
  • 2020-12-07 18:00

    My approach is

    task cleanBuildPublish (type: GradleBuild, dependsOn: ['clean', 'build', 'publish']) { 
    }
    

    This works for me.

    0 讨论(0)
提交回复
热议问题