简体   繁体   English

使用外部类路径运行可执行JAR

[英]Run an executable JAR with external class path

Using Maven I compiled my project into a JAR that includes all the dependencies except for one big dependecy. 使用Maven,我将项目编译到一个JAR中,该JAR包含除了一个大依赖项以外的所有依赖项。 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>

Exclusion of dependencies is done with <scope>provided</scope> 排除依赖项是通过<scope>provided</scope>

The target myjar.jar is in the same folder as BigExternalJar.jar , but when I try to run: 目标myjar.jar是在同一文件夹中BigExternalJar.jar ,但是当我尝试运行:

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

I get an exception for missing classes (those classes are from BigExternalJar.jar ). 对于缺少的类,我得到了一个例外(这些类来自BigExternalJar.jar )。

How can one pack dependencies into a JAR, using Maven only , but still be able to add additional JARs in classpath? 如何仅使用Maven将依赖项打包到JAR中,但仍然能够在classpath中添加其他JAR? Note that the BigExternalJar is not always in the same folder so I cannot add it manually to the MANIFEST file. 请注意, 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? Eclipse:如何使用外部jar构建可执行jar? AND Running a executable JAR with external dependencies AND 运行具有外部依赖项的可执行JAR

The classpath argument is ignored if you use the -jar option. 如果使用-jar选项,则忽略classpath参数。 Only the classpath provided in the manifest is used. 仅使用清单中提供的类路径。

<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>

pls try this~~~ all external jar will be in your packaged jar 请尝试一下~~~所有外部罐子都将放在包装好的罐子中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM