1

I have a project which is using a maven assembly plugin to include a bunch of files into a 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.

I tried doing something like this: maven-assembly plugin - how to create nested assemblies

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.

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:

<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

<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

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

Community
  • 1
  • 1
Gadam
  • 2,674
  • 8
  • 37
  • 56
  • Change the `` tag to `${project.basedir}/target/[name of zip file].zip`. You can't have `*` inside `` and the zip built by the first execution will generate it inside `target`. – Tunaki Aug 21 '15 at 17:36
  • Thanks that worked. Can I change the name of the zip file being built in the first execution? I tried does not work. Can i give the of the first execution as '${project.basedir}/src/main/....' or does it only have to be inside target? Also is there an efficient way to handle/delete the intermediary zip file created in the target ? – Gadam Aug 21 '15 at 18:08

1 Answers1

3

The <source> tag does not support *: it is only possible to select a single file with it. 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.

By default, the zip file will be named ${project.build.finalName}-zip-id.zip. You can change this name by setting the finalName attribute in the plugin configuration. Note that, by default, the assembly id will be concatenated to the finalName. You can turn this off by switching the appendAssemblyId flag to 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:

<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. This is the working folder of Maven and you should not worry about keeping intermediate files in there.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • I used but the name of the zip file comes out as myzip-zip-id.zip. Am I missing a setting to stop maven from appending the descriptor id to the end of the file name? – Gadam Aug 21 '15 at 18:39
  • I need to create another zip file the same way. Do I have to create another execution for it, or is there a way to leverage the existing zip execution? – Gadam Aug 21 '15 at 18:42
  • @Gadam I edited my post to include the `appendAssemblyId` flag. You should create an execution per generated file (assembly). – Tunaki Aug 21 '15 at 18:44
  • Thanks @Tunaki, for the detailed and spot on answer! – Gadam Aug 22 '15 at 18:45