【问题标题】:Maven include file contents from one module in anotherMaven 将一个模块的文件内容包含在另一个模块中
【发布时间】:2010-08-16 18:18:52
【问题描述】:

我有一个看起来像这样的 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_sn-p.xml 的内容包含到 module3 的 file.xml 中。这在maven中可能吗?我可以使用某种模板语言或插件吗?

【问题讨论】:

    标签: maven-2 template-engine


    【解决方案1】:

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

    1. 从其他模块获取 sn-ps
    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/sn-ps 和 module2.jar/sn-ps 中的所有 sn-ps 复制到 target/sn-ps(这是更容易的部分)。

    对于 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 类)

    【讨论】:

      【解决方案2】:

      我也想知道一些 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 中。

      【讨论】:

        【解决方案3】:

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

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

        这将在 generate-sources 阶段触发速度模板扩展。

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

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

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

        参考文献

        【讨论】:

          猜你喜欢
          • 2016-12-29
          • 2020-04-17
          • 1970-01-01
          • 2014-06-12
          • 1970-01-01
          • 2020-01-26
          • 2013-06-01
          • 1970-01-01
          • 2017-05-14
          相关资源
          最近更新 更多