maven assembly, avoiding full path in zip file?

10,328

Solution 1

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.

Solution 2

I just ran into the same problem: My resulting zip file contained the full system path! My original configuration looked like that:

<fileSets>
        <fileSet>
            <directory>${project.basedir}/src/main/resources</directory>
            <outputDirectory>${project.build.directory}</outputDirectory>
            <includes>
                <include>someFolder/somefile.txt</include>
            </includes>
        </fileSet>
</fileSets>

As the result my zip file contained full system path! After lots of investigating I found a working solution:

<fileSets>
        <fileSet>
            <outputDirectory>./</outputDirectory>
            <directory>src/main/resources</directory>
            <includes>
                <include>someFolder/somefile.txt</include>
            </includes>
        </fileSet>
</fileSets>

So when specifying the outputDirectory with ${project.build.directory} then for some strange reason the full path will be part of the jar/zip file.

Share:
10,328

Related videos on Youtube

Alexandre Thenorio
Author by

Alexandre Thenorio

Updated on September 19, 2022

Comments

  • Alexandre Thenorio
    Alexandre Thenorio over 1 year

    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?

  • Alexandre Thenorio
    Alexandre Thenorio over 10 years
    Thanks but if I still have to have a pom that keeps track of every single project then I might as well just keep a pom and main assembly on the project's main folder which is already working and saves me the trouble of having to create a distribution project (Though that sounds like it can be a nice way to keep things properly divided)
  • dann.dev
    dann.dev over 8 years
    <includeBaseDirectory>false</includeBaseDirectory> , exactly what I was looking for!