【问题标题】:spring-boot : Exclude dependencies on packagingspring-boot :排除对打包的依赖
【发布时间】:2016-10-24 00:33:51
【问题描述】:

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

已尝试以下方法

我尝试过使用:

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

罐子最终还是在最终的工件中。

还尝试将以下内容添加到 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 仍将最终出现在最终工件中。

【问题讨论】:

    标签: java spring maven spring-boot


    【解决方案1】:

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

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

    为了实现它,我们将所有可选依赖项设置为提供。例如,我们有一些在开发中使用的直接依赖项,例如 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>
    

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

    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>
    

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

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

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

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

    【讨论】:

    • 我使用了您使用 指定的方法并解决了我的问题。这将创建 artifact-exec.jarartifact.jar 其中 artifact.jar 可以用作库和 artifact-exec .jar 可用于独立运行。清洁解决方案,无需对 pom 文件进行任何破解。谢谢塞萨尔:)
    【解决方案2】:

    在项目 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>
    

    【讨论】:

    • 感谢您的提示。但是我已经知道可以在项目 B 上进行这种排除。我想看看在项目 A 本身的打包过程中是否有办法实现这一点。希望减轻从实施项目中排除不需要的依赖项的负担。带有 标记的 范围不应包含最终工件中的 jar。想检查它是否由 spring-boot-maven-plugin 完成了一些覆盖
    • 好吧,根据docs.spring.io/spring-boot/docs/current/maven-plugin/examples/…,您只排除了工件,而不是它的传递依赖项
    猜你喜欢
    • 2016-08-19
    • 2015-04-28
    • 2017-11-11
    • 2020-10-14
    • 2019-11-25
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    相关资源
    最近更新 更多