JAXB XJC Possible to suppress comment creation in

2019-02-08 02:09发布

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.)

8条回答
孤傲高冷的网名
2楼-- · 2019-02-08 02:55

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 int 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:

return Messages.format(
            Messages.FILE_PROLOG_COMMENT,
dateFormat.format(new Date()));

Messages.FILE_PROLOG_COMMENT defined as Driver.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 teir values from MessageBundle.properties.

We can do it in two way:

  1. Just copy that file (from repo by link, or just from jar of appropriate version what you are using) into src/main/resources/com/sun/tools/xjc/MessageBundle.properties in your project and change key Driver.FilePrologComment as you wish.
  2. But first case have some drawbacks - first you copy-paste many code which you do not change, second you should update it when you update XJC dependency. So better I recommend place it as src/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:
# 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

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

查看更多
甜甜的少女心
3楼-- · 2019-02-08 02:56

If you use ant, the following snippet may be useful for replacing the comments:

<replaceregexp
        match="^// Generated on:.*$" 
        replace="// Generated on: [date removed]"
        byline="true">
    <fileset dir="src">
        <include name="**/*.java"/>
    </fileset>
</replaceregexp>
查看更多
登录 后发表回答