• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

tbroyer/gradle-apt-plugin: [OBSOLETE] Gradle plugin making it easier/safer to us ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

tbroyer/gradle-apt-plugin

开源软件地址(OpenSource Url):

https://github.com/tbroyer/gradle-apt-plugin

开源编程语言(OpenSource Language):

Groovy 46.7%

开源软件介绍(OpenSource Introduction):

gradle-apt-plugin

The goal of this plugin was to eventually no longer be needed, being superseded by built-in features. This has become a reality with Gradle 5.2 and IntelliJ IDEA 2019.1. tl;dr: this plugin is obsolete, don't use it. If you're using Eclipse though, continue reading.

It originally did a few things to make it easier/safer to use Java annotation processors in a Gradle build. Those things are now available natively in Gradle, so what's this plugin about?

If you use older versions of Gradle (pre-4.6), you can still benefit from those features:

  • it ensures the presence of configurations for your compile-time only dependencies (annotations, generally) and annotation processors, consistently across all supported Gradle versions;
  • automatically configures the corresponding JavaCompile and GroovyCompile tasks to make use of these configurations, when the java or groovy plugin is applied.

With recent versions of Gradle (between 4.6 and 5.1), this plugin will actually only:

  • add some DSL to configure annotation processors; it is however recommended to directly configure the tasks' options.compilerArgs;
  • backport the sourceSet.output.generatedSourcesDirs Gradle 5.2 API;
  • configure JavaCompile and GroovyCompile tasks' options.annotationProcessorGeneratedSourcesDirectory with a sane default value so you can see the generated sources in your IDE and for debugging, and avoid shipping them in your JARs.

With Gradle 5.2 and later, only the (deprecated) DSL is contributed.

Quite ironically, what you'd have probably found the most useful here was the part that was only provided as a "best effort", namely the net.ltgt.apt-idea or net.ltgt.apt-eclipse plugins that will automatically configures IntelliJ IDEA and Eclipse respectively.

If you're interested in better IDE support, please vote for those issues to eventually have built-in support:

  • in Gradle for the idea and eclipse plugins
  • in Eclipse Buildship
  • in IntelliJ IDEA for annotation processing in the IDE (probably low priority as delegating build/run actions to Gradle is becoming the default in 2019.1) (fixed in 2019.3)

If you're using Eclipse, please migrate to the com.diffplug.eclipse.apt plugin, which is a (maintained) fork of net.ltgt.apt-eclipse.

Note: the documentation below only applies to version 0.21. For version 0.20, see the previous version of this README. For previous versions, please see this even earlier version.

Do without the plugin

This only applies if you are using Gradle ≥ 4.3

Do without net.ltgt.apt

DSL aside, the net.ltgt.apt plugin, is equivalent to the following snippet (unless otherwise noted, all snippets here assume Gradle ≥ 4.9, and only deal with Java projects, not Groovy ones):

Groovy
// Workaround for https://github.com/gradle/gradle/issues/4956
sourceSets.configureEach { sourceSet ->
  tasks.named(sourceSet.compileJavaTaskName).configure {
    options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/annotationProcessor/java/${sourceSet.name}")
  }
}
Kotlin
// Workaround for https://github.com/gradle/gradle/issues/4956
sourceSets.configureEach {
    tasks.named<JavaCompile>(compileJavaTaskName) {
        options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/annotationProcessor/java/${this@configureEach.name}")
    }
}

With Gradle ≤ 4.6, you'll also need to create the <sourceSet>AnnotationProcessor configurations and configure the tasks' options.annotationProcessorPath (the following snippet will disable falling back to the compilation classpath to resolve annotation processors though):

sourceSets.all {
  def annotationProcessorConfiguration = configurations.create(name == "main" ? "annotationProcessor" : "${name}AnnotationProcessor")
  tasks[compileJavaTaskName].options.annotationProcessorPath = annotationProcessorConfiguration
}

Alternatively, you can selectively create configurations and configure tasks as needed:

configurations {
  annotationProcessor
}
compileJava {
  options.annotationProcessorPath = configurations.annotationProcessor
}

Do without net.ltgt.apt-idea

If you're delegating IDEA build/run actions to Gradle, and/or somehow don't want or need IntelliJ IDEA to do the annotation processing, the net.ltgt.apt-idea plugin is roughly equivalent to the following snippet (this assumes a somewhat recent version of IntelliJ IDEA that automatically unexcludes source folders inside excluded folders):

