简体   繁体   English

如何使用Maven组件插件在tar中创建和包含zip文件

[英]How to create and include a zip file inside a tar using maven assembly plugin

I have a project which is using a maven assembly plugin to include a bunch of files into a tar. 我有一个项目正在使用Maven程序集插件将一堆文件包含到tar中。 Now I want to include a few more files in that tar, but first I want to zip them up and actually include that zip file. 现在,我想在该tar中包含更多文件,但是首先,我想将它们压缩并实际包含该zip文件。

I tried doing something like this: maven-assembly plugin - how to create nested assemblies 我试图做这样的事情: maven-assembly插件-如何创建嵌套程序集

So basically created 2 executions inside the maven assempbly plugin and have the first execution create the required zip file that the second execution can package into a tar. 因此,基本上在maven assempbly插件中创建了2个执行,并让第一个执行创建了所需的zip文件,第二个执行可以将其打包到tar中。

But I get the error saying the zip file 'is not a file'. 但是我收到错误消息,说压缩文件“不是文件”。

What is wrong with what I am doing? 我在做什么错了?
Is there a more efficient way to do this? 有没有更有效的方法可以做到这一点?

This is what I tried: 这是我尝试的:
My POM: 我的POM:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
                <executions>
                    <execution>
                        <id>create-zip</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>zip.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                    <execution>
                        <id>create-tar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                        <descriptors>
                            <descriptor>tar.xml</descriptor>d
                        </descriptors>
                </configuration>
                </execution>
            </executions> 
        </plugin>
    </plugins>
</build>

zip.xml zip.xml

<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>zip-id</id>
<formats>
    <format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
    <fileSet>
      <directory>${project.basedir}/src/main/resources/vertica_schema/schema_migrations</directory>
     <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>

tar.xml tar.xml

<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://maven.apache.org/plugins/maven-assembly-  plugin/assembly/1.1.2"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>tar-id</id>
<formats>
    <format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files> 
    <file>
        <source>/*.zip</source>
        <outputDirectory>/lib</outputDirectory>
        <destName>schema_migrations.zip</destName>
    </file>
</files> 

The <source> tag does not support * : it is only possible to select a single file with it. <source>标记不支持* :只能选择一个文件。 Additionally, the zip file build by the first execution will be generated inside the target folder by default , so you should point to that directory. 此外, 默认情况下 ,将在target文件夹内生成第一次执行时生成的zip文件,因此您应指向该目录。

By default, the zip file will be named ${project.build.finalName}-zip-id.zip . 默认情况下,该zip文件将命名为${project.build.finalName}-zip-id.zip You can change this name by setting the finalName attribute in the plugin configuration. 您可以通过在插件配置中设置finalName属性来更改此名称。 Note that, by default, the assembly id will be concatenated to the finalName . 请注意,默认情况下,程序集ID将连接到finalName You can turn this off by switching the appendAssemblyId flag to false . 您可以通过将appendAssemblyId标志切换为false来关闭此功能。

This is a working configuration: 这是一个有效的配置:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
                <executions>
                    <execution>
                        <id>create-zip</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>zip.xml</descriptor>
                            </descriptors>
                            <finalName>myzip</finalName> <!-- Name of zip file -->
                            <appendAssemblyId>false</appendAssemblyId> <!-- Do not concatenate "zip-id" to the finalName" -->
                        </configuration>
                    </execution>
                    <execution>
                        <id>create-tar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                        <descriptors>
                            <descriptor>tar.xml</descriptor>d
                        </descriptors>
                </configuration>
                </execution>
            </executions> 
        </plugin>
    </plugins>
</build>

with the following tar.xml : 使用以下tar.xml

<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://maven.apache.org/plugins/maven-assembly-  plugin/assembly/1.1.2"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>tar-id</id>
<formats>
    <format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files> 
    <file>
        <source>${project.basedir}/target/myzip.zip</source>
        <outputDirectory>/lib</outputDirectory>
        <destName>schema_migrations.zip</destName>
    </file>
</files>

It is a good practice to keep everything that is generated by Maven under the target directory. 最好将Maven生成的所有内容都保留在target目录下。 This is the working folder of Maven and you should not worry about keeping intermediate files in there. 这是Maven的工作文件夹,您不必担心将中间文件保存在其中。

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

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