在Java中测试注解处理器。

huangapple go评论65阅读模式
英文:

Testing of annotation processor in java

问题

我正在开发一个注解处理器,现在我正在进行测试的下一步:

  1. 将更改提交并推送到 GitHub
  2. 使用 JitPack 构建和发布处理器
  3. 在 Idea 中刷新 Gradle。我测试项目中的 build.gradle 内容如下:
repositories {
    maven { url 'https://jitpack.io' }
}
configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
dependencies {
    compileOnly 'com.github.hohserg1:MyAnnotationProcessor:main-SNAPSHOT'
}
  1. 尝试构建

这非常糟糕。如何简化为"点击运行按钮"?

英文:

I am developing an annotation processor and now I doing next steps for testing:

  1. commit and push changes to github
  2. use jitpack for build and publish processor
  3. refresh gradle in Idea. build.gradle in my test project:
repositories {
    maven { url 'https://jitpack.io' }
}
configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
dependencies {
    compileOnly 'com.github.hohserg1:MyAnnotationProcessor:main-SNAPSHOT'
}
  1. try to build

It very terrible. How to reduce it to "press run button"?

答案1

得分: 1

你需要将处理器、注解和应用程序分别放在不同的模块中,每个模块都是一个不同的依赖项。你这样做是为了避免处理器和应用程序之间出现循环依赖,并且可以帮助你避免将处理器类包含在应用程序构件中。

此外,注意在开发注解处理器时,你不会测试处理器本身,而是测试从该处理器生成的代码,如果生成的代码能够正常工作,那么处理器也会按预期工作。

英文:

You would need your processor, your annotation, and the application to be each in a different module is a different dependency, you do this to avoid cyclic dependency between the processor and the application, and also help you to avoid including the processor classes in your application artifacts.

also, note that when you develop an annotation processor you don't test the processor itself but you test the code generated from that processor if the generated code works then the processor is also working as expected.

huangapple
  • 本文由 发表于 2020年9月28日 04:43:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64093151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定