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

发布于 2024-09-14 13:17:54 字数 653 浏览 13 评论 0原文

我有一个看起来像这样的 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中可以吗?我可以使用某种模板语言或插件吗?

I have a maven application that looks like this

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

file.xml should be like this

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

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

</workflow>

I want to include the contents of file_snippet.xml from module2 and module2 into file.xml of module3 before the build. Is that possible in maven? Is there some sort of templating language or plugin I can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帥小哥 2024-09-21 13:17:54

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

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

您可以使用 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 中的所有片段复制到 target/snippets (这是更容易的部分)。

对于2.您需要选择模板引擎并创建一个主类用它组装你的模板,可能使用 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 类)

This is not easy, there are two parts you need to implement.

  1. get the snippets from the other module
  2. assemble the xml file using the include

for 1. you can use the 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>

Now you have copied all snippets from module1.jar/snippets and module2.jar/snippets to target/snippets (this was the easier part).

For 2. you need to pick a template engine and create a main class to assemble your template with it, probably using the exec:java mojo something like this:

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

(Personally, I tend to use GMaven instead of exec:java, as you can write inline groovy scripts without creating custom java classes)

洋洋洒洒 2024-09-21 13:17:54

我也想了解 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 中。

I would like to know about some template engine for maven too, but maybe you don't need maven at first for that.

There is a way to include another xml file in a xml file :
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>

else, for merging xml resources between dependencies/modules in maven, you can use maven-shade-plugin (with the resource transformer).

In your case :

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

to merge module1/src/main/resources/file.xml and module2/src/main/resources/file.xml
(depending on dependencies) in module3/src/main/resources/file.xml.

寒冷纷飞旳雪 2024-09-21 13:17:54

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

您可以在 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

Quite an old post - but solution may still be useful in other contexts.

You can use maven-velocity-plugin in module3/pom.xml.

This will trigger velocity template expansion in the generate-sources phase.

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

Note that velocity requires that all files must be under the same TEMPLATE_ROOT directory.

If included files must transitively include other files, this can be achieved by using "#parse", and processing all files with velocity ( eg rename them to FILE.xml.vm )

References

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文