【问题标题】:Can a standalone gradle plugin export a custom task type?独立的 gradle 插件可以导出自定义任务类型吗?
【发布时间】:2021-05-27 13:05:45
【问题描述】:

我有一个包含自定义任务类型的 standalone Gradle plugin

gradle-conventions/build.gradle

plugins {
  id 'groovy-gradle-plugin'
  id 'maven-publish'
}

group = 'com.example'
version = '1.0'

publishing {
  repositories {
    maven {
      url = uri('/path/to/repo')
    }
  }
}

gradle-conventions/src/main/groovy/com.example.my-conventions.gradle

abstract class CustomTask extends DefaultTask {
    @TaskAction
    def hello() {
        println "hello"
    }
}

我可以使用另一个项目的插件,但我如何注册CustomTask?像这样的:

project/build.gradle

plugins {                                                                       
  id 'com.example.my-conventions' version '1.0'
}

// how do I reference CustomTask here?
tasks.register('myCustomTask', com.example.CustomTask) {
  // ...
}

是否可以从自定义插件导出自定义任务?还是我必须consume custom tasks using the buildscript mechanism

【问题讨论】:

    标签: gradle gradle-task gradle-custom-plugin


    【解决方案1】:

    查看gradle-conventions-1.0.jar,好像自定义任务类属于默认包,所以可以如下注册任务:

    project/build.gradle

    plugins {                                                                       
      id 'com.example.my-conventions' version '1.0'
    }
    
    tasks.register('myCustomTask', CustomTask) {
      // ...
    }
    

    但这仅适用于com.example.my-conventions.gradle 包含除了类本身之外的常规代码,否则我会收到错误:

    An exception occurred applying plugin request [id: 'com.example.my-conventions', version: '1.0']
    > Failed to apply plugin 'com.example.my-conventions'.
       > java.lang.ClassNotFoundException: precompiled_ComExampleMyConventions
    

    这种方法避免了依赖buildscript mechanism(Gradle 文档中不建议这样做)。

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2019-08-15
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多