JAXB XJC 可以抑制生成的类中的注释创建吗?

发布于 2024-10-18 16:41:35 字数 383 浏览 2 评论 0原文

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

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

等风也等你 2024-10-25 16:41:35

我正在使用这个 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.*
lt;/token>
        <value>// Generated on: [TEXT REMOVED by maven-replacer-plugin]</value>                         
        <regexFlags>
            <regexFlag>MULTILINE</regexFlag>
        </regexFlags>
    </configuration>
</plugin>

I am using this Maven 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.*
lt;/token>
        <value>// Generated on: [TEXT REMOVED by maven-replacer-plugin]</value>                         
        <regexFlags>
            <regexFlag>MULTILINE</regexFlag>
        </regexFlags>
    </configuration>
</plugin>
送舟行 2024-10-25 16:41:35

我来晚了,但从 jaxb2-maven-plugin 2.0 版开始,就有了 noGeneeratedHeaderComments 配置选项。 (请参阅 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>
...

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

如果您想保留免责声明,您可以使用已经提到的技术之一将其注入到需要的位置。

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.

岛歌少女 2024-10-25 16:41:35

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

<replaceregexp
        match="^// Generated on:.*$" 
        replace="// Generated on: [date removed]"
        byline="true">
    <fileset dir="src">
        <include name="**/*.java"/>
    </fileset>
</replaceregexp>

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>
卖梦商人 2024-10-25 16:41:35

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

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.

旧城烟雨 2024-10-25 16:41:35

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

需要注意的一个问题是 元素按字面意思处理文本。因此,如果您想在替换文本中添加换行符,则需要在 pom.xml 文件中添加换行符(如我上面所演示的)。

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

对岸观火 2024-10-25 16:41:35

我知道这是两年后的事了,但因为这些类是生成的,所以它们在 SVN 中不一定需要。 SVN 中需要的是架构或任何用于生成类的源文件。只要您拥有生成类的源代码和工具,SVN 中的类就是多余的,并且如您所见,在 SVN 或任何 SCCS 中都会出现问题。因此,将架构文件放入 SVN 中即可完全避免该问题。

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.

余罪 2024-10-25 16:41:35

你应该做什么:

在目标中生成你的类:

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

如果你将目标添加到忽略列表(svn),就这样。

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.

楠木可依 2024-10-25 16:41:35

我还想有关于类自动生成的警告的文本​​标题并且不应该手动修改,但是因为我将这些文件放入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. 只需将该文件(通过链接从存储库中,或仅从您正在使用的适当版本的 jar 中)复制到 src/main/resources/com/sun/tools/xjc/MessageBundle 中。在您的项目中添加属性并根据需要更改键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 英文注释

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

墟烟 2024-10-25 16:41:35

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

<noFileHeader>true</noFileHeader>

If you are using maven-jaxb2-plugin there is an tag noFileHeader set it to true. It will prevent jaxb to generate the header that includes that date line on it.

<noFileHeader>true</noFileHeader>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文