我可以在gradle中使用compileOnly替代注解处理器吗? [英] Can I use compileOnly as a replacement for annotationProcessor in gradle?

查看:332
本文介绍了我可以在gradle中使用compileOnly替代注解处理器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对注释处理器的当前理解是,它指的是预先准备文件以查找某些注释,基于该注释生成或更改其他代码的代码.它发生在项目的常规编译阶段之前.

My current understanding of annotation processor is that it refers to code that pre-parses a file looking for certain annotations, generating or changing other code based on that. It happens before the regular compilation phase of your project.

在gradle中,我们通常使用apt,kpt-我有时看到使用annotationProcessor-表示在注释处理时"需要一些依赖.

In gradle we typically use apt, kpt - and I've seen sometimes the use of annotationProcessor - to indicate that some dependency will be needed at "annotation processing time".

如果上面的理解是正确的,那么compileOnly与apt,kpt等有何不同?

If the understand above is right, how does compileOnly differs from apt, kpt, etc?

推荐答案

您已经说过,Gradle中有一些注释处理解决方案:

As you've said, there are couple of annotation processing solutions in Gradle:

  1. annotationProcessor 设施适用于Android
  2. apt 用于纯Java和Groovy
  3. kapt for Kotlin

,依此类推.您甚至可以自己实现!他们都使用单独的configuration进行注释处理.

and so on. You can even implement one yourself! All of them use separate configuration for annotation processing.

实际上,其中一些确实曾经使用compile类路径进行处理.但这不是 emem 权限,它不是渐变方式".您不应将仅编译时间 time 的依赖项与工件混合在一起,而这些工件是应用程序运行所必需的.我可以想到的一种简单方案是创建胖的JAR:您很可能不想打包和运送所用的处理器,因为这没有任何意义!可能还有其他情况.

Some of them, indeed, used to use compile classpath for processing. But this is not semantically corrent, it is not a "Gradle way". You should not mix your compile time only dependencies with the artifacts, required for you app to run. One simple scenario I can think of is creating fat JARs: most probably you do not want to pack and ship the processors you've used because it makes no sense! There may be other scenarios as well.

得益于Gradle的灵活性,您可以做的是创建另一个类路径(configuration),并将其仅用于注释处理,然后再忽略它们.这是一种语义:您要告诉Gradle(和其他开发人员),这些依赖关系对于您的应用程序运行不是必需的.这是compileOnlyapt不同的地方:compileOnly依赖关系是您的代码操作所必需的,但它们必须由环境提供.它是您的应用程序服务器,还是插件宿主系统,或者甚至是将它们手动添加到类路径中-它们将在您的运行时存在,因此您不应将它们与可分发文件打包在一起.但是它们是运行代码所必需的. compileOnly依赖项的一些示例是Servlet API(您的类显然可以扩展并使用它们,但是它将由服务器提供),或者,如果您正在编写Jenkins插件,则可以使用Jenkins核心API(您的插件将安装在核心已经存在的詹金斯(). JDK本身也属于compileOnly.相反,注解处理器根本不打算在运行时使用.它们在类路径中将不存在,并且不需要它们来运行您的应用程序:它们已经生成了一些代码,这些代码随后会被编译.

What you can do, thanks to Gradle's flexibility, is create another classpath (configuration) and use it only for annotation processing and then just forget about them. It is kind of semantics: you're telling Gradle (and other developers) that these dependencies are not required for you application to run. And this is the place where compileOnly differs from apt: compileOnly dependencies are mandatory for you code to operate, but they are meant to be provided by the environment. Will it be your application server, or plugin host system, or even you'll add them to the classpath manually - they will just exist on you runtime so you should not pack them with your distributable. But they are required to for your code to run. Some examples of compileOnly dependencies are Servlet API (your classes obviously extend and use them, but it will be provided by the server) or, if you're writing a Jenkins plugin, Jenkins core APIs (your plugin will be installed in a Jenkins where that core already exists). JDK itself is kind of compileOnly too. Annotation processors, on the contrary, are not meant to be used at runtime at all. They won't exist on classpath and they are not needed to run your app: they already generated some code that was compiled later.

混合"配置的其他含义是性能.请允许我引用Android的文档:

Other implication of "mixing" configuration is performance. Just let me quote Android's documentation:

在该插件的早期版本中,对编译类路径的依赖项已自动添加到处理器类路径中.也就是说,您可以将注释处理器添加到编译类路径中,并且它将按预期工作.但是,这会通过向处理器添加大量不必要的依赖关系而对性能产生重大影响.

In previous versions of the plugin, dependencies on the compile classpath were automatically added to the processor classpath. That is, you could add an annotation processor to the compile classpath and it would work as expected. However, this causes a significant impact to performance by adding a large number of unnecessary dependencies to the processor.

这篇关于我可以在gradle中使用compileOnly替代注解处理器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