我有以下 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>
组装.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
我的罐子捆绑如下:
-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 errosr,因为库没有加载。
我想错误的是我在清单文件中的类路径。应该是什么?
所以, 事实证明你有点做不到。
我的解决方案如下:
更新程序集.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>
聚 甲醛:
<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 捆绑在里面并自动加载它,我基本上需要修改我的代码以拥有一个读取该文件的自定义类加载器。