Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Maven 2 Maven将一个模块中的文件内容包含在另一个模块中_Maven 2_Template Engine - Fatal编程技术网

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

Maven 2 Maven将一个模块中的文件内容包含在另一个模块中,maven-2,template-engine,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 <workflow>

我有一个像这样的maven应用程序

application_name/
    module1
        src/main/resources
            file_snippet.xml
    module2
        src/main/resources
            file_snippet.xml
    module3
        src/main/resources
            file.xml
<workflow>
  <action>
  <%= module1/src/main/resources/file_snippet.xml %>
  </action>

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

</workflow>
file.xml应该是这样的

application_name/
    module1
        src/main/resources
            file_snippet.xml
    module2
        src/main/resources
            file_snippet.xml
    module3
        src/main/resources
            file.xml
<workflow>
  <action>
  <%= module1/src/main/resources/file_snippet.xml %>
  </action>

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

</workflow>


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

我也想知道一些maven的模板引擎,但也许你一开始并不需要maven

有一种方法可以在xml文件中包含另一个xml文件:


]>
&xmlfrag;
txt(没有xml decl的格式良好的xml片段)
文本1
文本2
否则,要在maven中的依赖项/模块之间合并xml资源,可以使用maven shade插件(带有资源转换器)

就你而言:

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

...
org.apache.maven.plugins
maven阴影插件
1.4
包裹
阴凉处
file.xml
...
合并module1/src/main/resources/file.xml和module2/src/main/resources/file.xml
(取决于依赖关系)在module3/src/main/resources/file.xml中。

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

  • 从另一个模块获取代码段
  • 使用include命令组装xml文件
  • 1。你可以使用魔咒:

    
    org.apache.maven.plugins
    然后创建一个主类,用它来组装模板,可能使用类似以下的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>
    
    
    org.codehaus.mojo
    
    而不是exec:java,因为您可以在不创建自定义java类的情况下编写内联groovy脚本)

    这是一篇相当古老的文章,但解决方案在其他上下文中可能仍然有用

    您可以在module3/pom.xml中使用maven velocity插件

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

    <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>
    
    
    com.github.vdubus
    
    
  • 针对avro架构聚合的相同问题的解决方案:
  • <workflow>
      <action>
      #include("module1/src/main/resources/file_snippet.xml")
      </action>
    
      <action>
      #include("module2/src/main/resources/file_snippet.xml")
      </action>
    
    </workflow>