JAXB XJC Possible to suppress comment creation in generated classes?

前端 未结 8 1212
渐次进展
渐次进展 2021-01-01 09:45

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 comm

8条回答
  •  耶瑟儿~
    2021-01-01 10:25

    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:

    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 their 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 https://javaee.github.io/jaxb-v2/ \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

提交回复
热议问题