java spring-boot : 排除对打包的依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37961730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:58:55  来源:igfitidea点击:

spring-boot : Exclude dependencies on packaging

javaspringmavenspring-boot

提问by Sandheep

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.

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

Already tried the following

已经尝试了以下

I have tried using:

我试过使用:

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

Still the jars end up in the final artifact.

罐子仍然最终出现在最终的工件中。

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

还尝试将以下内容添加到 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>

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.

这只会删除 spring-boot 依赖项,但此依赖项的子项的 jar 仍将最终出现在最终工件中。

回答by Cèsar

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 文件,该文件必须部署在 JEE 服务器中。war 文件必须仅包含所需的 jar 文件,不包含 JEE 服务器已提供的任何 API 或实现。

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

但是,为了测试目的,我们希望保留生成 Boot 默认提供的可执行 war 或 jar 文件的可能性。

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-tomcatand spring-boot-starter-jdbcstarters. In our project, we have the followind dependencies in our pom.xmlfile:

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

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

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-providedfolder of the repackaged jar/war.

这样那些依赖就不会包含在原始的 jar/war 文件中,但是 spring boot maven 插件会将它们包含在重新打包的 jar/war的lib-provided文件夹中。

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:

这些依赖项不会被 JEE 服务器看到,但会使打包的应用程序比需要的更大。解决办法是告诉spring boot maven插件重新打包后的文件改名,同时排除开发工具:

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

This way maven will generate two packages for your application:

这样 maven 将为您的应用程序生成两个包:

  • 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-providedfolder and the support to run the application with java -jar file
  • 默认的 jar/war 包,没有提供的所有依赖项。
  • 名称以 _exec.jar/.war 结尾的重新打包文件,在lib-provided文件夹中包含所有提供的依赖项,并支持使用java -jar 文件运行应用程序

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.

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

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.

如果您不需要为项目 A 创建包自行运行,而只在您的 IDE 中对其进行测试,您甚至可以从pom.xml 中删除 spring boot maven 插件。

回答by WeMakeSoftware

inside project B pom.xml you can do the following:

在项目 B pom.xml 中,您可以执行以下操作:

  <dependencies>
    ....
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>projectA</artifactId>

        <exclusions>
            <exclusion>
                <groupId>com.foo.bar</groupId>
                <artifactId>baz</artifactId>
            </exclusion>
            ....
        </exclusions>
    </dependency>
    .....
   </dependencies>