javagradlesubproject

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


Exactly what the title says. For the record, I am using OpenJDK 15.0.1 and Gradle 6.7. I am trying to run code in a subproject with dependencies. When I try, I get a compiler error like so:

> 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;
                          ^

As far as I'm concerned, the dependencies in question (JavaFX, CSSBox) are already specified under the root build.gradle file. Why does the compiler overlook these dependencies, and how do I fix it?

For reference, here's my root 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()
}

and the subproject's build.gradle:

plugins {
    id 'application'
}

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

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform()
}

Solution

  • It turns out that

    java {
      modularity.inferModulePath = true;
    }
    

    needs to be set for all subprojects individually. Specifically, you need to place it in the allprojects closure.