Search code examples
mavenmaven-3maven-assembly-plugin

Create multiple zips using maven


I have multi module Project and each module has different configuration folders. Now I want to create zip files based configuration folder using maven and assembly.

Project
   |
   ---module 1
   |   |
   |    ---dir 1
   |   |
   |    ---dir 2
   |   |
   |    ---dir3 
   |   |
   |    ---src
   |
   ---module 2
       |
        ---dir 1
       |
        ---dir 2 
       |
        ---src     

My requirement is to create zip files based on directory names. Project has more than 50 modules and each module has different directory names.

Please suggest.

adding child pom.xml

<build>
    <plugins>
         <plugin>
            <groupId>com.soebes.maven.plugins</groupId>
            <artifactId>iterator-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>collective-package</id>
                    <configuration>
                        <items>
                            <item>dir 1</item>
                            <item>dir 2</item>
                            <item>dir 3</item>
                        </items>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <testSourceDirectory>test-integration-src</testSourceDirectory>
</build>

parent pom.xml

 <plugin>
                <groupId>com.soebes.maven.plugins</groupId>
                <artifactId>iterator-maven-plugin</artifactId>
                <version>0.5.0</version>
                <executions>
                    <execution>
                        <id>collective-package</id>
                        <phase>package</phase>
                        <goals>
                            <goal>iterator</goal>
                        </goals>
                        <configuration>
                            <pluginExecutors>
                                <pluginExecutor>
                                    <plugin>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-assembly-plugin</artifactId>
                                        <version>3.1.0</version>
                                    </plugin>
                                    <configuration>
                                        <descriptors>
                                            <descriptor>assembly/zip_assembly.xml</descriptor>
                                        </descriptors>
                                        <finalName>@item@</finalName>

                                    </configuration>
                                    <goal>single</goal>
                                </pluginExecutor>
                            </pluginExecutors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

zip_assembly.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>${project.version}</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
    <format>tar.gz</format>
</formats>
<moduleSets>
    <moduleSet>
        <useAllReactorProjects>true</useAllReactorProjects>
        <includes>
            <include>com.test:*</include>
        </includes>
        <sources>
            <excludeSubModuleDirectories>false</excludeSubModuleDirectories>
            <fileSets>
                <fileSet>
                    <directory>${project.basedir}/target/classes</directory>
                    <outputDirectory>conf</outputDirectory>
                    <includes>
                        <include>*</include>
                    </includes>
                    <excludes>
                        <exclude>com</exclude>
                        <exclude>schema</exclude>
                    </excludes>
                </fileSet>
                <fileSet>
                    <directory>${project.basedir}/../modulex/conf</directory>
                    <outputDirectory>conf</outputDirectory>
                    <includes>
                        <include>*</include>
                    </includes>
                </fileSet>
                <fileSet>
                    <directory>${project.basedir}/../modulex/scripts</directory>
                    <outputDirectory>/</outputDirectory>
                    <includes>
                        <include>*.sh</include>
                    </includes>
                </fileSet>
                <fileSet>
                    <directory>${project.basedir}/conf/<need here @item@></directory> <!--need directory name from parent/child pom.xml . sothat it picks the files from dir x-->
                    <outputDirectory>conf</outputDirectory>
                    <includes>
                        <include>*</include>
                    </includes>
                </fileSet>
                <fileSet>
                    <directory>${project.basedir}/../core/scripts</directory>
                    <outputDirectory>/</outputDirectory>
                    <includes>
                        <include>*.sh</include>
                    </includes>
                </fileSet>
                <fileSet>
                    <directory>${project.basedir}/target</directory>
                    <outputDirectory>lib</outputDirectory>
                    <includes>
                        <include>*.jar</include>
                    </includes>
                    <excludes>
                        <exclude>*-tests.jar</exclude>
                    </excludes>
                </fileSet>
             </fileSets>
        </sources>
    </moduleSet>
</moduleSets>

I can create zip for each directory but can't include files under dir x into zip. because I cant get dir name at runtime in zip_assembly.xml

how can we pass value of @item@ to zip_assembly.xml files


Solution

  • Found solution. Used maven-antrun-plugin instead of maven-assembly-plugin