繁体   English   中英

为什么 Java 编译器在 Gradle 子项目中找不到依赖模块?

[英]Why does the Java compiler not find dependency modules in Gradle subprojects?

正是标题所说的。 作为记录,我使用的是 OpenJDK 15.0.1 和 Gradle 6.7。 我正在尝试在具有依赖项的子项目中运行代码。 当我尝试时,我得到一个编译器错误,如下所示:

> Task :browser-core:compileJava FAILED
1 actionable task: 1 executed
(user profile)\IdeaProjects\cssbox-js-browser\browser-core\src\main\java\module-info.java:3: error: module not found: javafx.graphics
    requires javafx.graphics;
                   ^
(user profile)\IdeaProjects\cssbox-js-browser\browser-core\src\main\java\module-info.java:5: error: module not found: net.sf.cssbox
    requires net.sf.cssbox;
                   ^
(user profile)\IdeaProjects\cssbox-js-browser\browser-core\src\main\java\module-info.java:6: error: module not found: net.sf.cssbox.jstyleparser
    requires net.sf.cssbox.jstyleparser;
                          ^

就我而言,有问题的依赖项(JavaFX、CSSBox)已经在根build.gradle文件下指定。 为什么编译器会忽略这些依赖关系,我该如何解决?

作为参考,这是我的根 build.gradle:

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

java {
    modularity.inferModulePath = true
}

allprojects {
    plugins.withType(JavaPlugin, {
        dependencies {
            //determine dependency names
            def os = org.gradle.internal.os.OperatingSystem.current()
            def fxDep = ""
            if (os.isWindows()) {
                fxDep = "win"
            }
            else if (os.isMacOsX()) {
                fxDep = "mac"
            }
            else if (os.isLinux()) {
                fxDep = "linux"
            }

            //implementation 'org.slf4j:slf4j-jdk14:1.7.30'
            implementation 'org.slf4j:slf4j-nop:1.7.30'
            implementation('net.sf.cssbox:cssbox:5.0.0') {
                exclude group: 'xml-apis', module: 'xml-apis'
            }

            implementation "org.openjfx:javafx-base:15.0.1:${fxDep}"
            implementation "org.openjfx:javafx-controls:15.0.1:${fxDep}"
            implementation "org.openjfx:javafx-graphics:15.0.1:${fxDep}"
            implementation "org.openjfx:javafx-fxml:15.0.1:${fxDep}"

            testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
            testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
        }
    })
}

test {
    useJUnitPlatform()
}

和子项目的build.gradle:

plugins {
    id 'application'
}

group 'io.github.jgcodes'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform()
}

事实证明

java {
  modularity.inferModulePath = true;
}

需要为所有子项目单独设置。 具体来说,您需要将它放在allprojects闭包中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM