【问题标题】:How to build an maven executable jar with external jar library?如何使用外部 jar 库构建 maven 可执行 jar?
【发布时间】:2018-01-31 17:30:07
【问题描述】:

我正在尝试基于 Maven 构建我的应用程序的 jar 文件。所以我不需要在我的构建中包含外部 jar 库。我需要我的应用程序在运行时从本地 Maven 存储库或本地文件夹中提供此外部依赖项,这些将包含这些外部库。 我为这个依赖配置了我的 pom 文件,如下所示:

<profiles>
        <profile>
            <id>compile</id>
            <dependencies>
                <dependency>
                    <groupId>groupId</groupId>
                    <artifactId>some.artifact</artifactId>
                    <version>1.0</version>
                </dependency>
            </dependencies>
        </profile>

并尝试使用此前缀 -Pcompile 运行此 jar。我正在使用这个answer。

但是在运行时,当我尝试使用这个外部库执行一个方法时,我从外部库中获得了带有我的类名称的java.lang.NoClassDefFoundError。

那么我怎样才能构建一个使用本地存储中的外部 jar 库的构建呢?

【问题讨论】:

  • 构建一个包含所有依赖项的自包含可执行 jar 文件更为常见。使部署变得更加容易和安全。 stackoverflow.com/questions/574594/…
  • @Thilo:如果您在依赖项中有像 Bouncycastle 这样的库,这将不起作用,因为它是一个签名的加密提供程序,并且 jar-with 依赖项会杀死导致 BC 失败的 jar 签名。

标签: java maven jar pom.xml


【解决方案1】:

所以我找到了在最终 maven 构建之外存储 jar 的解决方案。 我只需要将这个 jar 添加到具有正确路径的类路径中。为此,我将其添加到 pom.xml:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>${project.build.finalName}.lib/</classpathPrefix>
                        <mainClass>your.mainClass</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>project-0.0.1-SNAPSHOT.lib/some.lib-1.1.jar</Class-Path>
                        </manifestEntries>

                    </archive>
                </configuration>            
</plugin>

【讨论】:

    猜你喜欢
    • 2010-10-04
    • 2017-03-19
    • 2010-12-21
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多