Search code examples
mavenmaven-pluginmaven-assembly-plugin

Merge two maven modules in one jar and have third module generate jar at specified location


I have been trying this now for 5 hours and not sure what am I missing. I have following

+- parent
   pom.xml
   +- core-module
      pom.xml
   +- excel-module
      pom.xml
   +- client-module
      pom.xml
   +- assembly-module
      pom.xml
  1. I want to create one core.jar file for core-module and excel-module ( which I have achieved already) assembly-module/target/dist/server/core.jar
  2. I want to create separate client.jar file at assembly-module/target/dist/client/client.jar

Following are my pom files.

core/pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.test.project</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0.0-SNAPSHOT</version>
</parent>
<artifactId>core</artifactId>
<dependencies>
    <dependency>
        <groupId>com.test.project</groupId>
        <artifactId>excel</artifactId>
        <version>${project.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
</project>

excel/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>parent</artifactId>
    <groupId>com.test.project</groupId>
    <version>1.0.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>excel</artifactId>
<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>
    </dependency>
</dependencies>
</project>

client/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>parent</artifactId>
    <groupId>com.test.project</groupId>
    <version>1.0.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>client</artifactId>
<packaging>jar</packaging>
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Build-Version>${project.version}</Build-Version>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
      <dependency>
           <groupId>com.test.project</groupId>
           <artifactId>core</artifactId>
           <version>${project.version}</version>>
      </dependency>
</dependencies>

assembly/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>parent</artifactId>
    <groupId>com.test.project</groupId>
    <version>1.0.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>assembly</artifactId>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly-    descriptor.xml</descriptor>
                        </descriptors>
                        <appendAssemblyId>false</appendAssemblyId>
                         <outputDirectory>${project.basedir}/target/dist/server/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>core</finalName>
</build>
<dependencies>
    <dependency>
        <groupId>com.test.project</groupId>
        <artifactId>excel</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>com.test.project</groupId>
        <artifactId>core</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

assembly/descriptor.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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>all-jar</id>
<formats>
    <format>jar</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
    <dependencySet>
        <unpack>true</unpack>
        <useTransitiveDependencies>false</useTransitiveDependencies>
    </dependencySet>
</dependencySets>
</assembly>

in what way I need to update assembly.xml and/or descriptor.xml to achieve 2nd point. I have look almost all related posts here on SO

Any help is greatly appreciated.

EDIT

<build>
<plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
            <execution>
                <id>core-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/core-assembly-descriptor.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>${project.basedir}/target/dist/framework/lib/server/</outputDirectory>
                    <finalName>core.jar</finalName>
                </configuration>
            </execution>
            <execution>
                <id>client-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/client-descriptor.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>${project.basedir}/target/dist/framework/runtime/</outputDirectory>
                    <finalName>client.jar</finalName>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>


Solution

  • I would actually try to add another execution tag in the assembly/pom.xml as below:

    <execution>
                    <id>assembly-client</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/client-assembly-descriptor.xml</descriptor>
                        </descriptors>
                        <appendAssemblyId>false</appendAssemblyId>
                         <outputDirectory>${project.basedir}/target/dist/client/</outputDirectory>
                    </configuration>
                </execution> 
    

    And add a new assembly descriptor file (client-assembly-descriptor.xml) as below:

    <assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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>all-jar</id>
       <formats>
    <format>jar</format>
        </formats>
          <includeBaseDirectory>false</includeBaseDirectory>
         <dependencySets>
    <dependencySet>
        <unpack>true</unpack>
        <useTransitiveDependencies>false</useTransitiveDependencies>
        <includes>
        <include>com.test.project:client</include>
        </includes>
    </dependencySet>
    

    You will need to add the client jar as a dependency too in the assembly project.