spring-boot:排除对包装的依赖性 [英] spring-boot : Exclude dependencies on packaging

查看:1703
本文介绍了spring-boot:排除对包装的依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个春季启动项目(项目A),该项目将包含在其他项目中(项目B,项目C ......)。我在项目A中有几个依赖项,但在导入项目A的项目中,可能需要一些或仅一个。
我试图找到一种方法来在打包项目A时排除jar依赖项,以便在运行时由Project B提供所需的依赖项。我希望在项目A独立运行以进行测试时可以使用依赖项。

I am working on a spring boot project ( Project A ) that would be included in other projects ( Project B, Project C ... ) . I have several dependencies in Project A, but in the project importing Project A, some or only one may be required. I am trying to find a way to exclude the jar dependencies while packaging Project A so that the required ones will be provided by Project B during run time. I would like to have the dependencies available when the Project A is run independently for testing purposes.

已尝试以下

我尝试过使用:

<scope>provided</scope>
<optional>true</optional>

最后的工件仍然是罐子。

Still the jars end up in the final artifact.

还尝试将以下内容添加到spring-boot-maven-plugin

Also tried adding the following to the spring-boot-maven-plugin

           <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <excludeArtifactIds>spring-boot-starter-redis</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

这只会删除spring-boot依赖项,但是这个依赖项的子项的jar仍然会结束最终的工件。

This would just remove the spring-boot dependency , but the jars for the children of this dependency would still end up in the final artifact.

推荐答案

在我们当前的项目中,我们需要为应用程序创建一个war文件,必须部署在JEE服务器中。 war文件必须只包含所需的jar文件,不包括JEE服务器已经提供的任何API或实现。

In our current project we have the requirement to create a war file for the application, which has to be deployed in a JEE server. The war file must include only the needed jar files, not including any API or implementation already provided by the JEE server.

但是,我们希望保留生成可执行war或jar文件,默认情况下由Boot提供,用于测试目的。

But, we want to retain the possibility to generate an executable war or jar file as provided by default by Boot, for testing purposes.

为实现这一目标,我们将所有可选的依赖项设置为提供的。例如,我们在开发中使用了一些直接依赖项,比如JDBC驱动程序,我们不希望包含在已部署的war文件中。还有一些启动主启动器,它们与JEE服务器中不需要的其他启动器和库提供依赖关系。这是 spring-boot-starter-tomcat spring-boot-starter-jdbc 启动器的情况。在我们的项目中,我们在 pom.xml 文件中有followind依赖项:

To achieve it, we've set all optional dependencies as provided. For example, we have some direct dependencies used in development, like the JDBC driver, we don't want to include in the deployed war file. Also there are some boot main starters which provide dependencies with other starters and libraries we don't need in a JEE server. This is the case of the spring-boot-starter-tomcat and spring-boot-starter-jdbc starters. In our project, we have the followind dependencies in our pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc7</artifactId>
  <scope>provided</scope>
</dependency>

这样,这些依赖项将不会包含在原始jar / war文件中,而是弹簧启动maven插件会将它们包含在重新打包的jar / war的 lib-provided 文件夹中。

This way those dependencies won't be included in the original jar/war file, but the spring boot maven plugin will include them in the lib-provided folder of the repackaged jar/war.

JEE不会看到这些依赖项服务器,但使打包的应用程序大于需要。解决方案是告诉spring boot maven插件创建具有其他名称的重新打包文件,以及排除开发工具:

Those dependencies won't be seen by the JEE server, but make the packaged application bigger than needed. The solution is to tell the spring boot maven plugin to create the repackaged file with another name, as well as excluding the development tools:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
      <mainClass>${start-class}</mainClass>
      <classifier>exec</classifier>
  </configuration>
</plugin>

这样maven会为你的应用程序生成两个包:

This way maven will generate two packages for your application:


  • 默认的jar / war包,没有提供所有依赖项。

  • 一个重新打包的文件,其名称以_exec.jar / .war结尾,在 lib-provided 文件夹中提供所有依赖项,并支持使用 java -jar文件运行应用程序

  • The default jar/war package, without all the provided dependencies.
  • A repackaged file whose name ends with _exec.jar/.war, with all provided dependencies in the lib-provided folder and the support to run the application with java -jar file

在您的情况下,您可以使用相同的技术来生成要包含在项目B中的项目A的包,以及项目A的包作为独立运行。

In your case you could use the same technique to be able to generate the package for the Project A to be included in Project B, and the package for Project A to be run as standalone.

如果您不需要为项目A创建要自行运行的程序包,并且只在IDE中测试它,您甚至可以从中删除spring boot maven插件你的 pom.xml

If you don't need to create the package for Project A to be run by itself, and only test it in your IDE, you might even remove the spring boot maven plugin from your pom.xml.

这篇关于spring-boot:排除对包装的依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