gradlebuild.gradlejava-11java-platform-module-systemmodule-info

Dependencies('requires' in module-info.java) not resolved when using custom configurations in build.gradle


For the following piece of code, the "requires org.postgresql.jdbc;" is unresolved when trying to perform Gradle-sync/assemble/build and using custom configuration 'driver' and custom sourceSet 'driver'.

build.gradle

plugins {
    id 'java-library'           //used to create java api and implementation separated libs
}

configurations{
    driver
}

dependencies {
    driver group: 'org.postgresql', name: 'postgresql', version: '42.6.0'
}

sourceSets{
    driver{
        java{ srcDirs = ['src'] }
    }
}
compileJava.dependsOn(compileDriverJava)

module-info.java

module database.driver {
    requires java.base;              // core dependencies(internal)
    requires java.sql;               // sql module (externalized) dependencies
    requires org.postgresql.jdbc;    // postgresql driver (internal) dependencies
}

while it is resolved and builds/assembles fine without issues when using the default 'implementation' configuration and default 'main' sourceSet.

build.gradle

plugins {
    id 'java-library'           //used to create java api and implementation separated libs
}

dependencies {
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.6.0'
}

sourceSets{
    main{
        java{ srcDirs = ['src'] }
    }
}
compileJava.dependsOn(compileDriverJava)

Why are the custom configuration dependencies not visible for module-info.java, is this due to the reason that sourceSet 'main' is not used?

NOTE: running ./gradlew dependencies when using custom configuration 'driver' for sourceSets and dependencies task, prints out that dependencies are resolved.

NOTE: compileJava itself doesn't do anything nor has any sourceSets to work with and in-turn doesn't need any dependencies. But it depends on custom configuration compilation 'compileDriverJava'

NOTE: postgres manifest contains Automatic-module-name to denote it is an automatic module

Manifest-Version: 1.0
Automatic-Module-Name: org.postgresql.jdbc

output of './gradlew dependencies' when using custom configuration 'driver'

> Task :dependencies

------------------------------------------------------------
Root project 'database.driver'
------------------------------------------------------------

annotationProcessor - Annotation processors and their dependencies for source set 'main'.
No dependencies

api - API dependencies for source set 'main'. (n)
No dependencies

apiElements - API elements for main. (n)
No dependencies

compileClasspath - Compile classpath for source set 'main'.
No dependencies

compileOnly - Compile only dependencies for source set 'main'. (n)
No dependencies

compileOnlyApi - Compile only API dependencies for source set 'main'. (n)
No dependencies

default - Configuration for default artifacts. (n)
No dependencies

driver
\--- org.postgresql:postgresql:42.6.0
     \--- org.checkerframework:checker-qual:3.31.0

driverAnnotationProcessor - Annotation processors and their dependencies for source set 'driver'.
No dependencies

driverCompileClasspath - Compile classpath for source set 'driver'.
No dependencies

driverCompileOnly - Compile only dependencies for source set 'driver'. (n)
No dependencies

driverImplementation - Implementation only dependencies for source set 'driver'. (n)
No dependencies

driverRuntimeClasspath - Runtime classpath of source set 'driver'.
No dependencies

driverRuntimeOnly - Runtime only dependencies for source set 'driver'. (n)
No dependencies

implementation - Implementation only dependencies for source set 'main'. (n)
No dependencies

mainSourceElements - List of source directories contained in the Main SourceSet. (n)
No dependencies

runtimeClasspath - Runtime classpath of source set 'main'.
No dependencies

runtimeElements - Elements of runtime for main. (n)
No dependencies

runtimeOnly - Runtime only dependencies for source set 'main'. (n)
No dependencies

testAnnotationProcessor - Annotation processors and their dependencies for source set 'test'.
No dependencies

testCompileClasspath - Compile classpath for source set 'test'.
No dependencies

testCompileOnly - Compile only dependencies for source set 'test'. (n)
No dependencies

testImplementation - Implementation only dependencies for source set 'test'. (n)
No dependencies

testResultsElementsForTest - Directory containing binary results of running tests for the test Test Suite's test target. (n)
No dependencies

testRuntimeClasspath - Runtime classpath of source set 'test'.
No dependencies

testRuntimeOnly - Runtime only dependencies for source set 'test'. (n)
No dependencies


Solution

  • okay found the issue, seems gradle DSL cares about sequence of token declaration,

    The issue was that sourceSets was declared after dependencies block and by the time the dependencies block was resolved, there was only one more configuration called driver available in addition to implicit configuration default

    After rearranging the code gradle was happy but the dependencies were still not resolvable by Intellij though they are on the driver sourceSet's runtime classpath (driverImplementation).

    The issue was that I had unknowingly mapped 'driver' sourceSet onto 'main' sourceSet

        driver{
            java{
                srcDirs = ['src/main/java']
            }
        }
    

    Though I haven't declared main sourceSet explicitly, but it is implicitly available and is mapped onto src/main/java which is same as driver's java sourceSet.

    So if I move the sources from src/java/main into src/driver/java and use the following

        driver{
            java{
                srcDirs = ['src/driver/java']
            }
        }
    

    Then IntelliJ is happy and is able to see/resolve the requires modules in module-info.java