3
votes

I am trying to create a multi module project that contains two module, core and web. They are both Spring Boot projects that I generated on Spring Initialzer. I setup the Maven POM files but I'm having issues getting it to deploy. However I am confused how the configuration is going to work.

The core module is going to contain the domain object / entities, Spring Data JPA repositories, services, and will be packaged as a JAR. The Web module is going to have Spring Security, the controllers, and the views. It will be packaged as a WAR.

The normal structure of a Spring Boot project looks like the following

/
pom.xml
src/
..main/
....com/
......example/
........app/
..........Application.java
..resources/
....application.properites

I essentially have two of these and two Spring Boot application / configuration / initialization classes.

My questions are

  1. Do the properties have to live in a single configuration file or can I have two application.properities, one the core jar, and one for the WAR?

  2. Can I have the following in my core.jar

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class TimesheetCoreApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(TimesheetCoreApplication.class, args);
      }
    }
    

Along with the following two in my web.war

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TimesheetWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(TimesheetWebApplication.class, args);
    }
}

AND

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(TimesheetWebApplication.class);
    }

}
  1. Since Spring Boot does a lot of auto configuration, will they step over each other with the configuration, either conflicting or overriding one another?

What is the best way to approach this? I would like to leverage Spring Boot if possible.

3
Your core application is going to do nothing as it is just another jar like anyother included in a web project. So only your web jar will do something, are you really sure you want your core application to be a spring boot application and not only the web? What is the point if it isn't a standalone application and only for embedding.M. Deinum

3 Answers

2
votes

your design is slightly incorrect, if your core project is going to have domain entities, repositories etc. it need not be a BOOT Application. As you said, this is going to be a Jar.

Now you make the your web application a Boot Application with dependency on the core module jar. You can define .properties or .yml in the Boot Application and you are good to go.

The problem with Spring Initializer is, it gives you an impression that all the code need to reside under a single project or module.

This is typical project I follow for my Applications

  • project-core-module (packaging Jar -> domain entities, repositories etc)
  • project-service-module (boot -> configuration, rest interface, security etc)
  • project-system-tests (runs on compile time and certify the build)

Now you bind core and system module with service module as dependency.

1
votes

Please find answers to your questions

1. Do the properties have to live in a single configuration file or can I have two application.properities, one the core jar, and one for the WAR?

They cannot both be named application.properties. Refer my answer to this question here.

2. Can I have the following in my core.jar

Yes you can, if you follow the steps mentioned in the link.

3. Since Spring Boot does a lot of auto configuration, will they step over each other with the configuration, either conflicting or overriding one another?

You will only ever boot one application at a time. Say if you are running the TimesheetWebApplication application then the TimesheetCoreApplicationwill not be booted. The time TimesheetCoreApplication will only be included as a normal JAR.

In addition you can also do the following

  1. Package your TimesheetCoreApplicationas follows. Refer here. This will make sure that every time you do maven install, the jar which will only contain the class files(No tomcat embedded libraries) will be installed to repo.

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                      <execution>
                        <goals>
                          <goal>repackage</goal>
                        </goals>
                        <configuration>
                          <classifier>exec</classifier>
                        </configuration>
                      </execution>
                    </executions>
                </plugin>
            </plugins>
    

0
votes

Briefly,

  1. You don't need .properties , you can use yaml with spring boot, it's more readable.Single yaml file is enough for your configuration,because yaml uses indents, so you can distinguish parameters easily.

  2. Single Application class is fine, because you won't execute only core module.

  3. There are lots of starter dependencies and their configurations , you can speed up in projects.