我还希望自动生成带有类警告的文本标题,不应手动修改,但因为我将此类文件放入 git 我不希望总是更改生成日期。
该标头在com.sun.tools.xjc.Options#getPrologComment方法中生成。所以本质上它调用:
return Messages.format(
Messages.FILE_PROLOG_COMMENT,
dateFormat.format(new Date()));
Messages.FILE_PROLOG_COMMENT
定义为Driver.FilePrologComment
。通过进一步调试,我发现它使用标准的 Java 本地化包。
因此,要更改标头格式,我们只需为MessageBundle.properties中的值提供我们的属性覆盖。
我们可以通过两种方式做到这一点:
- 只需将该文件(通过链接从 repo 中,或从您正在使用的适当版本的 jar 中)复制到
src/main/resources/com/sun/tools/xjc/MessageBundle.properties
您的项目中,然后根据需要更改密钥Driver.FilePrologComment
。
- 但是第一种情况有一些缺点 - 首先你复制粘贴了许多你没有更改的代码,其次你应该在更新
XJC
依赖项时更新它。所以更好的是我建议将它作为src/main/resources/com/sun/tools/xjc/MessageBundle_en.properties
(注意_en
文件名中的后缀)文件放置,并且只放置您真正想要更改的属性。就像是:
# We want header, but do NOT willing there `Generated on: {0}` part because want commit them into git!
Driver.FilePrologComment = \
This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.4.0-b180830.0438 \n\
See <a href="https://javaee.github.io/jaxb-v2/">https://javaee.github.io/jaxb-v2/</a> \n\
Any modifications to this file will be lost upon recompilation of the source schema. \n
确保编译器类路径中的文件,特别是如果您从某些插件调用它。
这是翻译的常见机制。请参阅相关答案:生成文件中的 JAXB 英文注释