库未加载到可执行 jar 中

Libraries not loaded in executable jar

我有以下maven配置

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
                <release>${java.version}</release>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass>${main.class}</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <classpathLayoutType>repository</classpathLayoutType>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>./lib/*.jar</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

assembly.xml:

<assembly>
    <id>assembly</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <includes>
                <include>com.relax-gaming.casinoworld:casinoworld</include>
            </includes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
            <excludes>
                <exclude>com.relax-gaming.casinoworld:casinoworld</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

我的清单内容是:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: username
Build-Jdk: 11
Specification-Title: project title
Specification-Version: 0.0.1-SNAPSHOT
Implementation-Title: Project title
Implementation-Version: 0.0.1-SNAPSHOT
Implementation-Vendor-Id: groupid
Main-Class: path.to.main.class
Class-Path: ./lib/*.jar

我的jar包如下:

-my.code.package (from src/main/java)
+ META-INF
- MANIFEST.MF  (where the above configuration is)
+ lib
- dep1.jar (all the dependencies as jar files)
-my.resources.file (from src/main/resources)

当我 运行 使用 java -jar 文件时,我得到 NoClassDefFound 错误,因为库未加载。

我想问题出在清单文件中的类路径上。应该是什么?

所以, 事实证明你有点做不到。

我的解决方案如下:

更新 assembly.xml 和 pom.xml 如下:

程序集:

<assembly>
    <id>assembly</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <excludes>
                <exclude>groupId:artifactIdOfResourceIWant</exclude>
            </excludes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>lib-packed</outputDirectory>
            <unpack>false</unpack>
            <includes>
                <include>groupId:artifactIdOfResourceIWant</include>
            </includes>
        </dependencySet>
    </dependencySets>
</assembly>

pom :

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>src/main/assembly/assembly.xml</descriptor>
                        </descriptors>
                        <archive>
                            <manifest>
                                <mainClass>${main.class}</mainClass>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

这基本上可以正常解压所有依赖项,但会将您需要的特定依赖项作为 jar 文件保留在 jar 文件中。这将复杂性降低到只有您需要解压的罐子。

现在,问题仍然存在 Class-Path 只能采用 dir/* 语法从外部加载所有 jar,它不能用于加载 jarfile(即使具有特定名称)所以要将 jar 捆绑在里面并让它自动加载,我基本上需要修改我的代码以拥有一个读取该文件的自定义类加载器。