运行 具有外部 class 路径的可执行 JAR

Run an executable JAR with external class path

我使用 Maven 将我的项目编译成一个 JAR,其中包含除一个大依赖项之外的所有依赖项。依赖项的包含是使用:

<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.jarBigExternalJar.jar 在同一文件夹中,但是当我尝试 运行:

java -cp ".:BigExternalJar.jar:myjar.jar" -jar myjar.jar

我因为缺少 类 而得到一个例外(那些 类 来自 BigExternalJar.jar)。

如何将依赖项打包到 JAR 中,仅使用 Maven,但仍然能够在类路径中添加其他 JAR?请注意,BigExternalJar 并不总是在同一文件夹中,因此我无法将其手动添加到 MANIFEST 文件中。

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 选项,类路径参数将被忽略。仅使用清单中提供的类路径。

<build>
    <plugins>
        <!-- compiler插件, 设定JDK版本 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>xxx.xxx.yourmain</mainClass>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

请试试这个~~~所有外部 jar 将在您打包的 jar 中