JAXB XJC 可以抑制生成的类中的注释创建吗?
我们的项目使用 XJC 从 XSD 生成 Java 类。我正在使用 JAVA EE 6。
重新生成我们拥有的所有 XSD 时,生成的类在文件顶部包含此注释:
// Generated on: 2011.02.23 at 02:17:06 PM GMT
是否可以抑制此注释?原因是我们使用 SVN 进行版本控制,每次重新生成类时,每个文件都会在 SVN 中显示为已更改,尽管唯一不同的是此注释。因此,如果可能的话,我想完全删除该评论。
有一个 -no-header
指令,但我不想删除整个标头,以便后代知道它是由工具生成的文件,并且修改将被覆盖。我只想删除时间戳。 (或者,我会删除内置标头,然后以某种方式插入我自己的标头。)
Our project uses XJC to generate Java classes from an XSD. I'm using JAVA EE 6.
When all the XSDs we have are re-generated, the generated classes include this comment at the top of the file:
// Generated on: 2011.02.23 at 02:17:06 PM GMT
Is it possible to suppress this comment? The reason is that we use SVN for version control, and every time we regenerate our classes, every single file shows as being changed in SVN, even though the only thing that differs is this comment. So I'd like to remove the comment altogether if possible.
There is a -no-header
directive, but I don't want to remove the entire header, so that future generations know that it's a file generated from a tool, and that modifications will be overwritten. I only want to remove the timestamp. (Or alternatively, I'd remove the inbuilt header and then insert my own header somehow.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我正在使用这个 Maven 插件,它替换了
// generated on: 2011.02.23 at 02:17:06 PM GMT
行:I am using this Maven plugin which replaces the
// Generated on: 2011.02.23 at 02:17:06 PM GMT
line:我来晚了,但从
jaxb2-maven-plugin
2.0 版开始,就有了noGeneeratedHeaderComments
配置选项。 (请参阅 JAXB-2 Maven 插件文档)您可以像这样使用它:
因此不需要运行其他插件或脚本。
如果您想保留免责声明,您可以使用已经提到的技术之一将其注入到需要的位置。
I'm late to the party, but since version 2.0 of the
jaxb2-maven-plugin
, there's anoGeneratedHeaderComments
configuration option. (see the JAXB-2 Maven Plugin Docs)You can use it like this:
So no need for another plugin or script to run.
If you want to keep a disclaimer, you can use one of the techniques already mentioned to inject it where wanted.
如果您使用 ant,以下代码片段可能有助于替换注释:
If you use ant, the following snippet may be useful for replacing the comments:
如果无法使用选项,您可以自己对生成的文件进行后处理。
对于一个非常具体的用例,我们必须在我们的项目中这样做......
我们使用 Maven,并在生成 Java 类之后、将它们编译并打包到可分发的 JAR 之前执行特定的脚本。
If it's not possible using an option you can post-process the generated files yourself.
For a very specific use-case we had to do it that way on our project...
We use Maven and we execute a specific script after the Java classes have been generated and before we compile and package them to a distriuable JAR.
要在cata的答案(已投票)的基础上构建 maven-replacer-plugin 是要走的路。我提出了以下内容,它删除了整个注释(而不仅仅是时间戳),您可以将其替换为文件注释(许可证等)。
需要注意的一个问题是
元素按字面意思处理文本。因此,如果您想在替换文本中添加换行符,则需要在 pom.xml 文件中添加换行符(如我上面所演示的)。To build on cata's answer (upvoted) the maven-replacer-plugin is the way to go. I've come up with the following that strips out the entire comment (not just the timestamp) which you can replace with your file comment (license etc.).
The one gotcha to watch out for is that the
<value>
element treats the text literally. So if you want a line break in your replacement text you need to put a line break in your pom.xml file (as I've demonstrated above).我知道这是两年后的事了,但因为这些类是生成的,所以它们在 SVN 中不一定需要。 SVN 中需要的是架构或任何用于生成类的源文件。只要您拥有生成类的源代码和工具,SVN 中的类就是多余的,并且如您所见,在 SVN 或任何 SCCS 中都会出现问题。因此,将架构文件放入 SVN 中即可完全避免该问题。
I know this is 2 years after the fact, but because the classes are generated they aren't necessarily needed in SVN. What needs to be in SVN is the schema or whatever file you use for source to generate the classes. As long as you have the source and the tools to generate the classes, the classes in SVN are redundant and as you saw, problematic in SVN or any SCCS. So put the schema file in SVN and avoid the issue altogether.
你应该做什么:
在目标中生成你的类:
如果你将目标添加到忽略列表(svn),就这样。
What you should you :
Generate your classes in target :
If you add target to ignore list (svn), that's all.
我还想有关于类自动生成的警告的文本标题并且不应该手动修改,但是因为我将这些文件放入git中,所以我不希望总是更改生成日期。
该标头在 com.sun.tools.xjc.Options#getPrologComment 方法。所以本质上它调用:
Messages.FILE_PROLOG_COMMENT
定义为Driver.FilePrologComment
。经过进一步调试,我发现它使用标准 Java 本地化包。因此,要更改标头格式,我们只需提供 MessageBundle.properties。
我们可以通过两种方式做到这一点:
Driver.FilePrologComment
。XJC
依赖项时更新它。因此,我建议将其作为src/main/resources/com/sun/tools/xjc/MessageBundle_en.properties
(注意文件名中的_en
后缀)文件放置并仅放置在那里您真正想要更改的属性。类似于:确保该文件位于编译器类路径中,特别是当您从某些插件调用它时。
这是通用的翻译机制。请参阅相关答案:生成文件中的 JAXB 英文注释
I also want to have text header with warning about classes was auto-generated and should not be modified manually, but because I place such files into git I do not want there always changed date of generation.
That header generated in com.sun.tools.xjc.Options#getPrologComment method. So essentially it call:
Messages.FILE_PROLOG_COMMENT
defined asDriver.FilePrologComment
. With futher debugging I found it use standard Java localization bundles.So, to change header format we just may provide our properties override for their values from MessageBundle.properties.
We can do it in two way:
src/main/resources/com/sun/tools/xjc/MessageBundle.properties
in your project and change keyDriver.FilePrologComment
as you wish.XJC
dependency. So better I recommend place it assrc/main/resources/com/sun/tools/xjc/MessageBundle_en.properties
(note_en
suffix in filename) file and place there only properties you really want to change. Something like:Ensure that file in compiler classpath, especially if you call it from some plugins.
That is common mechanism for translation. See related answer: JAXB english comments in generated file
如果您使用 maven-jaxb2-plugin,则有一个标签 noFileHeader 将其设置为 true。它将阻止 jaxb 生成包含该日期行的标头。
If you are using maven-jaxb2-plugin there is an tag noFileHeader set it to true. It will prevent jaxb to generate the header that includes that date line on it.