Maven shade可执行文件uber jar NoClassDefFoundError

rn0zuynd  于 2023-08-03  发布在  Maven
关注(0)|答案(2)|浏览(163)

我正在使用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>

字符串
它似乎可以很好地打包这个jar,但是当我使用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可以按预期运行程序。
下面是execmaven插件的设置

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

oxf4rvwz

oxf4rvwz1#

好吧,然后它似乎是问题是阴影插件不复制系统依赖到uber jar。有几种“正确”的方法可以解决这个问题
1.使用mvn install将依赖项安装到本地maven存储库
1.使用存储库管理器,如Nexus
但是,如果你和我一样,没有上述两个选项,因为这是一个合作项目,没有像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>

lvjbypge

lvjbypge2#

我相信你的方法可能有效,但它是不正确的。正确的方法是使用install maven插件来构建您拥有的所有第三方依赖项。下面是一个例子,说明你如何实现它:

<dependencies>
        <dependency>
            <groupId>uk.xxx</groupId>
            <artifactId>mobile-dock-support</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.5.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <id>shade-my-jar</id>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                    implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                <resource>META-INF/spring.factories</resource>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>io.Adrestus.Backend.App</Main-Class>
                                </manifestEntries>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <id>id0</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <file>
                            ${project.basedir}/../src/main/libs/mobile-dock-support.jar
                        </file>
                        <groupId>uk.xxx</groupId>
                        <artifactId>mobile-dock-support</artifactId>
                        <version>0.0.1-SNAPSHOT</version>
                        <packaging>jar</packaging>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

字符串

相关问题