Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how in maven multiple folders to the multiple zip archives with the same names

Tags:

maven

I want to collect the project module using, Maven I have a directory structure. Folders can be added dirN:

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

I tried the 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>

DESIRED OUTCOME Each directory to be packaged into a single zip archive folder with the same name, that is.

project-module
 |
 |-targer
   |-dir1.zip
   |-dir2.zip
   |-dir3.zip
   |-...
   |-dirN.zip

help me please.

like image 887
hagerds Avatar asked Oct 30 '22 12:10

hagerds


1 Answers

The iterator-maven-plugin could be used to work with a single assembly descriptor:

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

Then reference ${item} in the 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>

This results in target/dir1.zip etc.

Instead of listing the <items> explicitly, it can also iterate over a <folder>, though I don't see a way that could be restricted to such as dir*.

(Edit start)

E.g. to iterate the (immediate) subdirectories of parentDir:

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

The assembly descriptor (bin.xml) can then reference something like <directory>${project.basedir}/parentDir/${item}</directory>, much as before.

The lack of a filter would still be an issue if iterating over <folder>${project.basedir}</folder> though - this would zip up the target directory too.

like image 182
df778899 Avatar answered Nov 15 '22 07:11

df778899