• Stars
    star
    329
  • Rank 128,030 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Creating a Multi Module Project :: Learn how to build a library and package it for consumption in a Spring Boot application

This guide shows you how to create a multi-module project with Spring Boot. The project will have a library jar and a main application that uses the library. You could also use it to see how to build a library (that is, a jar file that is not an application) on its own.

What You Will Build

You will set up a library jar that exposes a service for simple โ€œHello, Worldโ€ messages and then include the service in a web application that uses the library as a dependency.

Create a Root Project

This guide walks through building two projects, one of which is a dependency to the other. Consequently, you need to create two child projects under a root project. But first, create the build configuration at the top level. For Maven you will want a pom.xml with <modules> listing the subdirectories:

link:complete/pom.xml[]

For Gradle, you will want a settings.gradle including the same directories:

link:complete/settings.gradle[]

and (optionally) you could include an empty build.gradle (to help IDEs identify the root directory).

Create the Directory Structure

In the directory that you want to be your root directory, create the following subdirectory structure (for example, with mkdir library application on *nix systems):

โ””โ”€โ”€ library
โ””โ”€โ”€ application

In the root of the project, you will need to set up a build system, and this guide shows you how to use Maven or Gradle.

Create the Library Project

One of the two projects serves as a library that the other project (the application) will use.

Create the Directory Structure

In a the library directory, create the following subdirectory structure (for example, by using mkdir -p src/main/java/com/example/multimodule/service on *nix systems):

โ””โ”€โ”€ src
    โ””โ”€โ”€ main
        โ””โ”€โ”€ java
            โ””โ”€โ”€ com
                โ””โ”€โ”€ example
                    โ””โ”€โ”€ multimodule
                        โ””โ”€โ”€ service

Now you need to configure a build tool (Maven or Gradle). In both cases, note that the Spring Boot plugin is not used in the library project at all. The main function of the plugin is to create an executable โ€œรผber-jarโ€, which we neither need nor want for a library.

Although the Spring Boot Maven plugin is not being used, you do want to take advantage of Spring Boot dependency management, so that is configured by using the spring-boot-starter-parent from Spring Boot as a parent project. An alternative would be to import the dependency management as a Bill of Materials (BOM) in the <dependencyManagement/> section of the pom.xml file.

Setting up the Library Project

For the Library project, you need not add dependencies. The basic spring-boot-starter dependency provides everything you need.

You can get a Maven build file with the necessary dependencies directly from the Spring Initializr. The following listing shows the pom.xml file that is created when you choose Maven:

link:initial/library/pom.xml[]

You can get a Gradle build file with the necessary dependencies directly from the Spring Initializr. The following listing shows the build.gradle file that is created when you choose Gradle:

link:initial/library/build.gradle[]

Adjusting the Library Project

If you generated the Library project from start.spring.io it will contain a wrapper script for the build system (mvnw or gradlew depending on the choice you made). You can move that script and its associated configuration up to the root directory:

$ mv mvnw* .mvn ..
$ mv gradlew* gradle ..

The Library project has no class with a main method (because it is not an application). Consequently, you have to tell the build system to not try to build an executable jar for the Library project. (By default, the Spring Initializr builds executable projects.)

To tell Maven to not build an executable jar for the Library project, you must remove the following block from the pom.xml created by the Spring Initializr:

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

The following listing shows the final pom.xml file for the Library project:

link:complete/library/pom.xml[]

To tell Gradle to not build an executable jar for the Library project, you must add the following blocks to the build.gradle created by the Spring Initializr:

plugins {
  id 'org.springframework.boot' version '2.5.2' apply false
  id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  // ... other plugins
}

dependencyManagement {
  imports {
    mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
  }
}

The bootJar task tries to create an executable jar, and that requires a main() method. As a result, you need to disable it by disabling the the Spring Boot plugin, while keeping it for its dependency management features.

The following listing shows the final build.gradle file for the Library project:

link:complete/library/build.gradle[]

Create a Service Component

The library will provide a MyService class that can be used by applications. The following listing (from library/src/main/java/com/example/multimodule/service/MyService.java) shows the MyService class:

link:complete/library/src/main/java/com/example/multimodule/service/MyService.java[]

