maven assembly, avoiding full path in zip file?

Alexandre Thenorio picture Alexandre Thenorio · Sep 12, 2013 · Viewed 9.7k times · Source

I have a multi-module project which contains 2 modules (each with its own pom.xml) and a parent pom.xml pointing to those modules.

When I run "mvn clean package" on the parent pom, each project ends up with a zip file under it's own target folder. I would like to package a zip file containing each module zip file under the zip file's root folder but I am having some issues doing so.

I have created an assembly file in the parent pom.xml folder:

<!-- Release distribution -->
<assembly>
<id>release</id>
<formats>
    <format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>

<fileSets>
    <fileSet>
        <directory>${basedir}/projectA/target/</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>*.tar.gz</include>
        </includes>
    </fileSet>
    <fileSet>
        <directory>${basedir}/projectB/target/</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>*.tar.gz</include>
        </includes>
    </fileSet>
</fileSets>
</assembly>

While the above works, if we start adding more and more modules to this project, it gets very annoying to have to keep updating this assembly.

Ideally I'd like for it to automatically just go into every target folder for a module and get a zip file in there.

This can be accomplished by doing

...
<fileSet>
        <directory>${basedir}</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>**/target/*.tar.gz</include>
        </includes>
    </fileSet>

however the issue here is the zip file will contain full paths, so rather than having zip files inside the root folder, the zip file will have projectA/target/xxxx.zip and projectB/target/xxxx.zip which is exactly what I do not want.

Is there any way to make a simple assembly in maven so I don't have to update it everytime I add a new module and not have full paths inside the zip?

EDIT 1

It looks like this is simply not possible. Either you get a nicely structured zip file with a hard to maintain assembly or you get an easy to maintain but annoingly structured zip file. I'll leave this unanswered until I can find a proper solution

EDIT 2

Back at looking for a solution for this again. Regarding khmarbaise solution posted below there are a few issues with it: - It relies on assemblies of dependencies, in my case I just want an assembly of assemblies (Which are more like fileSets and not dependencyset) - The fact that it relies on me manually specifying which projects I want to have included in the assembly. I already have this information in the parent pom which specifies which modules should be built, so if I remove a module from the parent pom so that it is no longer built, the assembly should already know that and skip picking that project (Reason for which fileset seems to work so well except for the shitty, non-controllable path inside the assembly zip). I shouldnt have to manually mess with what modules I want included other than simply removing adding modules from the pom I am running the assembly from.

Has nobody really ever run into a similar problem?

Answer

khmarbaise picture khmarbaise · Sep 15, 2013

First i would suggest to create dist-packaging module which contains the resulting package. Furthermore it sounds like your assembly-descriptor for creating the final archive which contains the zip files of others is wrong.

  <id>proj1-assembly</id>
  <formats>
      <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
      <dependencySet>
          <outputDirectory>/</outputDirectory>
          <useProjectArtifact>false</useProjectArtifact>
          <unpack>false</unpack>
          <scope>runtime</scope>
      </dependencySet>
  </dependencySets>

But you must be aware that you have to maintain the dependencies in the dist-module as usual module dependencies like the following:

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>package-one</artifactId>
      <version>${project.version}</version>
      <classifier>package-1-assembly</classifier>
      <type>zip</type>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>package-two</artifactId>
      <version>${project.version}</version>
      <classifier>package-2-assembly</classifier>
      <type>zip</type>
    </dependency>
    ....

Here you can find a full working example.