Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot throws ClassNotFoundException with maven dependency for another project

I have Spring Boot project with simple EnvironmentPostProcessor implementation:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;

public class DevProfilerResolverEnvironmentPostProcessor implements EnvironmentPostProcessor {

        @Override
        public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
            if (configurableEnvironment.getActiveProfiles().length == 0) {
                if (System.getenv().get("OS").contains("Windows")) {
                    configurableEnvironment.addActiveProfile("DEV");
                }
            }
        }
    }

Also, I registered this class to sprig.factories:

org.springframework.boot.env.EnvironmentPostProcessor = com.example.demo.DevProfilerResolverEnvironmentPostProcessor

Now structure looks like:

enter image description here

Snippet from pom file:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

I executed with Maven:

mvn install

Now I want to use this EnvironmentPostProcessor implementation on another spring boot project. Thus I added it to dependency section for the new project:

    <dependency>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

And I wrote simple service usage:

@Service
@Profile("DEV")
public class DeveloperService {

    @Scheduled(cron = "1/1 * * * * ?")
    public void doWork() {
        System.out.println("Developers.... ");
    }
}

and enabled scheduling for main class:

@SpringBootApplication
@EnableScheduling
public class LvivBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(LvivBootApplication.class, args);
    }
}

However, I got following exception after main execution:

14:56:09.822 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed
java.lang.IllegalArgumentException: Unable to instantiate factory class: org.springframework.boot.env.EnvironmentPostProcessor
Caused by: java.lang.ClassNotFoundException: com.example.demo.DevProfilerResolverEnvironmentPostProcessor
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

and dependency is added to the new project:

enter image description here

I am running it exactly on Windows environment.

The idea is when OS is Windows add DEV profile for the first project. Second has service which prints to console dummy info when the profile is DEV and scheduled this printing for every second.

I can't find what is missed at this example?

SOLUTION:

For making from first project library jar pom should be fixed like following:

<!--<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>--> 

Or simple to eliminate this plugin from pom file.

like image 549
catch23 Avatar asked Mar 08 '23 18:03

catch23


1 Answers

Your first jar is not a library jar. It's a repackaged boot jar, created by the spring boot plugin, containing the first spring boot application and all its dependencies, intended to be executed, and not to be used as a library.

If you want to use that as a library, you need to use the non-repackaged jar file, containing only the classes and resources of the project.

It's quite bizarre to have an application depend on another application, though. You should create a library project, only containing common common classes and resources, and use that as a dependency to your two spring boot applications.

like image 75
JB Nizet Avatar answered Mar 11 '23 06:03

JB Nizet