Maven assembly plugin - Multi module project - Common folder for dependencies

42 Views Asked by At

I have a multi module maven project and use the maven assembly plugin to create a tar.gz archive of my project. Each module has the following pom.xml and assembly.xml structure:

pom.xml

 ...
 <plugin>
     <artifactId>maven-assembly-plugin</artifactId>
     <version>${maven-assembly-plugin-version}</version>
     <configuration>
         <finalName>${project.artifactId}-${project.version}</finalName>
         <appendAssemblyId>false</appendAssemblyId>
         <skipAssembly>false</skipAssembly>
         <descriptors>
             <descriptor>assembly/package.xml</descriptor>
         </descriptors>
     </configuration>
     <executions>
         <execution>
             <id>assembly</id>
             <phase>package</phase>
             <goals>
                 <goal>single</goal>
             </goals>
         </execution>
     </executions>
 </plugin>
 ...

assembly.xml

...
<formats>
    <format>tar.gz</format>
</formats>
...

<dependencySets>
    <dependencySet>
        <useProjectArtifact>false</useProjectArtifact>
        <outputDirectory>${module1Path}/lib</outputDirectory>
        <unpack>false</unpack>
    </dependencySet>
</dependencySets>

<fileSets>
    <fileSet>
        <directory>${project.build.directory}</directory>
        <outputDirectory>${module1Path}</outputDirectory>
        <includes>
            <include>*.jar</include>
        </includes>
    </fileSet>
    ...
</fileSets>
...

To create the main package there is a distribution module with the following structure:

pom.xml

  ...
  <artifactId>myproject</artifactId>
  <packaging>pom</packaging>

  <name>myproject</name>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module1</artifactId>
      <version>${project.version}</version>
      <type>tar.gz</type>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module2</artifactId>
      <version>${project.version}</version>
      <type>tar.gz</type>
    </dependency>
    ...
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>${maven-assembly-plugin-version}</version>
        <executions>
          <execution>
            <id>make-bundles</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>package.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>

assembly.xml

    ...
    <formats>
        <format>tar.gz</format>
    </formats>
    ...
    <dependencySets>
        <dependencySet>
            <useTransitiveDependencies>false</useTransitiveDependencies>
            <outputFileNameMapping>${artifact.artifactId}-${artifact.version}.${artifact.extension}</outputFileNameMapping>
            <unpack>false</unpack>
      </dependencySet>
   </dependencySets>
    ...

The output is a project.tar.gz file which contains module1.tar.gz and module2.tar.gz. If I extract it I have the following directory structure:

+ module1
+-- module1.jar
+---- libs
+---- conf
...
+ module2
+-- module2.jar
+---- libs
+---- conf
...

In my original project there are even more modules. As every module contains its own dependencies there are a lot of duplicated jars in the package. I want to create a shared folder for the libs so that I finally get the following directory structure:

+ module1
+-- module1.jar
+ module2
+-- module2.jar
- libs
- conf

The lib folder should contain all jar files together as in module1/libs and module2/libs before.

So I removed the type .tar.gz from the dependencies:

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module1</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module2</artifactId>
      <version>${project.version}</version>
    </dependency>
   </dependencies>

And added a moduleset to create a folder with the dependencies of all modules:

true opt/bin false true ${artifact.artifactId}.${artifact.extension} opt/bin false false

I was expecting to see all required jar files for both projects in the folder. But this is not the case. There is a problem with trasitive dependencies. Here one example:

module1 has a dependency to guice-assistedinject version 4.2.2 which has a trasitive dependency to guice version 4.2.2. module2 has a direct dependency to guice version 5.0.1.

Depending on the order of the projects I either have only guice 4.2.2 or guice 5.0.1. But to start module1 I need guice 4.2.2 in the libs - To start module2 I need guice 5.0.1

Is there a way to put all jars in the shared folder with the maven assembly plugin?

0

There are 0 best solutions below