0

I am having a problem with spring boot multimodule jar deployment

I have 3 components like below.

Parent
   --Child1
   --Child2

Child2 has dependency of child1

But when i take jar child1 classes are not present in the jar. I am getting java.lang.classnotfoundexception

Below my

A pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
</parent>

<groupId>com.A</groupId>
    <artifactId>A</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <type>pom</type>
                <version>${spring.boot.version}</version>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>com.A</groupId>
                <artifactId>A</artifactId>
                <version>0.0.1</version>
            </dependency>
            <dependency>
                <groupId>com.B</groupId>
                <artifactId>B</artifactId>
                <version>0.0.1</version>
            </dependency>
            </dependencies>
        </dependencyManagement>
            <build>
    <finalName>service</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

    </build>

B pom.xml

<groupId>com.B</groupId>
    <artifactId>B</artifactId>
    <version>0.0.1</version>
    <name>Omni Channel Core</name>
    <description>Omni Channel Core Functions</description>
    <parent>
        <groupId>A</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1</version>
    </parent>
    <packaging>jar</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

C pom.xml

<groupId>com.C</groupId>
    <artifactId>C</artifactId>
    <version>0.0.1</version>
    <parent>
        <groupId>A</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1</version>
    </parent>
    <packaging>jar</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
                <groupId>com.B</groupId>
                <artifactId>B</artifactId>
        </dependency>
    </dependencies>

    <build>
    <finalName>service</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

    </build>

Whenever I take C's jar inside that dependent jar B is not exists.

How to resolve this issue any help will be greatly appreciated!!!

Selva
  • 1,620
  • 3
  • 33
  • 63

1 Answers1

3

The configuration of a maven multi-module spring-boot project is straightforward. check this sample that I have created on the basis of your question.

I have the following three modules:

multi-modules
   --module-B
   --module-C

So my multi-modules is basically the equivalent of your module A

Configuration of the parent pom

My parent's pom configuration is very similar to yours, except that I am also configuring a section in which I declare the list of all children modules of this parent. We might not be able to do that always, but in this particular case, it is possible since I am creating the parent and the children modules in the same project. One small benefit of this is the fact that I can build all children modules sequentially from the parent by running (at the parent's level) such a command as mvn clean install

<modules>
   <module>module-B</module>
   <module>module-C</module>
</modules>

Configuration of children modules

To simplify thing we can make the logical structure of the multi-module project matches it physical structure, that is, to have a parent's pom directory that contains two same level sub-directories, one for each module.

matching physical structure of multi-module project

This way we do not have to go through any ugly relative path configuration.

module-B

No thing particular about its configuration. I have added a dummy ServiceB class to have some minimalistic content inside of it.

module-C

Its configuration is similar to that of module-B except that it depends on module-B and of course I am taking advantage of the dependency management's configuration done in the parent to define the dependency to module-B using only groupId and artifactId.

Building and checking

At this point we can run an mvn clean install at the parent's level and go check that the module-B dependency makes its way into module-C

enter image description here

We can see that in the list of module-C libraries here:

enter image description here

Hope this helps.

alainlompo
  • 4,414
  • 4
  • 32
  • 41
  • thanks for your help, I have changed my code like the sample that you shared. Its running in eclipse properly. But when I take jar and run its still showing Classnotfoundexceptipon error – Selva Mar 14 '20 at 06:37
  • I am able to see my dependent module library in my boot-inf/ lib folder. – Selva Mar 14 '20 at 06:49
  • @Madhesh I can run the module-c jar using ```java -jar ./target/module-C-1.0-SNAPSHOT.jar``` (it will display a classic hello world). What command are you running that is giving you a ClassNotFoundException? – alainlompo Mar 14 '20 at 10:07
  • after adding repackage property in spring boot maven plugin I am able to run it. Thanks for your guidance to solve this issue – Selva Mar 14 '20 at 18:43