Groovy
// Workaround for https://youtrack.jetbrains.com/issue/IDEA-182577
idea {
  module {
    sourceDirs += compileJava.options.annotationProcessorGeneratedSourcesDirectory
    generatedSourceDirs += compileJava.options.annotationProcessorGeneratedSourcesDirectory
    testSourceDirs += compileTestJava.options.annotationProcessorGeneratedSourcesDirectory
    generatedSourceDirs += compileTestJava.options.annotationProcessorGeneratedSourcesDirectory
  }
}
Kotlin
// Workaround for https://youtrack.jetbrains.com/issue/IDEA-182577
idea {
    module {
        tasks.getByName<JavaCompile>("compileJava").options.annotationProcessorGeneratedSourcesDirectory?.also {
            // For some reason, modifying the existing collections doesn't work. We need to copy the values and then assign it back.
            sourceDirs = sourceDirs + it
            generatedSourceDirs = generatedSourceDirs + it
        }
        tasks.getByName<JavaCompile>("compileTestJava").options.annotationProcessorGeneratedSourcesDirectory?.also {
            // For some reason, modifying the existing collections doesn't work. We need to copy the values and then assign it back.
            testSourceDirs = testSourceDirs + it
            generatedSourceDirs = generatedSourceDirs + it
        }
    }
}
With Gradle 5.2 (and a version of IntelliJ IDEA older than 2019.1)

You can instead use:

Groovy
// Workaround for https://youtrack.jetbrains.com/issue/IDEA-182577
idea {
  module {
    sourceDirs += sourceSets.main.output.generatedSourcesDirs
    generatedSourceDirs += sourceSets.main.output.generatedSourcesDirs
    testSourceDirs += sourceSets.test.output.generatedSourcesDirs
    generatedSourceDirs += sourceSets.test.output.generatedSourcesDirs
  }
}
Kotlin
// Workaround for https://youtrack.jetbrains.com/issue/IDEA-182577
idea {
    module {
        tasks.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).output.generatedSourcesDirs.also {
            // For some reason, modifying the existing collections doesn't work. We need to copy the values and then assign it back.
            sourceDirs = sourceDirs + it
            generatedSourceDirs = generatedSourceDirs + it
        }
        tasks.sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME).output.generatedSourcesDirs.also {
            // For some reason, modifying the existing collections doesn't work. We need to copy the values and then assign it back.
            testSourceDirs = testSourceDirs + it
            generatedSourceDirs = generatedSourceDirs + it
        }
    }
}

If you want IntelliJ IDEA to process annotations, you'll have to do some manual configuration to enable annotation processing, and add the annotation processors to the project's compilation classpath:

Groovy
// Workaround for https://youtrack.jetbrains.com/issue/IDEA-187868
idea {
  module {
    scopes.PROVIDED.plus += configurations.annotationProcessor
    scopes.TEST.plus += configurations.testAnnotationProcessor
  }
}
Kotlin
// Workaround for https://youtrack.jetbrains.com/issue/IDEA-187868
idea {
    module {
        scopes["PROVIDED"]!!["plus"]!!.add(configurations.annotationProcessor)
        scopes["TEST"]!!["plus"]!!.add(configurations.testAnnotationProcessor)
    }
}

If you want to apply this configuration automatically for every project, you can do this with an init script similar to the following:

import org.gradle.util.GradleVersion;

// If running from IntelliJ IDEA (such as when importing the project)
if (Boolean.getBoolean("idea.active")) {
  def HAS_PROCESSOR_GENERATED_SOURCES_DIR = GradleVersion.current() >= GradleVersion.version("4.3")

  allprojects { project ->
    project.apply plugin: 'idea'

    if (HAS_PROCESSOR_GENERATED_SOURCES_DIR) {
      project.plugins.withType(JavaPlugin) {
        project.afterEvaluate {
          project.idea.module {
            def mainGeneratedSources = project.tasks["compileJava"].options.annotationProcessorGeneratedSourcesDirectory
            if (mainGeneratedSources) {
              sourceDirs += mainGeneratedSources
              generatedSourceDirs += mainGeneratedSources
            }
            def testGeneratedSources = project.tasks["compileTestJava"].options.annotationProcessorGeneratedSourcesDirectory
            if (testGeneratedSources) {
              testSourceDirs += testGeneratedSources
              generatedSourceDirs += testGeneratedSources
            }

            // Uncomment if you want to do annotation processing in IntelliJ IDEA:
            // def annotationProcessorConfiguration = configurations.findByName("annotationProcessor")
            // if (annotationProcessorConfiguration) {
            //   scopes.PROVIDED.plus += annotationProcessorConfiguration
            // }
            // def testAnnotationProcessorConfiguration = configurations.findByName("testAnnotationProcessor")
            // if (testAnnotationProcessorConfiguration) {
            //   scopes.TEST.plus += testAnnotationProcessorConfiguration
            // }
          }
        }
      }
    } else {
      // fallback to automatically applying net.ltgt.apt-idea whenever net.ltgt.apt is used
      project.plugins.withId("net.ltgt.apt") {
        try {
          project.apply plugin: "net.ltgt.apt-idea"
          // Comment if you want to do annotation processing in IntelliJ IDEA:
          project.plugins.withType(JavaPlugin) {
            project.afterEvaluate {
              project.idea.module.apt.addAptDependencies = false
            }
          }
        } catch (UnknownPluginException) {
          // ignore, in case an older version of net.ltgt.apt is being used
          // that doesn't come with net.ltgt.apt-idea.
        }
      }
    }
  }
}

Do without net.ltgt.apt-eclipse

There's no easy workaround at this point, sorry


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap