2010-08-16 43 views
5

我有一個Maven的應用程序,它看起來像這樣Maven的包括從一個模塊在另一個

application_name/ 
    module1 
     src/main/resources 
      file_snippet.xml 
    module2 
     src/main/resources 
      file_snippet.xml 
    module3 
     src/main/resources 
      file.xml 

file.xml應該是這樣的

<workflow> 
    <action> 
    <%= module1/src/main/resources/file_snippet.xml %> 
    </action> 

    <action> 
    <%= module2/src/main/resources/file_snippet.xml %> 
    </action> 

</workflow> 

我想包括file_snippet的內容文件內容在構建之前,將module2和module2中的.xml轉換爲module3的file.xml。這在maven中可能嗎?是否有某種模板語言或我可以使用的插件?

回答

1

我想知道一些關於maven的模板引擎,但也許你並不需要首先爲maven做些準備。

有一種方法,包括在一個XML文件中的另一個XML文件: http://bobcat.webappcabaret.net/javachina/faq/xml_01.htm#dtd_Q408

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE root [ 
    <!ENTITY xmlfrag SYSTEM "xmlfrag.txt" > 

    <!ELEMENT root (tag1, tag2) > 
    <!ELEMENT tag1 (childtag) > 
    <!ELEMENT tag2 (childtag) > 
    <!ELEMENT childtag (#PCDATA) > 
    <!ATTLIST childtag att NMTOKEN #REQUIRED > 
]> 
<root> 
    &xmlfrag; 
</root> 

xmlfrag.txt(well formed xml fragment without xml decl) 

<tag1> 
    <childtag att="child1">text1</childtag> 
</tag1> 
<tag2> 
    <childtag att="child2">text2</childtag> 
</tag2> 

別的,用於合併依賴性/模塊之間的XML資源 Maven中,你可以使用Maven遮陽簾插件(與資源變壓器)。

你的情況:

<project> 
    ... 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-shade-plugin</artifactId> 
     <version>1.4</version> 
     <executions> 
      <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>shade</goal> 
      </goals> 
      <configuration> 
       <transformers> 
       <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer"> 
        <resource>file.xml</resource> 
        <!-- Add this to enable loading of DTDs 
        <ignoreDtd>false</ignoreDtd> 
        --> 
       </transformer> 
       </transformers> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
    ... 
</project> 

合併模塊1/src目錄/主/資源/ file.xml和模塊2/src目錄/主/資源/ file.xml (取決於依賴)的單詞數/ src目錄/main/resources/file.xml。

2

這並不容易,你需要實現兩個部分。

  1. 得到片段從另一模塊
  2. 使用組裝XML文件中包括

爲1,你可以使用dependency:unpack魔力:

<build> 
    <plugins> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-dependency-plugin</artifactId> 
     <executions> 
     <execution> 
      <id>unpack</id> 
      <phase>initialize</phase> 
      <goals> 
      <goal>unpack</goal> 
      </goals> 
      <configuration> 
      <outputDirectory>${project.build.directory}/snippets</outputDirectory> 
      <includes>snippets/*.xml</includes> 
      <artifactItems> 
       <artifactItem> 
       <groupId>your.app</groupId> 
       <artifactId>module1</artifactId> 
       <version>${project.version}</version> 
       <type>jar</type> 
       </artifactItem> 
       <artifactItem> 
       <groupId>your.app</groupId> 
       <artifactId>module2</artifactId> 
       <version>${project.version}</version> 
       <type>jar</type> 
       </artifactItem> 
      </artifactItems> 
      </configuration> 
     </execution> 
     </executions> 
    </plugin> 
    </plugins> 
</build> 

現在你已拷貝從module1.jar/snippets和module2.jar/snippets到目標/片段(這是更容易的部分)的所有片段。

對於2.你需要選擇a template engine,並創建一個主類組裝用它你的模板,可能使用了exec:java魔力是這樣的:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.1</version> 
    <executions> 
     <execution> 
     <goals> 
      <goal>java</goal> 
     </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <mainClass>com.yourcompany.YourTemplateParser</mainClass> 
     <arguments> 
     <argument>path/to/your/template</argument> 
     <argument>path/to/the/snippets/folder</argument> 
     <argument>target/path</argument> 
     </arguments> 
    </configuration> 
    </plugin> 

(就個人而言,我傾向於使用GMaven代替exec:java,因爲你可以在不創建自定義java類的情況下編寫內聯groovy腳本)

相關問題