如何从 Gradle 插件调用注释处理器
How to invoke Annotation Processor from Gradle plugin
我目前正在开发一个 Gradle 自定义插件,它应该分析我的根项目以获取每个子项目中的特定配置,然后在构建目录中生成一些 kotlin 源代码。我想不出一种方法来从我的 gradle 插件调用我的注释处理器,该插件具有针对此事的自定义任务。
有什么实现方法吗?任何 resource/tutorial/documentation 也非常受欢迎。
提前致谢,注意安全。
经过长时间的谷歌搜索,大部分时间都在尝试但都失败了,我终于找到了问题的解决方案。这是我的任务配置。
基本上我们必须提供注释处理器的类路径作为项目配置。在我的例子中,我将这个块添加到项目的 build.gradle
allprojects {
configurations {
myProcessor //pick any name!!!
}
}
然后作为应用程序的依赖项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 默认任务,知道如何解决此问题吗?
我目前正在开发一个 Gradle 自定义插件,它应该分析我的根项目以获取每个子项目中的特定配置,然后在构建目录中生成一些 kotlin 源代码。我想不出一种方法来从我的 gradle 插件调用我的注释处理器,该插件具有针对此事的自定义任务。
有什么实现方法吗?任何 resource/tutorial/documentation 也非常受欢迎。
提前致谢,注意安全。
经过长时间的谷歌搜索,大部分时间都在尝试但都失败了,我终于找到了问题的解决方案。这是我的任务配置。
基本上我们必须提供注释处理器的类路径作为项目配置。在我的例子中,我将这个块添加到项目的 build.gradle
allprojects {
configurations {
myProcessor //pick any name!!!
}
}
然后作为应用程序的依赖项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 默认任务,知道如何解决此问题吗?