To make it configurable in the standard Spring Boot idiom (with application.properties), you can also add a @ConfigurationProperties class. The ServiceProperties class (from library/src/main/java/com/example/multimodule/service/ServiceProperties.java) fills that need:

link:complete/library/src/main/java/com/example/multimodule/service/ServiceProperties.java[]

You need not do it this way. A library might merely provide pure Java APIs and no Spring features. In that case, the application that consumes the library would need to provide the configuration itself.

Testing the Service Component

You will want to write unit tests for your library components. If you provide re-usable Spring configuration as part of the library, you might also want to write an integration test, to make sure that the configuration works. To do that, you can use JUnit and the @SpringBootTest annotation. The following listing (from library/src/test/java/com/example/multimodule/service/MyServiceTest.java) shows how to do so:

link:complete/library/src/test/java/com/example/multimodule/service/MyServiceTest.java[]
Note
In the preceding listing, we have configured the service.message for the test by using the default attribute of the @SpringBootTest annotation. We do not recommend putting application.properties in a library, because there might be a clash at runtime with the application that uses the library (only one application.properties is ever loaded from the classpath). You could put application.properties in the test classpath but not include it in the jar (for instance, by placing it in src/test/resources).

Create the Application Project

The Application project uses the Library project, which offers a service that other projects can use.

Create the Directory Structure

In the application directory, create the following subdirectory structure (for example, with mkdir -p src/main/java/com/example/multimodule/application on *nix systems):

โ””โ”€โ”€ src
    โ””โ”€โ”€ main
        โ””โ”€โ”€ java
            โ””โ”€โ”€ com
                โ””โ”€โ”€ example
                    โ””โ”€โ”€ multimodule
                        โ””โ”€โ”€ application

Do not use the same package as the library (or a parent of the library package) unless you want to include all Spring components in the library by @ComponentScan in the application.

Setting up the Application Project

For the Application project, you need the Spring Web and Spring Boot Actuator dependencies.

You can get a Maven build file with the necessary dependencies directly from the Spring Initializr. The following listing shows the pom.xml file that is created when you choose Maven:

link:initial/application/pom.xml[]

You can get a Gradle build file with the necessary dependencies directly from the Spring Initializr. The following listing shows the build.gradle file that is created when you choose Gradle:

link:initial/application/build.gradle[]

You can delete the mvnw and/or gradlew wrappers and their associated configuration files:

$ rm -rf mvnw* .mvn
$ rm -rf gradlew* gradle

Adding the Library Dependency

The Application project needs to have a dependency on the Library project. You need to modify your Application build file accordingly.

For Maven, add the following dependency:

<dependency>
  <groupId>com.example</groupId>
  <artifactId>library</artifactId>
  <version>${project.version}</version>
</dependency>

The following listing shows the finished pom.xml file:

link:complete/application/pom.xml[]

For Gradle, add the following dependency:

implementation project(':library')

The following listing shows the finished build.gradle file:

link:complete/application/build.gradle[]

Write the Application

The main class in the application can be a @RestController that uses the Service from the library to render a message. The following listing (from application/src/main/java/com/example/multimodule/application/DemoApplication.java) shows such a class:

link:complete/application/src/main/java/com/example/multimodule/application/DemoApplication.java[]

