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

问题描述 投票:6回答:3

我有一个看起来像这样的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-2 template-engine
3个回答
2
投票

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

  1. 从其他模块获取代码片段
  2. 使用include组装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,因为您可以编写内联常规脚本而无需创建自定义Java类)


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中的dependencies / modules之间合并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中。


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来实现)

参考

© www.soinside.com 2019 - 2024. All rights reserved.