Maven shade可执行的超级jar NoClassDefFoundError

时间:2015-11-08 12:00:46

标签: java maven maven-shade-plugin exec-maven-plugin

我正在使用maven shade插件创建一个可执行的uber jar,它依赖于我希望它在包阶段包含的本地系统jar(mobile-dock-support.jar)中的某些类,所以我将shade插件配置为如此

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.2</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>uk.xxx.mobiledock.PCSyncApplication</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>uk.xxx</groupId>
            <artifactId>mobile-dock-support</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.build.directory}/../src/main/libs/mobile-dock-support.jar</systemPath>
        </dependency>
    </dependencies>
</plugin>

似乎打包罐子很好但是当我使用java -jar xxx.jar运行jar时它给了我以下

java.lang.NoClassDefFoundError: uk/xxx/mobiledock/MobileDockException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: uk.xxx.mobiledock.MobileDockException
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"

我还设置了exec maven插件,运行mvn exec:java -Dexec.mainClass=uk.xxx.mobiledock.PCSyncApplication按预期运行程序。

以下是exec maven插件的设置方法

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includePluginDependencies>true</includePluginDependencies>
        <mainClass>uk.xxx.mobiledock.PCSyncApplication</mainClass>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>uk.xxx</groupId>
            <artifactId>mobile-dock-support</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.build.directory}/../src/main/libs/mobile-dock-support.jar</systemPath>
        </dependency>
    </dependencies>
</plugin>

1 个答案:

答案 0 :(得分:0)

好吧然后看来问题是阴影插件不会将系统依赖关系复制到超级jar。有几种“正确”的方法来解决这个问题

  1. 使用mvn install

  2. 将依赖项安装到本地maven存储库
  3. 使用存储库管理器,例如Nexus

  4. 如果你像我一样,并且没有上述两个选项,因为它是一个合作项目,并且没有像Nexus这样的选项,那么我就是为了让它工作而做的

    使用maven依赖项插件将任何系统依赖项复制到相对于打包jar的某个位置,例如。

    <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    <includeArtifactIds>mobile-dock-support</includeArtifactIds>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    将复制的jar作为类路径的一部分添加到uber jar的清单文件中,例如

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.2</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <manifestEntries>
                                <Main-Class>uk.xxx.mobiledock.PCSyncApplication</Main-Class>
                                <Class-Path>lib/mobile-dock-support-${mobile-dock-support.version}.jar</Class-Path>
                            </manifestEntries>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>