如何在 SpringBoot 中配置额外的类路径?

新手上路,请多包涵

我想制作一个独立的 Web 应用程序。我在使用 SpringBoot 时遇到了一些问题。

我的应用程序是一个来自 SpringBoot 的 jar 文件。

但是我的应用程序通常需要 jdbc 驱动程序 jar。我想为我的应用程序排除 jdbc 驱动程序 jar,并从 lib 文件夹中读取库 jar。

但是SpringBoot lib文件夹是 BOOT-INF/libfinal static 。所以,我想为 jdbc 驱动程序 jar 添加外部类路径 (lib)。

如何在 SpringBoot 中配置额外的类路径。是可用的么?

原文由 fightingmamoru 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 954
1 个回答

您可以使用 loader.path 参数来定义外部 lib 文件夹的位置。此文件夹下的所有 jar 都将添加到类路径中。例如,如果您想将 C:\extLib 定义为您的外部 lib 文件夹,您可以执行以下操作:

 java -Dloader.path=/C:/extLib/ -jar aapName.jar

为此,您需要使用 PropertiesLauncher。有两种方法可以做到这一点:

选项1

更新项目 pom.xml 并添加以下标签:

 <configuration>  <!-- added -->
  <layout>ZIP</layout> <!-- to use PropertiesLauncher -->
</configuration

有效的构建标签,更新后的内容如下所示:

 <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>  <!-- added -->
                <layout>ZIP</layout> <!-- to use PropertiesLauncher -->
            </configuration>
        </plugin>
    </plugins>
</build>

选项 2

从命令行启动应用程序时使用 PropertiesLauncher:

 java -cp aapName.jar -Dloader.path=/C:/extLib/ org.springframework.boot.loader.PropertiesLauncher

参考:

如何使用 jarlauncher 将 jar 添加到 SpringBoot 类路径

原文由 giftednewbie 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题