Skip to content
Advertisement

Is it possible in main app to use dependancies from added dependancies?

The problem I have is that I have a Library which I want to use in the Main App. In the Library I have added external libraries, and the question is, is it possible to use directly code of this external libraries in Main App despite of that these libraries are not added directly to Main App but to my Library?

I’ve added my Library to pom.xml in Main App and I can use the code that is written there, but I have problem using Library classes that extends external library classes, because Spring throws ClassNotFoundException

I’ve tried @ComponentScan in Main App and as argument I passed the main package of Library (com.project.common.starter), but it helped only with Components of the Library, but no external ones

EDIT:
my Library 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/>
    </parent>
    <groupId>com.project.starter</groupId>
    <artifactId>project-commons-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project-commons-starter</name>
    <description>project-commons-starter</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.external.library</groupId>
            <artifactId>external-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
</project>

my Main App 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/>
    </parent>
    <groupId>com.project</groupId>
    <artifactId>project-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>main-app-name</name>
    <description>main-app-desc</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        ...
        <dependency>
            <groupId>com.project.starter</groupId>
            <artifactId>project-commons-starter</artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>
    ...
</project>

Class in Library that probably causes problem:

package com.project.starter.service;

import com.external.ExternalLibraryProcessor;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

@Component
public class LibraryProcessor implements ExternalLibraryProcessor {

    @Override
    public void process() {
       String context = MDC.get(ContextUtils.REQUEST_CONTEXT)      
       doSmth...
    }
}

my MainPortalApplication :

package com.project

@SpringBootApplication
@ComponentScan(basePackages = {"com.project","com.project.starter"})
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
       
    }
}

ContextUtils.class:

public class ContextUtils {

    private ContextUtils() {
    }
    public static final String REQUEST_CONTEXT = "requestContext";
}

When I run application I get:

Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanDefinitionStoreException: 
Failed to read candidate component class: URL [jar:file:/home/***/.m2/repository/com/project/project-commons-starter/0.0.1/project-commons-starter-0.0.1.jar!/com/project/starter/commons/ContextUtils.class];
nested exception is 
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.project.starter.service.LibraryProcessor] for bean with name 'libraryProcessor' defined in URL [jar:file:/home/***/.m2/repository/com/project/project-commons-starter/0.0.1/project-commons-starter-0.0.1.jar!/com/project/starter/service/LibraryProcessor.class]: problem with class file or dependent class; 
nested exception is java.lang.NoClassDefFoundError: com/external/library/ExternalLibraryProcessor

Advertisement

Answer

The issue in my case was that I was using wrong maven command to compile my Library:

mvn install:install-file [...]

When using above .pom file has no dependencies which where included in Library.

When changed to mvn install it started working well – .pom file started to look like it should and Main App recognized transient dependencies.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement