如何使用外部 jar 库构建 maven 可执行 jar?
How to build an maven executable jar with external jar library?
我正在尝试基于 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。我正在使用这个 .
但是在 运行 的时候,当我尝试执行一个方法时,使用这个外部库,我得到了 java.lang.NoClassDefFoundError
和我的 class 的名字我的外部图书馆。
那么我如何构建一个使用本地存储中的外部 jar 库的构建?
所以我找到了在最终 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>
我正在尝试基于 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。我正在使用这个
但是在 运行 的时候,当我尝试执行一个方法时,使用这个外部库,我得到了 java.lang.NoClassDefFoundError
和我的 class 的名字我的外部图书馆。
那么我如何构建一个使用本地存储中的外部 jar 库的构建?
所以我找到了在最终 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>