Using a Spring Boot Application as a Dependency – 将Spring Boot应用程序作为依赖关系使用

最后修改: 2019年 7月 26日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll see how to use a Spring Boot application as a dependency of another project.

在本教程中,我们将看到如何将一个Spring Boot应用程序作为另一个项目的依赖项。

2. Spring Boot Packaging

2.Spring Boot包装

The Spring Boot Maven and Gradle plugins both package our application as executable JARs – such a file can’t be used in another project since class files are put into BOOT-INF/classes. This is not a bug, but a feature.

Spring Boot的Maven和Gradle插件都将我们的应用程序打包成可执行的JAR–这样的文件不能在另一个项目中使用,因为类文件被放在BOOT-INF/classes。这不是一个错误,而是一个特点。

In order to share classes with another project, the best approach to take is to create a separate jar containing shared classes, then make it a dependency of all modules that rely on them.

为了与另一个项目共享类,最好的办法是创建一个包含共享类的独立jar,然后将其作为所有依赖这些类的模块的依赖项。

But if that isn’t possible, we can configure the plugin to generate a separate jar that can be used as a dependency.

但如果这不可能,我们可以配置该插件,使其生成一个单独的jar,可以作为一个依赖项。

2.1. Maven Configuration

2.1.Maven配置

Let’s configure the plugin with a classifier:

让我们用一个分类器来配置这个插件。

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

Though, the configuration for Spring Boot 1.x would be a little different:

虽然,Spring Boot 1.x的配置会有一些不同。

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

This will create two jars, one with the suffix exec as an executable jar, and another as a more typical jar that we can include in other projects.

这将创建两个罐子,一个以exec为后缀,作为可执行的罐子,另一个作为更典型的罐子,我们可以包含在其他项目中。

3. Packaging with Maven Assembly Plugin

3.用Maven汇编插件打包

We may also use the maven-assembly-plugin to create the dependent jar:

我们也可以使用maven-assembly-plugin来创建依赖jar。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

If we use this plugin along with the exec classifier in spring-boot-maven-plugin, it will generate three jars. The first two will be the same we saw previously.

如果我们把这个插件和exec分类器一起用在spring-boot-maven-plugin中,它将生成三个jars。前两个将与我们之前看到的一样。

The third will have whatever suffix we specified in the <descriptorRef> tag and will contain all the project’s transitive dependencies. If we include it in another project, we won’t need to separately include Spring dependencies.

第三个将具有我们在<descriptorRef>tag中指定的任何后缀,并且将包含项目的所有横向依赖。如果我们把它包含在另一个项目中,我们就不需要再单独包含Spring的依赖。

4. Conclusion

4.总结

In this article, we showed a couple of approaches to packaging a Spring Boot application for use as a dependency in other Maven projects.

在本文中,我们展示了几种打包Spring Boot应用程序的方法,以便在其他Maven项目中作为依赖项使用。

As always, the code backing the article is available over on GitHub.

像往常一样,支持该文章的代码可在GitHub上获得