I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll show how to create a multi-module project with Spring Boot.

First, we’ll build a library jar that isn’t an application itself, and then we’ll build an application that uses our library.

For an introduction to Spring Boot, please refer to this article.

2. Setup

To set up our multi-module project, let’s create a simple module using pom packaging to aggregate our library and application modules in our Maven configuration:

<groupId>org.baeldung</groupId>
<artifactId>parent-multi-module</artifactId>
<packaging>pom</packaging>

We’ll create two directories inside our project that will divide the application module from the library jar module.

Let’s declare our modules in the pom.xml:

<modules>
    <module>library</module>
    <module>application</module>
</modules>

3. Library Jar

For our library module, we’ll use jar packaging:

<groupId>com.baledung.example</groupId>
<artifactId>library</artifactId>
<packaging>jar</packaging>

As we want to take advantage of Spring Boot dependency management, we’ll use the spring-boot-starter-parent as the parent project, taking care to set <relativePath/> to an empty value so that Maven will resolve the parent pom.xml from the repositories:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/>
</parent>

Note that if we have our own parent project, we can instead import the dependency management as a Bill of Materials (BOM) in the <dependencyManagement/> section of the pom.xml:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <type>pom</type>
            <version>2.0.2.RELEASE</version>
            <scope>import</scope>
        </dependency>
    </dependencies>
<dependencyManagement>

Finally, the initial dependencies are going to be quite simple:

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

In this module, the Spring Boot plugin isn’t necessary because the main function of it is to create an executable über-jar, which we don’t want and don’t need for a library.

After that, we’re ready to develop a service component that will be provided by the library:

@Service
public class EvenOddService {

    public String isEvenOrOdd(Integer number) {
        return number % 2 == 0 ? "Even" : "Odd";
    }
}

4. Application Project

Like our library module, our application module will use jar packaging:

<groupId>com.baeldung.example</groupId>
<artifactId>application</artifactId>
<packaging>jar</packaging>

And we’ll take advantage of Spring Boot dependency management as before:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/>
</parent>

In addition to the Spring Boot starter dependency, we’ll include our library jar created in the previous section:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baeldung.example</groupId>
        <artifactId>library</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

Finally, we’ll use the Spring Boot plugin:

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

There are several convenient reasons to use the plugin mentioned above in this place.

First, it provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies.

Second, it searches for the main method to flag as a runnable class.

Finally and perhaps most importantly, it collects all the jars on the classpath and builds a single, runnable über-jar.

Now that everything is ready to write our application class and to go straight to the point, let’s implement a controller inside the main application class:

@SpringBootApplication(scanBasePackages = "com.baeldung")
@RestController
public class EvenOddApplication {

    private EvenOddService evenOddService;

    // constructor

    @GetMapping("/validate/")
    public String isEvenOrOdd(
      @RequestParam("number") Integer number) {
        return evenOddService.isEvenOrOdd(number);
    }

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

5. Conclusion

In this article, we’ve explored how to implement and configure a multi-module project and build a library jar on its own with Spring Boot.

As always, code samples can be found over on GitHub.

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS

newest oldest most voted
jean
Guest
jean

Hello
thanks for this article.
would you please try some example with a library that contain multiple modules. For example library has POM packaging type and has his own sub-modules JAR/WAR packaging type.

Thanks.

Loredana Crusoveanu
Editor

Hey Jean,

Thanks for the suggestion. I’ve added the topic to our content calendar.