Search code examples
springgradlejava-9java-platform-module-system

Gradle with Java 10 (Spring able to scan module classes even when package not exported)


So I am playing around with Spring and Java 10 along with Gradle.

In my application, I created 2 modules

  • UI
  • Application

The application module's gradle file looks like this

dependencies {
    compile project (':ui')
}

And module-info file looks like this

module application {
    requires spring.context;
    requires spring.boot.autoconfigure;
    requires spring.boot;
}

In my UI module, the build.gradle looks like this

dependencies {
    compile('com.vaadin:vaadin-spring-boot-starter')
}

dependencyManagement {
    imports {
        mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}"
    }
}

And module-info looks like this

module ui {
    requires vaadin.server;
    requires vaadin.spring;
}

So my UI code lives inside ui module. As you can see, I am not exporting any package outside from the ui module and application's module-info also doesn't require ui.

In the main class I added

@ComponentScan("com.factory_manager")

And magically it's able to scan the UI module and the vaadin UI classes and I am able to access them via their respective endpoints.

From my understanding, the application module should not be able to read ui module's classes as I am not exporting any package outside.

Does anyone has any idea on how spring is magically able to scan the ui module and find the ui classes?

And yes, I am unable to access UI module's classes inside my application module but spring somehow is able to..


Solution

  • Not very gradle specific, yet a probable reason for this could be your dependency code -

    dependencies {
        compile project (':ui')
    } 
    

    Due to which the ui module classes are present on the classpath instead of the module path and are under the unnamed module.

    To correlate further, the unnamed module exports all of its packages and hence you're able to access those classes without exporting them.

    Top of my mind, one way you can confirm this is, by trying out something like -

    <yourClassFromUIModule>.class.getModule().isNamed(); //should return false