百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

gs-multi-module

> 编程语言
开源

创建多模块项目 :: 了解如何构建库并将其打包以供 Spring Boot 应用程序使用

358 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

创建多模块项目 :: 了解如何构建库并将其打包以供 Spring Boot 应用程序使用

:toc: :icons: font :source-highlighter: prettify :project_id: gs-multi-module

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.

== What You Need

:java_version: 17 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/how_to_complete_this_guide.adoc[]

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/build_system_intro.adoc[]

[#scratch] == 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 `` listing the subdirectories:

==== [src,xml]

include::complete/pom.xml[]

====

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

==== [src,groovy]

include::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.

[#initial] == 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 https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies[(BOM)] in the `` section of the pom.xml file.

[#setting-up-the-library-project] === 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 https://start.spring.io/#!type=maven-project&language=java&packaging=jar&groupId=com.example&artifactId=multi-module-library&name=multi-module-library&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.multi-module-library[Spring Initializr]. The following listing shows the pom.xml file that is created when you choose Maven:

==== [src,xml]

include::initial/library/pom.xml[]

====

You can get a Gradle build file with the necessary dependencies directly from the https://start.spring.io/#!type=gradle-project&language=java&packaging=jar&groupId=com.example&artifactId=multi-module-library&name=multi-module-library&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.multi-module-library[Spring Initializr]. The following listing shows the build.gradle file that is created when you choose Gradle:

==== [src,java]

include::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:

[source]

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

It is better that the library depends on the most narrowed dependencies, and not a starter. For our own use there org.springframework.boot:spring-boot has all the code that we need. Removing the -starter of the existing entry makes sure the library doesn't bring up too much dependencies.

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:

==== [source,xml]

  org.springframework.boot
  spring-boot-maven-plugin

====

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

==== [src,xml]

include::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:

==== [source,groovy,tabsize=2]

plugins { id 'org.springframework.boot' version '3.2.2' apply false id 'io.spring.dependency-management' version '1.1.4' // ... 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.

Also, now that we have disabled the Spring Boot plugin, it no longer automatically configures the JavaCompiler task to enable the -parameters option. This is important if you are using an expression that refers to a parameter name. The following enables this option:

==== [source,groovy,tabsize=2]

tasks.withType(JavaCompile).configureEach { options.compilerArgs.add("-parameters")

}

====

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

==== [src,java]

include::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:

==== [source,java,tabsize=2]

include::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:

==== [source,java,tabsize=2]

include::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:

==== [source,java,tabsize=2]

include::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] === 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 https://start.spring.io/#!type=maven-project&language=java&packaging=jar&groupId=com.example&artifactId=multi-module-application&name=multi-module-application&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.multi-module-application&dependencies=web,actuator[Spring Initializr]. The following listing shows the pom.xml file that is created when you choose Maven:

==== [src,xml]

include::initial/application/pom.xml[]

====

You can get a Gradle build file with the necessary dependencies directly from the https://start.spring.io/#!type=gradle-project&language=java&packaging=jar&groupId=com.example&artifactId=multi-module-application&name=multi-module-application&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.multi-module-application&dependencies=web,actuator[Spring Initializr]. The following listing shows the build.gradle file that is created when you choose Gradle:

==== [src,java]

include::initial/application/build.gradle[]

====

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

[source]

$ 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:

==== [source,xml]

com.example library ${project.version}


====

The following listing shows the finished pom.xml file:

==== [source,xml]

include::complete/application/pom.xml[]

====

For Gradle, add the following dependency:

==== [source,groovy,tabsize=2]

implementation project(':library')

====

The following listing shows the finished build.gradle file:

==== [source,groovy]

include::complete/application/build.gradle[]

====

== Write the Application

The main class in the application can be a @RestController that uses the Service from

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Java

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言