Sumit Ghosh
Sumit Ghosh

Reputation: 514

Custom dependencies are not identified in Spring-Boot

I have two spring-boot project

  1. greeter-library
  2. greeter-spring-boot-autoconfigure

I have created jar file for greeter-library and installed that in my local m2(maven) repository. Now I am using that jar as a maven dependency in greeter-spring-boot-autoconfigure.But it is stating Class not found on Greeter.java.

pom.xml for greeter-library

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


<artifactId>greeter-library</artifactId>
<groupId>com.xyz.greeter</groupId>
<version>0.0.1-SNAPSHOT</version>
<name>greeter-library</name>
<dependencies>
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

Now pom.xml for greeter-spring-boot-autoconfigure is as follows

<artifactId>greeter-spring-boot-autoconfigure</artifactId>
<name>greeter-spring-boot-autoconfigure</name>
<groupId>com.xyz</groupId>


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

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <artifactId>greeter-library</artifactId>
        <groupId>com.xyz.greeter</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

But during maven build time for greeter-spring-boot-autoconfigure, it is stating that Greeter.java not found which is part of greeter-library project.

Can anyone has any solution to this?

Upvotes: 1

Views: 1662

Answers (2)

Mark Bramnik
Mark Bramnik

Reputation: 42491

Since you've added spring-boot-maven-plugin in "greeter-library" module, it will be packaged as a spring boot application. Open it with WinRar/WinZip and you'll see. This is a little bit confusing, but in general spring boot application, although is packed as a JAR is not actually a jar in the sense that JVM can't load it, can't find its classes automatically, etc. For example, it has its dependencies in BOOT-INF/lib folder - this is not the way regular jars work, in fact, spring boot has a bootstrapping code that uses custom class loaders to read the classes from such a structure.

As a consequence of all this, Maven and IDE won't be able to recognize the classes from the greeter-library hence the error. Bootom line, you can't really declare a dependency on a spring boot application from your greater-spring-boot-autoconfigure module.

Now as a solution - why do you need a greeter-library to be a spring boot artifact? Maybe if you just remove the spring-boot-maven-plugin and turn it to the regular jar (with regular dependencies on spring boot infrastructure perhaps) it will work?

If this doesn't help, feel free to share more details in the question to get a more precise solution to the problem...

Upvotes: 3

vishnu g
vishnu g

Reputation: 109

Some ideas to explore

  1. the first jar "greeter-library" doesnt need to be packed for spring-boot, it can be a normal jar file.
  2. Create a spring.factories file under src/resources/META-INF path. Expose the java classes from this JAR that can be used by "greeter-spring-boot-autoconfigure"

Upvotes: 0

Related Questions