Spring Boot Configure DevTools in Modular Java

Viewed 1686

I'm new to Java modules. I've been trying to make DevTools work with jigsaw modules but I get the following error

Exception in thread "restartedMain" java.lang.IllegalAccessException: class org.springframework.boot.devtools.restart.RestartLauncher (in module spring.boot.devtools) cannot access class com.thanosfisherman.mancala.MancalaApplication (in module com.thanosfisherman.mancala) because module com.thanosfisherman.mancala does not export com.thanosfisherman.mancala to module spring.boot.devtools

What should I put into my module-info.java file in so that the app can run normally?

module-info.java

 module com.thanosfisherman.mancala {
    requires spring.web;
    requires spring.boot.autoconfigure;
    requires spring.boot;
}

Note that I'm using Gradle. Here is my gradle config script

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.thanosfisherman'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 11

repositories {
    mavenCentral()
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-actuator')
    implementation('org.springframework.boot:spring-boot-starter-web')
    runtimeOnly('org.springframework.boot:spring-boot-devtools')
    runtimeOnly('org.postgresql:postgresql')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

EDIT: I read somewhere that spring uses reflection to read the module file so I have to add the open keyword like so.

open module com.thanosfisherman.mancala {
    requires spring.boot.autoconfigure;
    requires spring.boot;
    requires spring.web;
}

Now My app runs normally without the devtools dependency but again throws a different error with the dependency.

java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration due to javax/sql/DataSource not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)

2 Answers

I had the same issue but on jdk8 (no modules involved) because my main class wasn't public.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
/*public*/ class MyApp {
  public static void main(String[] args) {
    SpringApplication.run(LocationsTest.class, args);
  }
}

That means code on the classpath cannot access this module by default. It needs to be manually added with the --add-modules option of Java 9’s javac.

compileJava {
    options.compilerArgs += ["--add-modules", "spring.boot.devtools"]
}

so add them step by step.

Related