Spring Boot throws ClassNotFoundException with maven dependency for another project

I have a Spring Boot project with a simple implementation EnvironmentPostProcessor

:

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 in sprig.factories

:

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

      

The structure now looks like this:

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 have executed with Maven:

mvn install

Now I want to use this implementation EnvironmentPostProcessor

for another Spring Boot project. So I added it to the dependencies section for the new project:

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

      

And I wrote a 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, after main execution, I got the following exception:

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 the dependency is added to the new project:

enter image description here

I run it in Windows environment.

The idea is that the OS is a Windows add DEV

profile for the first project. Secondly, a service that prints on bogus console information when a profile is DEV

, and scheduling this print every second.

I can't find what I missed in this example?

DECISION:

To create projects from the first library, the jar pom

should be fixed as follows:

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

      

Or simply remove this plugin from the pom file.

+3


source to share


2 answers


Your first bank is not a library bank. This is a packaged boot jar, created by the spring boot plugin, containing the first spring boot application and all its dependencies, intended to be run, not used as a library.

If you want to use this as a library, you need to use an untangled jar file containing only the classes and project resources.



It is rather strange if the application depends on another application. You should create a library project containing only common shared classes and resources and use that as a dependency on two spring boot applications.

+6


source


I am writing because I had a similar error in similar circumstances - I was looking for a watch and the solution was very simple. I am using Eclipse for debugging - Eclipse does not use the generated jar from the Maven build - it uses its own set of build paths with, as far as I understood, the spaced classes, etc.

My Maven project that created the JAR that I included in my main POM project did not in any way declare any dependency on the main project for example. through a common parent or something else.



Eclipse doesn't seem to understand that one of the dependencies I was using in the POM was the result of another local project - somehow, although the (jar-with-dependencies) file was in the Maven cache fine, it didn't collect it to be copied to your aforementioned set of classpaths.

I needed to explicitly add it (my library project) to the main project via Project -> Properties -> Java Build Path -> Projects - adding it to the list called "Required Projects in Build Path:"

0


source







All Articles