5

I have this multi modules project:

Parent Pom
|
|----Main module (@SpringBootApplcation and application.properties)
|
|----Module I
|
|----Module II
|
|----Module III

The main module depends on other modules and contains messages.properties and messages_fr.properties under resources/messages/ and also the application.properties where I defined MessageResource bean spring.messages.basename=messages/messages. Now I would like to put the messages.properties files in each module. Eg for Module I, under src/main/resources/messages/messages.properties same thing for Module II. My question is, how can I add the module's messages in the MessageResource bean defined in application.properties? I tried several declarations like these:

spring.messages.basename=messages/messages,classpath:/com/company/moduleI/resources/messages/messages or spring.messages.basename=messages/messages,classpath:moduleI/resources/messages/messages but none works. Is there a way to achieve this?

akuma8
  • 4,160
  • 5
  • 46
  • 82
  • By default Maven copies the content of a `resource` folder to the root of class path. Have you tried `classpath:messages/messages`? – Ken Bekov Dec 26 '18 at 13:43
  • @KenBekov I've just tried it but same result – akuma8 Dec 26 '18 at 19:39
  • Check if this file exists in the root classpath (`target`). If it isn't then you can use Maven assembly plugin in order to copy (and rename) files from dependency modules. – Ken Bekov Dec 27 '18 at 06:21
  • @KenBekov When I looked inside the main module .jar all other modules are in its `lib` folder. Resources files of each module are on the root of the module's classpath. I will take a look on that Maven assembly plugin. I'll let you know. Thanks for this hint – akuma8 Dec 27 '18 at 10:00

1 Answers1

4

I finally found a better solution, using the ResourceBundleMessageSource#addBasenames(...) to add all modules' messages.

EDIT

In each module, I renamed the message files, e.g. for Module I, I had resources/messages/moduleI-messages.properties, resources/messages/moduleI-messages_en.properties. Then I defined a configuration class in that module to add the messages source files.

@Configuration
public class ModuleConfig {
    private final ResourceBundleMessageSource messageSource;

    @PostConstruct
    public void addMessageBaseName() {
        messageSource.addBasenames( "messages/module1-messages" );
    }
}

I am sure there could be a better way to go but I didn't find it.

akuma8
  • 4,160
  • 5
  • 46
  • 82