【问题标题】:How can I specify a category for a Gradle task?如何为 Gradle 任务指定类别?
【发布时间】:2015-07-13 06:11:06
【问题描述】:

我正在 Intellij IDEA 中编写 Gradle 任务。我注意到在 Gradle 窗口中,任务出现在如下文件夹下:

我想知道,你如何给一个任务一个“类别”,以便它出现在屏幕截图所示的文件夹中?

我创建的所有任务通常都以other 结尾。我还在编写一个自定义插件,并希望它出现在我选择的“文件夹”名称下。但我认为在编写任务时这将是相同的答案。

【问题讨论】:

    标签: gradle intellij-idea groovy gradle-kotlin-dsl gradle-groovy-dsl


    【解决方案1】:

    您只需要设置任务的group 属性。例如(来自http://mrhaki.blogspot.co.uk/2012/06/gradle-goodness-adding-tasks-to.html

    task publish(type: Copy) {
        from "sources"
        into "output"
    }
    
    configure(publish) {   
        group = 'Publishing'
        description = 'Publish source code to output directory'
    }
    

    【讨论】:

    • 谢谢。根据您的回答,我可以为自定义插件做到这一点,我在 BlahPlugin.groovy 的签名中添加了组:target.task('greetingTask', type: GreetingTask, group:'platitudes')
    【解决方案2】:

    或者,更短的语法:

    task publish(type: Copy) {
      group = "Publishing"
      description = "Publish source code to output directory"
      from "sources"
      into "output"
    }
    

    【讨论】:

      【解决方案3】:

      另外,下一个是分组任务和避免样板代码的好方法:

      class PublishCopy extends Copy {
          PenguinTask() {
              group = 'publish copy'
          }
      }
      

      然后你不必每次都指定任务组:

      task copySources(type: PublishCopy) {
        from "sources"
        into "output"
      }
      
      task copyResources(type: PublishCopy) {
        from "res"
        into "output/res"
      }
      

      【讨论】:

        【解决方案4】:

        如果你有很多任务,你可以配置组如下:

        def groupName = "group-name"
        task1.group = groupName
        task2.group = groupName
        task3.group = groupName
        

        【讨论】:

          【解决方案5】:

          这适用于 Kotlin DSL(build.gradle.kts 脚本):

          tasks.create("incrementVersion") {
              group = "versioning"
              // ...
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-05-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-31
            • 2015-12-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多