33

我们的项目使用 XJC 从 XSD 生成 Java 类。我正在使用 JAVA EE 6。

当我们拥有的所有 XSD 重新生成时,生成的类在文件顶部包含以下注释:

// Generated on: 2011.02.23 at 02:17:06 PM GMT 

是否可以禁止此评论?原因是我们使用 SVN 进行版本控制,每次我们重新生成类时,每个文件都显示为在 SVN 中发生了更改,尽管唯一不同的是这条注释。因此,如果可能,我想完全删除评论。

有一个-no-header指令,但我不想删除整个标题,以便后人知道它是由工具生成的文件,并且修改将被覆盖。我只想删除时间戳。(或者,我会删除内置的标题,然后以某种方式插入我自己的标题。)

4

9 回答 9

15

我正在使用这个 Maven 插件来替换该// Generated on: 2011.02.23 at 02:17:06 PM GMT行:

<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>
于 2011-08-23T11:45:13.120 回答
9

我迟到了,但是从 2.0 版开始jaxb2-maven-plugin,有一个noGeneratedHeaderComments配置选项。(请参阅JAXB-2 Maven 插件文档

你可以像这样使用它:

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

因此无需运行其他插件或脚本。

如果您想保留免责声明,您可以使用已经提到的一种技术在需要的地方注入它。

于 2017-07-25T09:30:34.080 回答
4

如果您使用ant,以下代码段可能对替换注释有用:

<replaceregexp
        match="^// Generated on:.*$" 
        replace="// Generated on: [date removed]"
        byline="true">
    <fileset dir="src">
        <include name="**/*.java"/>
    </fileset>
</replaceregexp>
于 2013-06-07T21:51:43.440 回答
4

我知道这是事实发生后的 2 年,但是由于生成了类,因此在 SVN 中不一定需要它们。SVN 中需要的是模式或您用于源生成类的任何文件。只要您拥有生成类的源代码和工具,SVN 中的类就是多余的,并且如您所见,在 SVN 或任何 SCCS 中存在问题。因此,将架构文件放入 SVN 并完全避免该问题。

于 2015-03-04T14:10:09.983 回答
3

如果无法使用选项,您可以自己对生成的文件进行后处理。对于一个非常具体的用例,我们必须在我们的项目中这样做......我们使用 Maven,并在生成 Java 类之后、编译并将它们打包到可分发的 JAR 之前执行特定的脚本。

于 2011-02-23T16:16:56.487 回答
3

为了建立 cata 的答案(赞成),maven-replacer-plugin是要走的路。我提出了以下内容,删除了整个注释(不仅仅是时间戳),您可以将其替换为文件注释(许可证等)。

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

需要注意的一个问题是该<value>元素按字面意思对待文本。因此,如果您想在替换文本中添加换行符,则需要在 pom.xml 文件中添加换行符(正如我在上面演示的那样)。

于 2011-09-09T18:02:28.920 回答
3

你应该做什么:

在 target 中生成你的类:

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

如果您将目标添加到忽略列表 (svn),仅此而已。

于 2016-12-16T23:34:36.277 回答
1

我还希望自动生成带有类警告的文本​​标题,不应手动修改,但因为我将此类文件放入 git 我不希望总是更改生成日期

该标头在com.sun.tools.xjc.Options#getPrologComment方法中生成。所以本质上它调用:

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

Messages.FILE_PROLOG_COMMENT定义为Driver.FilePrologComment。通过进一步调试,我发现它使用标准的 Java 本地化包。

因此,要更改标头格式,我们只需为MessageBundle.properties中的值提供我们的属性覆盖。

我们可以通过两种方式做到这一点:

  1. 只需将该文件(通过链接从 repo 中,或从您正在使用的适当版本的 jar 中)复制到src/main/resources/com/sun/tools/xjc/MessageBundle.properties您的项目中,然后根据需要更改密钥Driver.FilePrologComment
  2. 但是第一种情况有一些缺点 - 首先你复制粘贴了许多你没有更改的代码,其次你应该在更新XJC依赖项时更新它。所以更好的是我建议将它作为src/main/resources/com/sun/tools/xjc/MessageBundle_en.properties(注意_en文件名中的后缀)文件放置,并且只放置您真正想要更改的属性。就像是:
# 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

确保编译器类路径中的文件,特别是如果您从某些插件调用它。

这是翻译的常见机制。请参阅相关答案:生成文件中的 JAXB 英文注释

于 2019-01-22T21:42:33.730 回答
0

如果您使用的是 maven-jaxb2-plugin,则有一个标签 noFileHeader 将其设置为 true。它将阻止 jaxb 生成包含该日期行的标题。

<noFileHeader>true</noFileHeader>
于 2021-12-01T20:11:27.797 回答