使用外部类路径运行可执行JAR
使用Maven,我将项目编译到一个JAR中,该JAR包含除了一个大依赖项以外的所有依赖项.依赖关系的包含是使用以下方式完成的:
Using Maven I compiled my project into a JAR that includes all the dependencies except for one big dependecy. The inclusion of the dependecies is done using:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mypackage.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
排除依赖项是通过<scope>provided</scope>
目标myjar.jar
与BigExternalJar.jar
在同一文件夹中,但是当我尝试运行时:
The target myjar.jar
is in the same folder as BigExternalJar.jar
, but when I try to run:
java -cp ".:BigExternalJar.jar:myjar.jar" -jar myjar.jar
对于缺少的类,我会得到一个例外(这些类来自BigExternalJar.jar
).
I get an exception for missing classes (those classes are from BigExternalJar.jar
).
如何仅使用Maven 将一个依赖包打包到一个JAR中,但仍然能够在classpath中添加其他JAR?请注意,BigExternalJar
并不总是位于同一文件夹中,因此我无法将其手动添加到MANIFEST文件中.
How can one pack dependencies into a JAR, using Maven only, but still be able to add additional JARs in classpath? Note that the BigExternalJar
is not always in the same folder so I cannot add it manually to the MANIFEST file.
有两个类似的问题可能看起来很重复,但是它们并不能解决这种情况. Eclipse:如何使用外部jar构建可执行jar? 并且 运行具有外部依赖关系的可执行JAR
There are two similar questions that might look duplicate but they do not have an answer to this situation. Eclipse: How to build an executable jar with external jar? AND Running a executable JAR with external dependencies
如果使用-jar
选项,则将忽略classpath参数.仅使用清单中提供的类路径.
The classpath argument is ignored if you use the -jar
option. Only the classpath provided in the manifest is used.