简体   繁体   中英

How to zip only specific directories using maven assembly plugin

I am using maven-assembly-plugin to create a zip file of the project when building the project. It is successful. Now what I want is to zip only specific directories in the project.

This is the code for plugin in my pom which I used previously.

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make shared resources</id>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <descriptors>
                                <descriptor>resource.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

And the resource.xml file is as follows.

<assembly>
    <id>resources</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <!-- Since it bundles all the units -->
            <directory></directory>
            <outputDirectory>/project</outputDirectory>
            <excludes>
                <exclude>pom.xml</exclude>
                <exclude>*.iml</exclude>
                <exclude>*.jar</exclude>
                <exclude>**/src/**</exclude>
                <exclude>**/target/**</exclude>
                <exclude>resource.xml</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

I want to zip only some directories of the project in the maven build.

As an example I have following folder structure in the project.

project

   |_directories

         |_dir1
             |_ ....
         |_dir2
             |_ ....
         |_dir3
            |_ ....
         |_pom.xml

What I want is make zip file which only include the directories folder. When extracting my zip file that should only contain the four directories in it.

How can I achieve this? Does the maven-assembly-plugin is enough to do this or should I create a mojo?

You misused directory and outputDirectory .

directory is the path in your project where the files to be zipped are taken from (so it should be directories in your case), outputDirectory the folder inside the generate zip file, where the fileset is place (since you do not want the toplevel directory, it should be / ):

<assembly>
    <id>resources</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <!-- Take everything inside the directories folder -->
            <directory>directories</directory>
            <!-- And place it inside the root of the zip file -->
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM