pom文件配置打包外部/自定义jar文件在fat spring boot可执行jar文件中

Posted

技术标签:

【中文标题】pom文件配置打包外部/自定义jar文件在fat spring boot可执行jar文件中【英文标题】:Pom file configuration to package external/custom jar file in the fat spring boot executable jar file 【发布时间】:2021-06-24 02:04:19 【问题描述】:

我的要求:

我正在尝试创建一个依赖于外部/自定义 jar 文件的 Spring Boot Web 项目。我在 pom.xml 文件中添加了这个依赖项,如下所示:

<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>customjar</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>$basedir/lib/customjar.jar</systemPath>
  </dependency>
</dependencies>

我在编译期间没有看到任何错误。但是当我正在构建项目并且它正在生成一个可执行的 fat jar 文件时。此 jar 文件具有其他依赖项,但没有添加为 "System scope" 并且运行时它抛出 ClassNotFoundException。

谁能告诉我我们必须在 pom.xml 文件中添加什么配置,以便系统范围 jar 文件也将添加到可执行 jar 文件中。

【问题讨论】:

【参考方案1】:

阅读spring-boot-maven-plugin 的文档可能会有所帮助:您似乎可以将&lt;includeSystemScope&gt;true&lt;/includeSystemScope&gt; 添加到插件的配置中以包含所述范围。

或者,您可以尝试先安装 jar install:install-file 目标:

最好在你的 spring-boot 项目之前的 pom 中完成。 如果您的构建在同一个反应器中,最好有一个从 spring-boot 项目到执行 install-file 的项目的 provided 依赖项。

sanjeevRm 的答案可能有效,但您必须确保布局正确:安装文件目标可以通过提供 localRepositoryPath 为您做到这一点。

<repositories>
    <repository>
        <id>local-project-repo</id>
        <url>file:$project.basedir/lib/repository</url>
    </repository>
</repositories>

您的文件必须放在:$project.basedir/lib/repository/com/example/customjar/1.0/customjar-1.0.jar

【讨论】:

【参考方案2】:

您可以在 pom 中添加以下内容并将工件放置在此存储库中

<repositories>
    <repository>
        <id>local-project-repo</id>
        <url>file:$basedir/lib/repository</url>
    </repository>
</repositories>

您还可以检查导入范围 documentation.

【讨论】:

以上是关于pom文件配置打包外部/自定义jar文件在fat spring boot可执行jar文件中的主要内容,如果未能解决你的问题,请参考以下文章

2017.5.1 使用fat jar插件来打包有引用外部jar包的项目

如何使用fat-jar打包jar文件啊

fat jar怎么用

使用 Sbt-native-packager 将 fat.jar + 外部文件创建到单个 tar.gz 文件中以在我的 Nexus 上发布

自定义jar配置文件问题?

maven库中有jar包,但是引入不到,可以直接把包下载下来