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

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.

查看更多
唯我独甜
3楼-- · 2019-02-08 02:40

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.

查看更多
小情绪 Triste *
4楼-- · 2019-02-08 02:43

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.

查看更多
迷人小祖宗
5楼-- · 2019-02-08 02:44

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

查看更多
等我变得足够好
6楼-- · 2019-02-08 02:52

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.

查看更多
倾城 Initia
7楼-- · 2019-02-08 02:54

I am using this mvn plugin which replaces the // Generated on: 2011.02.23 at 02:17:06 PM GMT line:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>maven-replacer-plugin</artifactId>
    <version>1.3.8</version>
    <executions>
        <execution> 
            <phase>prepare-package</phase>                          
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>                         
        <includes>                              
            <include>src/main/java/jaxb/*.java</include>            
        </includes>
        <token>^// Generated on.*$</token>
        <value>// Generated on: [TEXT REMOVED by maven-replacer-plugin]</value>                         
        <regexFlags>
            <regexFlag>MULTILINE</regexFlag>
        </regexFlags>
    </configuration>
</plugin>
查看更多
登录 后发表回答