1

我有一个相对简单的模块化项目,我已经设置了我在文档以及不同答案或文章中找到的所有选项,例如如何强制 gradle 将依赖项添加到模块路径而不是eclipse中的类路径?和别的。

我收到一个未找到模块的异常:

java.lang.module.FindException: Module gdx.backend.lwjgl not found, required by com.myproject.client

我只在通过 gradle 的 application:run 任务运行我的应用程序时遇到此异常。当我在 IntelliJ 中运行它或使用 Badass JLink 插件生成的运行时映像时,一切运行正常。经过数小时的手动调试和构建命令行后,我能够弄清楚正确启动的程序与 gradle 所做的事情之间的区别在于,其他一切都使用 --module-path 选项启动程序,而 Gradle 坚持生成无论我在 build.gradle 文件中做什么,都遵循命令行:

Starting process 'command 'C:\Tools\java\bin\java.exe''. Working directory: C:\repos\pv-core\master\pv-client Command: C:\Tools\java\bin\java.exe -Dfile.encoding=windows-1252 -Duser.country=FR -Duser.language=fr -Duser.variant -cp C:\Users\accou\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-backend-lwjgl\1.9.11\3c094feb74e2aef14e30e988326612ee75387c8f\gdx-backend-lwjgl-1.9.11.jar;[...] --module com.myproject.client/com.myproject.client.ClientStarter
Successfully started process 'command 'C:\Tools\java\bin\java.exe''
Error occurred during initialization of boot layer
java.lang.module.FindException: Module gdx not found, required by com.myproject.client

我的 gradle 文件包含以下内容:

根文件:

subprojects {
    apply plugin: "java"

    java {
        modularity.inferModulePath = true
    }

    targetCompatibility = '11'
    sourceCompatibility = '11'

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
        google()
    }

    test {
        useJUnitPlatform()

        testLogging {
            events 'PASSED', 'FAILED', 'SKIPPED'
        }
    }

    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
        testImplementation 'org.junit.jupiter:junit-jupiter-params:5.3.1'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
    }

    version = '0.0.1'

    ext {
        appName = 'MyProject'
        gdxVersion = '1.9.11'
        box2DLightsVersion = '1.4'
        aiVersion = '1.8.0'
        artemisVersion = '2.3.0'
        junitVersion = '5.4.2'
        slf4jVersion = '1.8.0-beta2'
    }
}

project(':pv-core') {
    dependencies {
    }
}

客户项目(应用):

plugins {
    id 'org.beryx.jlink' version '2.22.3'
    id 'application'
}

jlink {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher{
        name = 'hello'
        jvmArgs = ['-Dlog4j.configurationFile=./log4j2.xml']
    }
}

ext.moduleName = 'com.myproject.client'
group = 'com.myproject.client'
//mainClassName = 'com.myproject.client.PVClientStarter'

application {
    mainModule = 'com.myproject.client'
    mainClass = 'com.myproject.client.ClientStarter'
}
apply plugin: 'java-library'

dependencies {
    api "com.badlogicgames.gdx:gdx:$gdxVersion"
    api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
    api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    api "com.esotericsoftware.spine:spine-libgdx:3.6.53.1"
    api "com.underwaterapps.overlap2druntime:overlap2d-runtime-libgdx:0.1.0"
    api "com.kotcrab.vis:vis-ui:1.3.0"
    api "net.dermetfan.libgdx-utils:libgdx-utils:0.13.4"
    //api "de.tomgrill.gdxfacebook:gdx-facebook-core:1.4.1"
    //api "de.tomgrill.gdxdialogs:gdx-dialogs-core:1.2.5"
    api "com.github.czyzby:gdx-kiwi:1.9.1.9.6"
    api "com.github.czyzby:gdx-lml:1.9.1.9.6"
    api "com.github.czyzby:gdx-lml-vis:1.9.1.9.6"
    api "de.golfgl.gdxgamesvcs:gdx-gamesvcs-core:1.0.2"
    compile "com.badlogicgames.gdx:gdx-ai:$aiVersion"
    compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
    //compile "com.esotericsoftware:kryo:5.0.0"
    compile "kryonet:kryonet:2.21"
    testCompile 'org.testng:testng:7.3.0'
}

test {
    useTestNG()
    //testLogging.showStandardStreams = true
    testLogging {
        events "passed", "skipped", "failed"
    }
}

compileJava {
    inputs.property("moduleName", moduleName)
    doFirst {
        options.compilerArgs += [
                '--module-path', classpath.asPath
        ]
        classpath = files()
    }
}

关于我如何(最终)告诉 gradle 使用 --module-path 而不是 -cp 的任何想法?!

4

2 回答 2

0

I think I just figured it out randomly. I kept messing with the gradle files and eventually removed the java blocks containing the statements modularity.inferModulePath = true (despite this being the main advice given in the gradle docs....!), moved up the compileJava block above dependencies and the jlink plugin configuration and it ended up working somehow.

Lots of black magic involved in all this stuff, is the feeling I'm left with after a couple of days of fighting vs modules & gradle tbh..

EDIT: actually I didn't address the root cause of the problem, gradle is still using -cp over --module-path, but the changes I just described above here eventually lead to the gradle successfully launching my application, yet STILL with the -cp option. So I'm still very much interested in having gradle do a proper, JPMS-compliant, module launch (instead of a classpath launch).

于 2020-11-23T21:48:44.817 回答
-1

Gradle 引入的用于处理 Java 中的模块的模型是基于推断当前项目代码是否为模块。

这意味着您的应用程序需要在其清单中包含一个module-info或一个条目。Automatic-Module-Name

文档中的更多详细信息。

于 2021-07-15T09:21:29.467 回答