Maven包含另一个模块中的文件内容

时间:2010-08-16 18:18:53

标签: maven-2 template-engine

我有一个看起来像这样的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>

我想在构建之前将module2和module2中的file_snippet.xml的内容包含到module3的file.xml中。这在maven有可能吗?我可以使用某种模板语言或插件吗?

3 个答案:

答案 0 :(得分:2)

这并不容易,需要实现两个部分。

  1. 从其他模块获取片段
  2. 使用include
  3. 组装xml文件

    表示1.你可以使用dependency:unpack mojo:

    <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 mojo,如下所示:

      <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,因为你可以编写内联groovy脚本而无需创建自定义java类)

答案 1 :(得分: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>

否则,为了在maven中合并依赖关系/模块之间的xml资源,可以使用maven-shade-plugin(使用资源转换器)。

在你的情况下:

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

合并module1 / src / main / resources / file.xml和module2 / src / main / resources / file.xml (取决于依赖关系)在module3 / src / main / resources / file.xml中。

答案 2 :(得分:0)

一个很老的帖子-但解决方案在其他情况下仍然可能有用。

您可以在module3 / pom.xml中使用maven-velocity-plugin。

这将在“生成源”阶段触发速度模板扩展。

<build>
        <plugins>
            <plugin>
                <groupId>com.github.vdubus</groupId>
                <artifactId>velocity-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <id>Generate source velocity</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>velocity</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <removeExtension>.vm</removeExtension>
                            <templateFiles>
                                <directory>${project.basedir}/..</directory>
                                <includes>
                                    <include>**/*.vm</include>
                                </includes>
                            </templateFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
   </plugins>
</build>

module3 / src / main / resources / file.xml.vm:

<workflow>
  <action>
  #include("module1/src/main/resources/file_snippet.xml")
  </action>

  <action>
  #include("module2/src/main/resources/file_snippet.xml")
  </action>

</workflow>

请注意,速度要求所有文件必须位于同一TEMPLATE_ROOT目录下。

如果包含的文件必须可传递地包括其他文件,则可以使用“ #parse”并快速处理所有文件(例如,将它们重命名为FILE.xml.vm)来实现

参考