简体   繁体   English

如何使用 Maven 程序集插件仅压缩特定目录

[英]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.我在构建项目时使用 maven-assembly-plugin 创建项目的 zip 文件。 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.这是我以前使用的pom中插件的代码。

 <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.而resource.xml 文件如下。

<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.我只想在 maven 构建中压缩项目的一些目录。

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.我想要的是制作仅包含目录文件夹的 zip 文件。 When extracting my zip file that should only contain the four directories in it.解压缩我的 zip 文件时,它应该只包含其中的四个目录。

How can I achieve this?我怎样才能做到这一点? Does the maven-assembly-plugin is enough to do this or should I create a mojo? maven-assembly-plugin 是否足以做到这一点,还是我应该创建一个 mojo?

You misused directory and outputDirectory .您误用了directoryoutputDirectory

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 / ): directory是项目中要压缩的文件所在的路径(因此在您的情况下应该是directories ), outputDirectory是生成 zip 文件内的文件夹,文件集所在的位置(因为您不想要顶级目录, 应该是/ ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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