0

所以我有一个 libgdx 项目,它的内容并不多,但我只想构建一个所有依赖项分开的 jar。

所以我有一个 Maven 构建设置来执行此操作,并且所有 jar 都被构建/复制到它们正确的位置,但是当我尝试从命令行运行 jar 时,我收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/badlogic/gdx/ApplicationListener
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
at java.lang.Class.getMethod0(Class.java:2764)
at java.lang.Class.getMethod(Class.java:1653)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: com.badlogic.gdx.ApplicationListener
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more

所以这个项目的核心 pom.xml 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.game.client</groupId>
        <artifactId>game-client</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>game-client-core</artifactId>
    <packaging>jar</packaging>
    <name>Client Core</name>

    <dependencies>
        <dependency>
            <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx</artifactId>
            <version>${gdx.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.8.4</version>
            <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>io.netty</groupId>
             <artifactId>netty-all</artifactId>
             <version>4.0.7.Final</version>
         </dependency>
         <dependency>
             <groupId>net.onedaybeard.artemis</groupId>
             <artifactId>artemis-odb</artifactId>
             <version>0.3.4</version>
         </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>generate-resources</phase>
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

桌面模块的 pom.xml 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.game.client</groupId>
        <artifactId>game-client</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>game-client-desktop</artifactId>
    <packaging>jar</packaging>
    <name>Client desktop</name>

    <properties>
        <mainClass>com.game.client.java.ClientDesktop</mainClass>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.game.client</groupId>
            <artifactId>game-client-core</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx</artifactId>
            <version>${gdx.version}</version>
        </dependency>

        <dependency>
           <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx-backend-lwjgl</artifactId>
            <version>${gdx.version}</version>
        </dependency>

        <dependency>
            <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx-platform</artifactId>
            <version>${gdx.version}</version>
            <classifier>natives-desktop</classifier>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>../assets</directory>
            </resource>
        </resources>

        <plugins>
            <!-- this run the game when you invoke: mvn integration-test -Pdesktop -->
            <plugin>
                <groupId>com.googlecode.mavennatives</groupId>
                <artifactId>maven-nativedependencies-plugin</artifactId>
                <version>0.0.6</version>
                <executions>
                    <execution>
                        <id>unpacknatives</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                           <goal>copy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <configuration>
                            <target>
                                <java fork="true" classname="${mainClass}" classpathref="maven.test.classpath">
                                    <sysproperty key="java.library.path" value="target/natives"/>
                                </java>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <outputDirectory>${project.build.directory}/${pom.version}</outputDirectory>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>libs/</classpathPrefix>
                            <mainClass>${mainClass}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/${pom.version}/libs</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

最后,这会产生 MANIFEST.MF:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: daniel
Build-Jdk: 1.7.0_25
Main-Class: com.game.client.java.ClientDesktop
Class-Path: libs/game-client-core-1.0-SNAPSHOT.jar libs/netty-all-4.0.7
 .Final.jar libs/artemis-odb-0.3.4.jar libs/gdx-0.9.9-20130916.065150-
 211.jar libs/gdx-backend-lwjgl-0.9.9-20130916.065254-208.jar libs/gdx
 -openal-0.9.9-20130916.065228-208.jar libs/lwjgl-2.9.0.jar libs/lwjgl
 -platform-2.9.0-natives-windows.jar libs/lwjgl-platform-2.9.0-natives
 -linux.jar libs/lwjgl-platform-2.9.0-natives-osx.jar libs/jinput-2.0.
 5.jar libs/jutils-1.0.0.jar libs/jinput-platform-2.0.5-natives-linux.
 jar libs/jinput-platform-2.0.5-natives-windows.jar libs/jinput-platfo
 rm-2.0.5-natives-osx.jar libs/lwjgl_util-2.9.0.jar libs/gdx-platform-
 0.9.9-20130916.065208-209-natives-desktop.jar

我知道有些罐子不需要每个平台都在那里,但这不应该真正超越它的工作。让我担心的是,主要是因为我还没有完全理解 Class-Path 的东西是新行开头的单个空格。这会阻止它加载还是我错过了其他东西?

4

1 回答 1

0

只是预感,但尝试从maven-jar-pluginmaven-dependency-plugin中删除${pom.version}子文件夹的东西(outputDirectory 等)- 因为 maven-nativedependencies-plugin 似乎不支持它。

于 2013-09-22T20:03:12.747 回答