spring-boot:排除对包装的依赖性

San*_*eep 5 java spring maven spring-boot

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

已经尝试过以下内容

我尝试过使用:

<scope>provided</scope>
<optional>true</optional>
Run Code Online (Sandbox Code Playgroud)

罐子里的最后一件神器仍然存在.

还尝试将以下内容添加到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>
Run Code Online (Sandbox Code Playgroud)

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

Cès*_*sar 8

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

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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