Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create JAR file as Aggregation in maven multi module package

Tags:

maven

maven-3

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 .

like image 205
vaquar khan Avatar asked Jun 21 '18 19:06

vaquar khan


People also ask

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.

Can a Maven project have multiple pom files?

Yes you can use Maven Profiles to manage this. Obviously you can tweak this approach to suit your needs however works best.


1 Answers

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

like image 154
Noman Khan Avatar answered Sep 20 '22 13:09

Noman Khan