如何在maven多个文件夹中使用相同名称的多个zip存档

时间:2017-02-09 10:42:18

标签: maven

我想使用Maven收集项目模块 我有一个目录结构。文件夹可以添加dirN:

project-module
 |
 |-dir1
 |-dir2
 |-dir3
 |-...
 |-dirN
 |-bin.xml
 |-pom.xml

我尝试了maven-assembly-plugin

的pom.xml

<plugins>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <descriptor>bin.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>sql dir</id>
            <goals>
                <goal>single</goal>
            </goals>
            <phase>package</phase>
        </execution>
    </executions>
</plugin>
</plugins>

bin.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>../mspost-db</directory>
            <excludes>
                <exclude>*.*</exclude>
            </excludes>
            <useDefaultExcludes>false</useDefaultExcludes>
            <!--<outputDirectory>/</outputDirectory>-->
        </fileSet>
    </fileSets>
</assembly>

期望的结果 每个目录都要打包成一个同名的zip存档文件夹,即。

project-module
 |
 |-targer
   |-dir1.zip
   |-dir2.zip
   |-dir3.zip
   |-...
   |-dirN.zip
请帮助我。

1 个答案:

答案 0 :(得分:2)

iterator-maven-plugin可以用于单个程序集描述符:

<plugin>
  <groupId>com.soebes.maven.plugins</groupId>
  <artifactId>iterator-maven-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>iterator</goal>
      </goals>
      <configuration>
        <items>
          <item>dir1</item>
          <item>dir2</item>
          <item>dir3</item>
        </items>
        <pluginExecutors>
          <pluginExecutor>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-assembly-plugin</artifactId>
            </plugin>
            <goal>single</goal>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/bin.xml</descriptor>
              </descriptors>
              <finalName>${item}</finalName>
              <appendAssemblyId>false</appendAssemblyId>
            </configuration>
          </pluginExecutor>
        </pluginExecutors>
      </configuration>
    </execution>
  </executions>
</plugin>

然后在${item}中引用bin.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>${item}</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/${item}</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

这将导致target/dir1.zip等。

除了没有明确列出<items>之外,它还可以在<folder>上进行迭代,尽管我看不到dir*这样的方法。

(编辑开始)

例如迭代parentDir的(立即)子目录:

  <configuration>
    <folder>${project.basedir}/parentDir</folder>
    <pluginExecutors>
    ... as above ...

然后,汇编描述符(bin.xml)可以像以前一样引用<directory>${project.basedir}/parentDir/${item}</directory>之类的东西。

如果遍历<folder>${project.basedir}</folder>仍然缺少过滤器仍然是一个问题-这也会压缩target目录。