【问题标题】:How to invoke Annotation Processor from Gradle plugin如何从 Gradle 插件调用注释处理器
【发布时间】:2020-03-27 12:47:15
【问题描述】:

我目前正在开发一个 Gradle custon 插件,该插件应该分析我的根项目以获取每个子项目中的特定配置,然后在构建目录中生成一些 kotlin 源代码。我想不出一种方法来从我的 gradle 插件中调用我的注释处理器,该插件有一个自定义任务。

任何想法如何实现这一目标?任何资源/教程/文档也非常受欢迎。

提前致谢,注意安全。

【问题讨论】:

    标签: gradle-plugin annotation-processing


    【解决方案1】:

    经过长时间的谷歌搜索和大部分尝试和失败后,我终于找到了解决问题的方法。这是我的任务配置。

    基本上我们必须提供注解处理器的类路径作为项目配置。就我而言,我将此块添加到项目的 build.gradle

    allprojects {
        configurations {
            myProcessor //pick any name!!!
        }
    }
    

    然后作为 app build.gradle 中的依赖项

    dependencies {
        myProcessor "PATH_TO_MY_PROCESSOR_JAR" //or maven dependency if it's uploaded to maven central
    }
    
    tasks.register(
    "myTaskName",
    JavaCompile::class.java
    ) {
        compiler ->
        with(compiler.options) {
            isFork = true
            isIncremental = true
        }
        with(compiler) {
            group = shuttle.plugin.ShuttlePlugin.TASK_GROUP
            destinationDir = outputDir
            classpath = variant.getCompileClasspath(null)
            options.annotationProcessorPath = configurations.getByName("myProcessor") //this is the missing piece!!
            source = files(projectDir.resolve("src/main/java")).asFileTree
        }
    }
    

    但是,这个任务只会编译 Java 类Only 而不是kotlin。 知道我的插件仅针对 android 应用程序,因此我无法直接访问kotlinCompile gradle 默认任务,知道如何解决此问题吗?

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2018-08-07
      • 2021-02-03
      相关资源
      最近更新 更多