Maven 不包括 LWJGL 安装的清单属性
Maven not including manifest attributes for LWJGL install
我正在尝试使用 Maven 设置 LWJGL 项目。我正在使用来自 the official website.
的示例 "getting started" 源代码
这包括访问 LWJGL 清单属性的几行,例如简单的版本检查:
System.out.println("Hello LWJGL " + Version.getVersion() + "!");
这在 Eclipse 环境中运行没有任何问题(当然是在使用 Maven 构建项目之后),但是当 运行 clean install
然后 运行 **-jar-with-dependencies.jar
通过 cmd,抛出以下异常:
java.lang.NullPointerException
at org.lwjgl.system.APIUtil.apiGetManifestValue(APIUtil.java:97)
at org.lwjgl.Version.getVersion(Version.java:33)
at HelloWorld.run(HelloWorld.java:43)
at HelloWorld.main(HelloWorld.java:130)
这是因为 APIUtil
创建的 Manifest
对象不包含任何属性 - 但仅在 Maven 构建的版本中 .
这是为什么?是我的 pom.xml 有问题,还是 LWJGL 3.0.0 还没有准备好?
这是我的 pom.xml
:
<properties>
<mainClass>HelloWorld</mainClass>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<finalName>${project.artifactId}-${project.version}.jar</finalName>
</properties>
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-platform</artifactId>
<version>3.0.0</version>
<classifier>natives-windows</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-platform</artifactId>
<version>3.0.0</version>
<classifier>natives-linux</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-platform</artifactId>
<version>3.0.0</version>
<classifier>natives-osx</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
发生此错误是因为 LWJGL 3.0.0 is looking inside the Manifest a property called "Implementation-Version"
,但是当您制作 uber-jar 时,此 属性 未设置。
这并不是你制作 uber-jar 的真正问题:maven-assembly-plugin
创建的清单看起来像:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Me
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_102
Main-Class: HelloWorld
您可以在 jar-with-dependencies
的 META-INF/MANIFEST.MF
内看到它。此文件没有 "Implementation-Version"
属性。这是正常的:当创建这个可执行 JAR 时,所有依赖项的所有 MANIFEST 都被(理所当然地)忽略了,只生成一个包含 "Main-Class"
的清单,这样 JAR 就可以执行了。
uber-jar 不能包含每个依赖项清单中的内容。例如,"Implementation-Version"
是一个 属性,它出现在多个库的清单中,那么它应该保留哪个? (最后只能有一个清单,在 uber-jar 中)。所以问题出现了,因为我们正在制作一个可执行 JAR,它只能有 1 个清单,所以它不能聚合每个依赖项清单中的所有属性。
有 2 种可能的解决方案:
- 忽略它。毕竟,这并不是真正的错误。
不要通过将所有依赖项嵌入单个 JAR 来制作可执行 jar,而是创建一个 ZIP 程序集,每个依赖项都放在 lib
文件夹中:这样,每个 Manifest 都会被保存。这是通过告诉 maven-jar-plugin
添加主 class 的清单条目并添加 class 路径并创建自定义程序集描述符来完成的。
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>/path/to/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
其中 /path/to/assembly.xml
是程序集描述符的路径,相对于 POM 的位置,是:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>dist</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
</dependencySet>
</dependencySets>
</assembly>
使用这样的配置,运行 mvn clean install
将创建一个 ZIP 文件 artifactId-version-dist.zip
。解压它并 运行(将 <finalName>
替换为你的 JAR 的 finalName
)
java -jar lib\<finalName>.jar
将毫无问题地打印该版本。
我正在尝试使用 Maven 设置 LWJGL 项目。我正在使用来自 the official website.
的示例 "getting started" 源代码这包括访问 LWJGL 清单属性的几行,例如简单的版本检查:
System.out.println("Hello LWJGL " + Version.getVersion() + "!");
这在 Eclipse 环境中运行没有任何问题(当然是在使用 Maven 构建项目之后),但是当 运行 clean install
然后 运行 **-jar-with-dependencies.jar
通过 cmd,抛出以下异常:
java.lang.NullPointerException
at org.lwjgl.system.APIUtil.apiGetManifestValue(APIUtil.java:97)
at org.lwjgl.Version.getVersion(Version.java:33)
at HelloWorld.run(HelloWorld.java:43)
at HelloWorld.main(HelloWorld.java:130)
这是因为 APIUtil
创建的 Manifest
对象不包含任何属性 - 但仅在 Maven 构建的版本中 .
这是为什么?是我的 pom.xml 有问题,还是 LWJGL 3.0.0 还没有准备好?
这是我的 pom.xml
:
<properties>
<mainClass>HelloWorld</mainClass>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<finalName>${project.artifactId}-${project.version}.jar</finalName>
</properties>
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-platform</artifactId>
<version>3.0.0</version>
<classifier>natives-windows</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-platform</artifactId>
<version>3.0.0</version>
<classifier>natives-linux</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-platform</artifactId>
<version>3.0.0</version>
<classifier>natives-osx</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
发生此错误是因为 LWJGL 3.0.0 is looking inside the Manifest a property called "Implementation-Version"
,但是当您制作 uber-jar 时,此 属性 未设置。
这并不是你制作 uber-jar 的真正问题:maven-assembly-plugin
创建的清单看起来像:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Me
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_102
Main-Class: HelloWorld
您可以在 jar-with-dependencies
的 META-INF/MANIFEST.MF
内看到它。此文件没有 "Implementation-Version"
属性。这是正常的:当创建这个可执行 JAR 时,所有依赖项的所有 MANIFEST 都被(理所当然地)忽略了,只生成一个包含 "Main-Class"
的清单,这样 JAR 就可以执行了。
uber-jar 不能包含每个依赖项清单中的内容。例如,"Implementation-Version"
是一个 属性,它出现在多个库的清单中,那么它应该保留哪个? (最后只能有一个清单,在 uber-jar 中)。所以问题出现了,因为我们正在制作一个可执行 JAR,它只能有 1 个清单,所以它不能聚合每个依赖项清单中的所有属性。
有 2 种可能的解决方案:
- 忽略它。毕竟,这并不是真正的错误。
不要通过将所有依赖项嵌入单个 JAR 来制作可执行 jar,而是创建一个 ZIP 程序集,每个依赖项都放在
lib
文件夹中:这样,每个 Manifest 都会被保存。这是通过告诉maven-jar-plugin
添加主 class 的清单条目并添加 class 路径并创建自定义程序集描述符来完成的。<plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <mainClass>${mainClass}</mainClass> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <descriptors> <descriptor>/path/to/assembly.xml</descriptor> </descriptors> </configuration> </plugin>
其中
/path/to/assembly.xml
是程序集描述符的路径,相对于 POM 的位置,是:<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> <id>dist</id> <formats> <format>zip</format> </formats> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <useProjectArtifact>true</useProjectArtifact> </dependencySet> </dependencySets> </assembly>
使用这样的配置,运行
mvn clean install
将创建一个 ZIP 文件artifactId-version-dist.zip
。解压它并 运行(将<finalName>
替换为你的 JAR 的finalName
)java -jar lib\<finalName>.jar
将毫无问题地打印该版本。