JAXB XJC Possible to suppress comment creation in generated classes?

前端 未结 8 1171
渐次进展
渐次进展 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:21

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

    <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>maven-replacer-plugin</artifactId>
        <executions>
          <execution>
            <phase>prepare-package</phase>
              <goals>
                <goal>replace</goal>
              </goals>                   
            </execution>
          </executions>
          <configuration>
            <!-- assumes your xjc is putting source code here -->
            <includes>
              <include>src/main/java/**/*.java</include>
            </includes>
            <regex>true</regex>
            <regexFlags>
              <regexFlag>MULTILINE</regexFlag>
            </regexFlags>
            <replacements>
              <replacement>
                <token>(^//.*\u000a|^\u000a)*^package</token>
                <value>// your new comment
    package</value>
              </replacement>         
            </replacements>
          </configuration>
       </plugin>
    

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

    0 讨论(0)
  • 2021-01-01 10:23

    What you should you :

    Generate your classes in target :

    ${project.build.directory}/generated-sources
    

    If you add target to ignore list (svn), that's all.

    0 讨论(0)
  • 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 <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

    0 讨论(0)
  • 2021-01-01 10:32

    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.

    0 讨论(0)
  • 2021-01-01 10:42

    I'm late to the party, but since version 2.0 of the jaxb2-maven-plugin, there's a noGeneratedHeaderComments configuration option. (see the JAXB-2 Maven Plugin Docs)

    You can use it like this:

    ...
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <target>2.1</target>
                <sources>
                    <source>FirstXSD.xsd</source>
                    <source>SecondXSD.xsd</source>
                </sources>
                <xjbSources>
                    <xjbSource>OptionalBindings.xjb</xjbSource>
                </xjbSources>
                <noGeneratedHeaderComments>true</noGeneratedHeaderComments>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.glassfish.jaxb</groupId>
                    <artifactId>jaxb-xjc</artifactId>
                    <version>${jaxb.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
    ...
    

    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.

    0 讨论(0)
  • 2021-01-01 10:42

    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>
    
    0 讨论(0)
提交回复
热议问题