简体   繁体   English

maven-compiler-plugin:为增量构建添加额外的依赖

[英]maven-compiler-plugin: add extra dependency for incremental build

I want to construct a java class file with a class with a long, multi-line string constant.我想用一个带有长多行字符串常量的类来构造一个 java 类文件。 (The usual way of putting the string into a separate file and load it from the same JAR as the class is not an option. There won't be a JAR, only that class file.) Originally, I was using the multiline-string annotation-processor, it allows to place the string content in a doc comment: (将字符串放入单独的文件并从与类相同的 JAR 加载它的通常方法不是一种选择。不会有 JAR,只有那个类文件。)最初,我使用的是多行字符串注释处理器,它允许将字符串内容放在文档注释中:

/**
 * This will be the actual
 * content of the string
**/
@Multiline static final String foo = "this literal is ignored";

Here I mistakenly thought: "oh, comments are inconvenient, why not change multiline-string to load the content from an external file?"在这里我误以为:“哦,注释不方便,为什么不改变multiline-string从外部文件加载内容?” It turns out that this works, but maven (which I'm using for building), does of course not understand that the class file needs to be recompiled if the file read by the annotation processor is changed.事实证明这是可行的,但是 maven(我用于构建)当然不明白如果注释处理器读取的文件发生更改,则需要重新编译类文件。

Approaches:方法:

  • set useIncrementalCompilation to false : Does not at all do what I would expect, instead disables dependency checking for incremental buildsuseIncrementalCompilation设置为false :根本不做我期望的事情,而是禁用增量构建的依赖项检查
  • add .py to fileExtensions (didn't expect that to work).py添加到fileExtensions (没想到会起作用)
  • add the maven-clean-plugin as part of the initialization phase: This works, but it is a bit overkill…添加maven-clean-plugin作为初始化阶段的一部分:这可行,但有点矫枉过正……

What I'd really like to do is to add the path of the embedded file to maven's list of "if this file changes, recompile that file".我真正想做的是将嵌入文件的路径添加到 maven 的“如果此文件更改,重新编译该文件”列表中。 Is that possible, or is there another, elegant way of solving this?这是可能的,还是有另一种优雅的方法来解决这个问题?

Maybe this is not a full answer, but just my thoughts on the above:也许这不是一个完整的答案,而只是我对上述问题的看法:

Annotation processors in java usually are not intended for changing the existing classes. Java 中的注解处理器通常不用于更改现有类。

Yes, there is Lombok, that kind of breaks this assumption, but the price is using some internal compiler internal APIs and "Manually" compiling the modified AST:是的,有 Lombok,这打破了这个假设,但代价是使用一些内部编译器内部 API 并“手动”编译修改后的 AST:

This is also written in this tutorial for example.例如,这也写在本教程中。

Usually annotation processors can create some descriptors, like JSON or files, or, alternatively new Java sources, but they never (again with the exception of lombok) modify the existing sources.通常注释处理器可以创建一些描述符,比如 JSON 或文件,或者新的 Java 源,但它们永远不会(同样除了 lombok 之外)修改现有的源。

If I've got you correctly, you wrote your own annotation processor for multi-line handling like the one that you've provided in the first link , but this kind of incompatible with the idea of annotation processors, so I doubt maven can help you out here.如果我对您的理解正确,您会像在第 一个链接中提供的那样为多行处理编写自己的注释处理器,但是这种与注释处理器的想法不兼容,所以我怀疑 maven 可以提供帮助你在这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM