How to know what module a package is included in?

Viewed 536

I'm not yet too familiar with Java modules, but want to dive in a bit deeper. However, when modularizing an existing application, two questions arise for me:

  1. How do I know what exact modules my application has to require?
  2. How do I know if a certain package is contained in a module and if so, what is its name?

Take the example of a plain, empty Spring Boot project with Spring Reactive Web dependency. By default, only these two classes are imported:

org.springframework.boot.SpringApplication;
org.springframework.boot.autoconfigure.SpringBootApplication;

Now how do I know (1) if these classes are part of any module at all (in the case of Spring they for sure are) and (2) what exact modules I need to require in module-info.java?

1 Answers

If you are using Maven, make sure to use an updated version of the maven-compiler-plugin (like 3.8.1).

Then create the module-info.java in the src/main/java directory. As soon as you create it and try to build it, the compiler itself will tell that you're including a package that is not declared in the module-info.java

You can also use the following command to get the module name of your dependencies:

mvn compile org.apache.maven.plugins:maven-dependency-plugin:3.1.1:resolve -DexcludeTransitive

TLDR: Include the module-info and follow the build process, it will tell you what you need to include.

Related