1. Overview
1.概述
In this quick tutorial, we’ll show how to create a multi-module project with Spring Boot.
在这个快速教程中,我们将展示如何用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.
首先,我们将建立一个本身不是应用程序的库jar,然后我们将建立一个使用我们库的应用程序。
For an introduction to Spring Boot, please refer to this article.
关于Spring Boot的介绍,请参考这篇文章。
2. Setup
2.设置
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:
为了设置我们的多模块项目,让我们在Maven配置中使用pom打包创建一个简单的模块,以聚合我们的库和应用模块。
<groupId>com.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.
我们将在项目中创建两个目录,将应用程序模块与库jar模块分开。
Let’s declare our modules in the pom.xml:
让我们在pom.xml中声明我们的模块。
<modules>
<module>library</module>
<module>application</module>
</modules>
3. Library Jar
3.图书馆罐子
For our library module, we’ll use jar packaging:
对于我们的library模块,我们将使用jar打包。
<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:
由于我们想利用Spring Boot依赖性管理,我们将使用spring-boot-starter-parent作为父项目,注意设置<relativePath/>为空值,这样Maven就能从资源库解析父pom.xml。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.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:
请注意,如果我们有自己的父项目,我们可以在pom.xml的<dependencyManagement/>部分将依赖管理作为物料清单(BOM)导入。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<version>2.4.0</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.
在这个模块中,Spring Boot插件不是必需的,因为它的主要功能是创建一个可执行的über-jar,我们不想要也不需要这样的一个库。
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
4.应用项目
Like our library module, our application module will use jar packaging:
与我们的library模块一样,我们的应用模块将使用jar打包。
<groupId>com.baeldung.example</groupId>
<artifactId>application</artifactId>
<packaging>jar</packaging>
And we’ll take advantage of Spring Boot dependency management as before:
我们将像以前一样利用Spring Boot的依赖性管理。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
In addition to the Spring Boot starter dependency, we’ll include our library jar created in the previous section:
除了Spring Boot启动器的依赖性,我们将包括我们在上一节创建的库 jar。
<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:
最后,我们将使用Spring Boot插件:。
<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.
首先,它提供了一个内置的依赖性解析器,可以设置版本号以匹配Spring Boot依赖性。
Second, it searches for the main method to flag as a runnable class.
第二,它搜索main方法,将其标记为一个可运行的类。
Finally and perhaps most importantly, it collects all the jars on the classpath and builds a single, runnable über-jar.
最后,也许是最重要的,它收集了classpath上的所有jars,并建立了一个单一的、可运行的ü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
5.总结
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.
在这篇文章中,我们探讨了如何实现和配置一个多模块项目,并通过Spring Boot独立构建一个库jar。
As always, code samples can be found over on GitHub.
一如既往,代码样本可以在GitHub上找到over。