Package not found (Multi-module spring project)

You are (re-)packaging db as a Spring boot "application" rather than a library by using spring-boot-maven-plugin:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>

The jar is repackaged, thus adding the com.example.db.repositories package (and its classes) in the BOOT-INF folder. This causes the compilation failure.

Boot-repackage

Simply remove the <plugin>..</plugin> part from the db/pom.xml. This will create a regular jar that can be imported in the api module.

Jar

Note: I'm assuming that api has the Main class and will packaged as a boot application.


Springboot autodiscovery will descend only from your configuration class down. your application is at

com.example.api

but the repo is at

com.example.db

either add a search path to autodiscover .db as well or move your application class to com.example or the db code to com.example.api

Option 1

@ComponentScan(“com.example”)
@SpringBootApplication
public class ExampleApplication  {

Option 2

@ComponentScan({"com.example.api","com.example.db"})
@SpringBootApplication
public class ExampleApplication {

You can also add the scanBasePackages attribute to the SpringbootApplication annotation for the same effect.

@SpringBootApplication(scanBasePackages= {"com.example.api","com.example.db"})
public class ExampleApplication {

see the docs here https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html#scanBasePackages--

Tags:

Java

Maven

Spring