我们的项目使用XJC从XSD生成Java类。我正在使用JAVA EE 6。
当重新生成我们拥有的所有XSD时,生成的类在文件顶部包含此注释:
// Generated on: 2011.02.23 at 02:17:06 PM GMT
是否可以取消此评论?原因是我们使用SVN进行版本控制,每次我们重新生成类时,每个文件都显示为在SVN中被更改,即使唯一不同的是此注释。所以如果可能的话,我想完全删除评论。
有-no-header
指令,但我不想删除整个标题,以便后代知道它是从工具生成的文件,并且修改将被覆盖。我只想删除时间戳。 (或者,我会删除内置的标题,然后以某种方式插入我自己的标题。)
答案 0 :(得分:14)
我正在使用这个替换// Generated on: 2011.02.23 at 02:17:06 PM GMT
行的maven插件:
<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>
答案 1 :(得分:5)
我迟到了,但是从jaxb2-maven-plugin
的2.0版开始,就有noGeneratedHeaderComments
个配置选项。 (见JAXB-2 Maven Plugin Docs)
你可以像这样使用它:
...
<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>
...
因此无需运行其他插件或脚本。
如果您想保留免责声明,可以使用已提及的技术之一将其注入所需的位置。
答案 2 :(得分:4)
我知道这是事实发生后2年,但由于生成了类,因此SVN中不一定需要这些类。 SVN中需要的是架构或用于生成类的源的任何文件。只要您拥有生成类的源和工具,SVN中的类就是多余的,如您所见,在SVN或任何SCCS中都存在问题。所以把模式文件放在SVN中,完全避免这个问题。
答案 3 :(得分:3)
如果无法使用选项,您可以自行对生成的文件进行后处理。 对于一个非常具体的用例,我们必须在我们的项目中这样做... 我们使用Maven,在生成Java类之后,在编译并将它们打包到一个可分析的JAR之前,我们执行一个特定的脚本。
答案 4 :(得分:3)
要建立在cata的答案(upvoted)之上,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文件中添加换行符(如上所述)。
答案 5 :(得分:3)
如果您使用 ant ,则以下代码段可能有助于替换评论:
<replaceregexp
match="^// Generated on:.*$"
replace="// Generated on: [date removed]"
byline="true">
<fileset dir="src">
<include name="**/*.java"/>
</fileset>
</replaceregexp>
答案 6 :(得分:2)
你应该做什么:
在目标中生成您的课程:
${project.build.directory}/generated-sources
如果你将目标添加到忽略列表(svn),那就是全部。
答案 7 :(得分:0)
我还希望具有自动生成的带有警告类的文本标题,并且不应该手动修改,但是因为我将此类文件放入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中的区域值提供属性覆盖。
我们可以通过两种方式做到这一点:
src/main/resources/com/sun/tools/xjc/MessageBundle.properties
中,然后根据需要更改密钥Driver.FilePrologComment
。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 english comments in generated file