2011-02-23 96 views
15

我們的項目使用XJC從XSD生成Java類。我使用的Java EE 6JAXB XJC可以抑制生成的類中的註釋創建?

當所有我們已經重新生成的XSD,生成的類包括在文件的頂部,此評論:

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

是否有可能抑制此評論?原因是我們使用SVN進行版本控制,並且每次我們重新生成類時,每個文件都會顯示爲在SVN中發生更改,即使唯一不同的是此評論。因此,如果可能,我想完全刪除評論。

有一個-no-header指令,但我不想刪除整個標題,以便後代知道它是從工具生成的文件,並且修改將被覆蓋。我只想刪除時間戳。 (或者,我會刪除內置頭文件,然後以某種方式插入我自己的頭文件。)

回答

3

如果無法使用某個選項,則可以自行後處理生成的文件。 對於一個非常具體的用例,我們必須在我們的項目中這樣做... 我們使用Maven,並且在生成Java類之後並且在我們編譯並將它們打包到不可用的JAR之前,我們執行特定的腳本。

14

我使用這個插件MVN替換了// 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> 
+1

此解決方案非常好,但您對ObjectFactory類做什麼?每次它生成時,它的方法都會隨機移動...... – unixorn 2013-02-13 15:20:58

+0

爲什麼在'prepare-package'階段而不是'generate-sources'階段執行此操作的任何原因?編譯後您的版本會修改源代碼... – 2017-04-24 08:29:33

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文件中放一個換行符(就像我上面演示的那樣)。

3

如果使用螞蟻,下面的片段可更換的意見是有用的:

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

謝謝,非常方便:-) – mmey 2013-12-17 22:02:36

4

我知道這是事實後2年,但因爲生成的類他們不一定在SVN中需要。 SVN中需要的是架構或用於源代碼生成類的任何文件。只要你有源代碼和工具來生成這些類,SVN中的類是多餘的,正如你所看到的,在SVN或任何SCCS中都存在問題。因此,將架構文件放在SVN中並完全避免該問題。

1

你應該你:

生成目標的類:

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

如果添加的目標忽略列表(SVN),這就是全部。

2

我遲到了,但從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> 
... 

所以不需要另一個插件或腳本來運行。

如果您想保留免責聲明,您可以使用已經提到的技術之一將其注入需要的地方。

相關問題