Because DemoApplication is inside a different package (com.example.multimodule.application) than MyService (com.example.multimodule.service), @SpringBootApplication cannot automatically detect it. There are different ways to let `MyService be picked up:

  • Import it directly with @Import(MyService.class).

  • Fetch everything from its package by using @SpringBootApplication(scanBasePackageClasses={โ€ฆโ€‹}).

  • Specifying the parent package by name: com.example.multimodule. (This guide uses this method)

Note
If your application also uses JPA or Spring Data, the @EntityScan and @EnableJpaRepositories (and related) annotations inherit only their base package from @SpringBootApplication when not explicitly specified. That is, once you specify scanBasePackageClasses or scanBasePackages, you might also have to also explicitly use @EntityScan and @EnableJpaRepositories with their package scans explicitly configured.

Create the application.properties File

You need to provide a message for the service in the library in application.properties. In the source folder, you need to create a file named src/main/resources/application.properties. The following listing shows a file that would work:

link:complete/application/src/main/resources/application.properties[]

Test the Application

Test the end-to-end result by starting the application. You can start the application in your IDE or use the command line. Once the application is running, visit the client application in the browser, at http://localhost:8080/. There, you should see Hello, World reflected in the response.

If you use Gradle, the following command (really two commands run in sequence) will first build the library and then run the application:

$ ./gradlew build && ./gradlew :application:bootRun

If you use Maven, the following command (really two commands run in sequence) will first build the library and then run the application:

$ ./mvnw install && ./mvnw spring-boot:run -pl application

Summary

Congratulations! You have used Spring Boot to create a re-usable library and then used that library to build an application.

More Repositories

1

tut-spring-security-and-angular-js

Spring Security and Angular:: A tutorial on how to use Spring Security with a single page application with various backend architectures, ranging from a simple single server to an API gateway with OAuth2 authentication.
TypeScript
1,695
star
2

gs-rest-service

Building a RESTful Web Service :: Learn how to create a RESTful web service with Spring.
Java
1,391
star
3

tut-spring-boot-kotlin

Building web applications with Spring Boot and Kotlin :: Learn how to easily build and test web applications with Spring, Kotlin, Junit 5 and JPA
Kotlin
914
star
4

tut-spring-boot-oauth2

Spring Boot and OAuth2:: A tutorial on "social" login and single sign on with Facebook and Github
Java
905
star
5

gs-spring-boot

Building an Application with Spring Boot :: Learn how to build an application with minimal configuration.
Java
897
star
6

tut-react-and-spring-data-rest

React.js and Spring Data REST :: A tutorial based on the 5-part blog series by Greg Turnquist
JavaScript
884
star
7

gs-spring-boot-docker

Spring Boot with Docker :: Learn how to create a Docker container from a Spring Boot application with Maven or Gradle
Java
619
star
8

gs-messaging-stomp-websocket

Using WebSocket to build an interactive web application :: Learn how to the send and receive messages between a browser and the server over a WebSocket
Java
528
star
9

getting-started-guides

Getting Started Guide template :: The template for new guides and also the place to request them.
Java
519
star
10

tut-rest

Building REST services with Spring :: Learn how to easily build RESTful services with Spring
Java
516
star
11

gs-uploading-files

Uploading Files :: Learn how to build a Spring application that accepts multi-part file uploads.
Java
471
star
12

gs-securing-web

Securing a Web Application :: Learn how to protect your web application with Spring Security.
Java
351
star
13

gs-serving-web-content

Serving Web Content with Spring MVC :: Learn how to create a web page with Spring MVC and Thymeleaf.
Java
275
star
14

gs-batch-processing

Creating a Batch Service :: Learn how to create a basic batch-driven solution.
Java
243
star
15

gs-accessing-data-jpa

Accessing Data with JPA :: Learn how to work with JPA data persistence using Spring Data JPA.
Java
229
star
16

top-spring-security-architecture

Spring Security Architecture:: Topical guide to Spring Security, how the bits fit together and how they interact with Spring Boot
226
star
17

gs-consuming-rest

Consuming a RESTful Web Service :: Learn how to retrieve web page data with Spring's RestTemplate.
Java
195
star
18

gs-accessing-data-mysql

Accessing data with MySQL :: Learn how to set up and manage user accounts on MySQL and how to configure Spring Boot to connect to it at runtime.
Java
192
star
19

gs-messaging-rabbitmq

Messaging with RabbitMQ :: Learn how to create a simple publish-and-subscribe application with Spring and RabbitMQ.
Java
183
star
20

gs-testing-web

Testing the Web Layer :: Learn how to test Spring Boot applications and MVC controllers.
Java
178
star
21

gs-maven

Building Java Projects with Maven :: Learn how to build a Java project with Maven.
Java
165
star
22

gs-reactive-rest-service

Building a Reactive RESTful Web Service :: Learn how to create a RESTful web service with Reactive Spring and consume it with WebClient.
Java
162
star
23

gs-gradle

Building Java Projects with Gradle :: Learn how to build a Java project with Gradle.
Java
161
star
24

gs-producing-web-service

Producing a SOAP web service :: Learn how to create a SOAP-based web service with Spring.
Java
153
star
25

gs-service-registration-and-discovery

Service Registration and Discovery :: Learn how to register and find services with Eureka
Java
151
star
26

gs-accessing-data-rest

Accessing JPA Data with REST :: Learn how to work with RESTful, hypermedia-based data persistence using Spring Data REST.
Java
146
star
27

gs-consuming-web-service

Consuming a SOAP web service :: Learn how to create a client that consumes a WSDL-based service
Java
136
star
28

gs-accessing-data-mongodb

Accessing Data with MongoDB :: Learn how to persist data in MongoDB.
Java
135
star
29

gs-scheduling-tasks

Scheduling Tasks :: Learn how to schedule tasks with Spring.
Java
129
star
30

gs-validating-form-input

Validating Form Input :: Learn how to perform form validation with Spring.
Java
124
star
31

gs-rest-service-cors

Enabling Cross Origin Requests for a RESTful Web Service :: Learn how to create a RESTful web service with Spring that support Cross-Origin Resource Sharing (CORS).
Java
110
star
32

gs-crud-with-vaadin

Creating CRUD UI with Vaadin :: Use Vaadin and Spring Data JPA to build a dynamic UI
Java
108
star
33

gs-gateway

Building a Gateway :: Learn how to configure a gateway
Java
106
star
34

gs-authenticating-ldap

Authenticating a User with LDAP :: Learn how to secure an application with LDAP.
Java
97
star
35

gs-messaging-jms

Messaging with JMS :: Learn how to publish and subscribe to messages using a JMS broker.
Java
90
star
36

gs-async-method

Creating Asynchronous Methods :: Learn how to create asynchronous service methods.
Java
86
star
37

gs-relational-data-access

Accessing Relational Data using JDBC with Spring :: Learn how to access relational data with Spring.
Java
80
star
38

gs-messaging-redis

Messaging with Redis :: Learn how to use Redis as a message broker.
Java
80
star
39

gs-actuator-service

Building a RESTful Web Service with Spring Boot Actuator :: Learn how to create a RESTful Web service with Spring Boot Actuator.
Java
74
star
40

gs-rest-hateoas

Building a Hypermedia-Driven RESTful Web Service :: Learn how to create a hypermedia-driven RESTful Web service with Spring.
Java
73
star
41

gs-accessing-mongodb-data-rest

Accessing MongoDB Data with REST :: Learn how to work with RESTful, hypermedia-based data persistence using Spring Data REST.
Java
70
star
42

gs-caching

Caching Data with Spring :: Learn how to cache data in memory with Spring
Java
67
star
43

gs-centralized-configuration

Centralized Configuration :: Learn how to manage application settings from an external, centralized source
Java
58
star
44

gs-accessing-data-r2dbc

Accessing data with R2DBC :: Learn how to access relational data with the reactive protocol R2DBC
Java
47
star
45

gs-spring-boot-kubernetes

Spring Boot Kubernetes :: Deploy a Spring Boot application to Kubernetes :: spring-boot,spring-framework
Java
46
star
46

tut-spring-webflux-kotlin-rsocket

Spring Boot with Kotlin Coroutines and RSocket :: Build a chat application with Reactive Web services from Spring, Kotlin, WebFlux and RSocket
JavaScript
45
star
47

gs-handling-form-submission

Handling Form Submission :: Learn how to create and submit a web form with Spring.
Java
45
star
48

gs-spring-data-reactive-redis

Accessing Data Reactively with Redis :: Learn how to reactively interface with Redis and Spring Data
Java
35
star
49

gs-sts

Working a Getting Started guide with STS :: Learn how to import a Getting Started guide with Spring Tool Suite (STS).
Shell
34
star
50

gs-consuming-rest-angularjs

Consuming a RESTful Web Service with AngularJS :: Learn how to retrieve web page data with AngularJS.
HTML
32
star
51

gs-accessing-data-neo4j

Accessing Data with Neo4j :: Learn how to persist objects and relationships in Neo4j's NoSQL data store.
Java
29
star
52

quoters

Spring Boot quotation service to support REST-based guides
Java
28
star
53

gs-spring-cloud-loadbalancer

Client-Side Load-Balancing with Spring Cloud LoadBalancer :: Dynamically select correct instance for the request :: spring-cloud,spring-cloud-loadbalancer,spring-cloud-commons
Java
28
star
54

gs-convert-jar-to-war

Converting a Spring Boot JAR Application to a WAR :: Learn how to convert your Spring Boot JAR-based application to a WAR file.
Shell
22
star
55

gs-graphql-server

Building a GraphQL service :: Learn how to build a GraphQL service with Spring for GraphQL.
Java
21
star
56

top-spring-on-kubernetes

Spring on Kubernetes :: Topic guide to Spring and Kubernetes
Java
20
star
57

gs-managing-transactions

Managing Transactions :: Learn how to wrap key parts of code with transactions.
Java
19
star
58

gs-messaging-gcp-pubsub

Messaging with Google Cloud Pub/Sub :: Learn how to exchange messages using Spring Integration channel adapters and Google Cloud Pub/Sub
Java
18
star
59

gs-vault-config

Vault Configuration :: Learn how to store and retrieve application configuration details in HashiCorp Vault
Java
16
star
60

getting-started-macros

Collection of macros used to support getting started guides
15
star
61

gs-testing-restdocs

Creating API Documentation with Restdocs :: Learn how to generate documentation for HTTP endpoints using Spring Restdocs
Java
14
star
62

gs-intellij-idea

Working a Getting Started guide with IntelliJ IDEA :: Learn how to work a Getting Started guide with IntelliJ IDEA.
Shell
14
star
63

gs-spring-boot-for-azure

Deploying a Spring Boot app to Azure :: Learn how to deploy a Spring Boot app to Azure.
Shell
14
star
64

gs-contract-rest

Consumer Driven Contracts :: Learn how to with contract stubs and consuming that contract from another Spring application
Java
13
star
65

gs-integration

Integrating Data :: Learn how to build an application that uses Spring Integration to fetch data, process it, and write it to a file.
Java
12
star
66

gs-cloud-circuit-breaker

Spring Cloud Circuit Breaker Guide :: How to Use Spring Cloud Circuit Breaker
Java
12
star
67

gs-accessing-neo4j-data-rest

Accessing Neo4j Data with REST :: Learn how to work with RESTful, hypermedia-based data persistence using Spring Data REST.
Java
11
star
68

gs-consuming-rest-jquery

Consuming a RESTful Web Service with jQuery :: Learn how to retrieve web page data with jQuery.
HTML
10
star
69

gs-caching-gemfire

Caching Data with Pivotal GemFire :: Learn how to cache data in GemFire.
Java
9
star
70

gs-accessing-data-gemfire

Accessing Data in Pivotal GemFire :: Learn how to build an application using Gemfire's data fabric.
Java
9
star
71

gs-accessing-vault

Accessing Vault :: Learn how to use Spring Vault to load secrets from HashiCorp Vault
Java
7
star
72

gs-accessing-gemfire-data-rest

Accessing Data in Pivotal GemFire with REST :: Learn how to work with RESTful, hypermedia-based data persistence using Spring Data REST.
Java
6
star
73

gs-spring-cloud-stream

Spring Cloud Stream :: Learn how to build and test Spring Cloud Stream Applications with RabbitMQ and Apache Kafka
Java
5
star
74

gs-accessing-data-cassandra

Accessing Data with Cassandra :: Learn how to persist data in Cassandra.
Java
5
star
75

issue-aggregator

List issues from multiple GH repo
Kotlin
3
star
76

gs-spring-cloud-task

Spring Cloud Task :: Learn how to build and test Spring Cloud Task Applications
Java
3
star
77

gs-tanzu-observability

Observability with Spring :: Learn how to send application metrics to Tanzu Observability
Java
3
star
78

drone-aggregator

Get a snapshot view of your CI jobs hosted at drone.io
SCSS
2
star
79

gs-sts-cloud-foundry-deployment

Deploying to Cloud Foundry from STS :: Learn how to deploy a Spring application to Cloud Foundry from STS
Shell
2
star
80

gs-guides-with-vscode

Building a Guide with VS Code :: Learn how to import and work with a Spring Getting Started Guide in VS Code.
Shell
2
star
81

gs-spring-cloud-dataflow

Spring Cloud Data Flow :: Learn how to build, deploy and launch streaming and batch data pipelines using Spring Cloud Data Flow
2
star
82

topical-guides

Spring Topical Guides:: The template for new topical guides on spring.io and also the place to request them
2
star
83

top-observing-graphql-in-action

Observing GraphQL in action :: Tutorial on GraphQL and Observability
Java
1
star