0

I'm creating a multimodule project using Spring Boot. the structure of the project is like this:

-configuration
|--- src
  |--- main
    |--- java
    |--- resources
  |--- build.gradle
-module1
|--- src
  |--- main
    |--- java
    |--- resources
  |--- build.gradle
-module2
|--- src
  |--- main
    |--- java
    |--- resources
  |--- build.gradle
build.gradle
settings.gradle

module1 is for the persistence of the application, and its resources directory contains the file application.properties containing the configuration of the database and also data.sql and schema.sql

When I run the application the resources are loaded only from the "configuration" module and not from module1. my goal is to load all the resources folders of all the modules (since each module has a different responsibility)

idri
  • 31
  • 6

2 Answers2

0

Could you post the error you receive? My guess is you need a @componentScan but it would be much helpful to see the error.

  • There is no error, the issue is that data.sql and schema.sql are not executed on startup – idri Feb 22 '22 at 20:03
  • Could you give out more information? What is supposed to run the sql files (and in which module it is)? where are they located? Why the sql files should run everytime you start a service? – Ravid luz Feb 22 '22 at 20:35
  • As I already wrote, the sql files are in module1 in the resources folder (schema and data). those files are needed to initialize the schema of my database and to insert some initial data when application start the first time. – idri Feb 22 '22 at 20:40
  • Ok. Is module1 is the one who loads them or is it another module? – Ravid luz Feb 22 '22 at 20:42
  • Here is the problem, they are not loaded at all. But normally they should be loaded by the "configuration" module (this is my goal) – idri Feb 22 '22 at 20:46
  • First of all, If the configuration module is supposed to load them why are they located in module1? Also, I assume that you tried to run the configuration module right? – Ravid luz Feb 22 '22 at 20:48
  • They are located in module1 because I'm applying a clean architecture so I'm separating concerns. module1 is responsible for persistence so all resources related to persistence should be located there. And the configuration module is the one which start the app so it unify all the configs from all the modules. – idri Feb 22 '22 at 20:53
  • Please answer me both questions: 1. Which module were you trying to run? 2. How do you load the files? (a buffered reader for example) – Ravid luz Feb 22 '22 at 20:57
0

I found a solution to my issue like this:

in build.gradle of the configuration module I added this task:

task copyResources(type: Copy) {
    println "Copying resource"
    from "${project(':module1').buildDir}/resources/main/schema.sql"
    into "${buildDir}/resources/test"
    from "${project(':module1').buildDir}/resources/main/data.sql"
    into "${buildDir}/resources/test"
}
processResources.dependsOn copyResources

This will copy the data.sql and schema.sql from module1 to the configuration module at build time. and then when application start (from the configuration module) it will use those copied files as they are located in its resources folder.

idri
  • 31
  • 6