11

I have following architecture

enter image description here

Now if i am creating parent child relationship and building child first and parent end it will working fine

<packaging>jar</packaging>

Requirements :

I need packaging with following features :

Command run on parent project "A" - mvn clean install package etc First create Jar "B" ,"C","D" then create Jar "A" then add "B","C","D" jar inside Jar A

When i am adding modules

  <modules>
        <module>../B</module>
         <module>../C</module>
         <module>../D</module>
    </modules> 

then maven force to add

<packaging>pom</packaging>

insted of

<packaging>jar</packaging>

Issue :

When i am adding packaging pom so jar "A" is not crearing

SO i have tried to create one super pom

enter image description here

POM Super :

   <packaging>pom</packaging>

    <modules>
        <module>../A</module>

    </modules> 

POM A:

   <parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>Super</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> 
    </parent>

<packaging>pom</packaging>

 <modules>
        <module>../B</module>
         <module>../C</module>
         <module>../D</module>
    </modules> 


<dependencies>

        <!-- B -->
         <dependency>
            <groupId>com.khan.vaquar</groupId>
            <artifactId>B</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency> 
        <!-- C -->
         <dependency>
            <groupId>com.khan.vaquar</groupId>
            <artifactId>C</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency> 
        <!-- D-->
        <dependency>
            <groupId>com.khan.vaquar</groupId>
            <artifactId>D</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency> 
        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

POM B:

<packaging>jar</packaging>
   <parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> 
    </parent>

POM C:

<packaging>jar</packaging>
   <parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

POM D:

<packaging>jar</packaging>

<parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/>
    </parent>

Problem : maven not allow to add jar if we are adding module , so how can i add child jar into parent jar and create build .

vaquar khan
  • 10,864
  • 5
  • 72
  • 96
  • What is the exact error? I think in that scenario you need to specify the relativePath to the parent for the reactor build to work. – DrHopfen Jun 22 '18 at 05:23
  • Jar A (A contain cihild jar B,C,D) not creating using pom and maven not allowed jar if we add any module , – vaquar khan Jun 22 '18 at 06:07
  • looking workaround solution how to do that – vaquar khan Jun 22 '18 at 06:07
  • Why don't you use a seperate reactor to build the dependencies (B, C, D)? Alternatively I think you could use the super pom as a reactor and add all 4 artifacts (A, B, C, D) after changing A to packaging jar – DrHopfen Jun 22 '18 at 07:10
  • B,C,D independent jars but A jar contain its owncode and B,C,D inside of A. independent reactor will create 4 jars independently .A,B,C,D not A( B,C,D,) – vaquar khan Jun 22 '18 at 11:05
  • What do you mean by contains? So A is a fat jar (uber jar) that contains the classes of the other three? Or do you literally want to include the jars B,C,D in A? – DrHopfen Jun 22 '18 at 13:19
  • Yes A is fat jar, B,C,D are microservices and saving cost on cloud aggregating all while deploy so add all jars into jar A, and it's also contain spring boot dependency – vaquar khan Jun 22 '18 at 13:31

1 Answers1

16

To create fat jar from multiple modules you can use maven-shade-plugin in A project as

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <id>create-fat-jar</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                <!-- add Main-Class to manifest file -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.nk.test.Application</mainClass>
                    </transformer>
                </transformers>
                    <finalName>A</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>

For your example you can create structure as

parent module

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nk.test</groupId>
  <artifactId>P</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
   <module>../C</module>
   <module>../B</module>
   <module>../A</module>
  </modules>

</project>

Core project A with maven-shade-plugin and project B & C as dependencies

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nk.test</groupId>
  <artifactId>A</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
   <dependency>
   <artifactId>B</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <groupId>com.nk.test</groupId>
   </dependency>
     <dependency>
   <artifactId>C</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <groupId>com.nk.test</groupId>
   </dependency>
  </dependencies>

  <build>
   <plugins>
   **<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <id>create-fat-jar</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                <!-- add Main-Class to manifest file -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.nk.test.Application</mainClass>
                    </transformer>
                </transformers>
                    <finalName>A</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>**
   </plugins>
  </build>
</project>

Dependency module B

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nk.test</groupId>
  <artifactId>B</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

Dependenc Module C

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nk.test</groupId>
  <artifactId>C</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

run maven install on parent project.

This will result in Fat jar named named A.jar with B and C as dependencies inside it.

you can find example at : https://github.com/nomanbplmp/maven-multimodule-fat-jar

Noman Khan
  • 920
  • 5
  • 12