【问题标题】:Maven concat files specific files in a directoryMaven concat 文件目录中的特定文件
【发布时间】:2015-03-23 09:30:27
【问题描述】:

我有一个未知深度和文件夹名称的目录。

>A
-> AB
--> configuration.xml
--> ABC
---> configuration.xml
-> AD
--> configuration.xml
-> allconfigurations.xml

我需要一个 maven 插件来连接所有 configuration.xml 文件并在根目录中创建 allconfigurations.xml 文件。不幸的是,文件夹名称和深度未知。在pom.xml 文件中完成它而不需要任何其他文件会很棒。

【问题讨论】:

    标签: java xml maven ant maven-plugin


    【解决方案1】:

    只是给你一个快速的谷歌:带有 XmlAppendingTransformer 的 maven-shade-plugin 可以提供帮助。

    示例配置为here

    【讨论】:

      【解决方案2】:

      在苦苦挣扎时,我意识到真正的问题是为编译的allconfigurations.xml 文件添加页眉和页脚,因为每个configuration.xml 文件都是一个片段,当我将它们全部连接在一起时,生成的 xml 不是有效的 xml .

      情况就是这样; xml 文件类似于:

      <Configuration xmlns="abc">
           ...
          <connectionTimeoutInMs>240000</connectionTimeoutInMs>
          <socketTimeoutInMs>240000</socketTimeoutInMs>
          <persist>false</persist>
          <internal>false</internal>
          ...
      </Configuration>
      

      并且放置其中许多是无效的,因此结果 xml 必须是这样的;

      <AllConfigurations xmlns="abc">
          ...
          <Configuration xmlns="abc">
             ...
          </Configuration >    
          ...
      <AllConfigurations xmlns="abc">
      

      所以必须将第一行和最后一行添加到结果中。

      这是我想出的解决方案;

                  <plugin>
                  <artifactId>maven-antrun-plugin</artifactId>
                  <version>1.8</version>
                  <executions>
                      <execution>
                          <id>default-cli</id>
                          <phase>generate-resources</phase>
                          <goals>
                              <goal>run</goal>
                          </goals>
                          <!--<phase>process-resources</phase>-->
                          <!--<phase>compile</phase>-->
                          <configuration>
                              <target>
                                  <concat destfile="${project.basedir}/.../allConfigurations.xml"
                                          force="yes">
                                      <fileset dir="${project.basedir}/...">
                                          <include name="xmlHeaderForConfiguration"></include>
                                      </fileset>
                                      <fileset dir="${project.basedir}/...">
                                          <include name="**/configuration.xml"></include>
                                      </fileset>
                                      <fileset dir="${project.basedir}/...">
                                          <include name="xmlFooterForConfiguration"></include>
                                      </fileset>
                                  </concat>
      
                              </target>
                          </configuration>
                      </execution>
                  </executions>
              </plugin>
      

      其中 xmlHeaderForConfiguration 是一个包含内容的文件; &lt;AllConfigurations xmlns="abc"&gt; 和 xmlHeaderForConfiguration 有 &lt;/AllConfigurations&gt;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-14
        • 2021-01-13
        相关资源
        最近更新 更多