Using jdeps to create module descriptors

Once you start breaking down your library JARs into modules, depending on the size of your library, you may have a lot of work to do. It's not straightforward to identify which modules you'll need to require and which you'll need to export. The jdeps tool has another trick up its sleeve. It can look at your JAR files and automatically come up with module descriptors for you to use.

The syntax is as follows:

$ jdeps --generate-module-info <output-location> <path-to-jars>

Let's try this for the commons-collections JAR file:

$ jdeps --generate-module-info out lib/commons-collections4-4.1.jar

The output should look like this:

writing to out/commons.collections4/module-info.java

As you can see, jdeps has generated a module root folder with the same automatic naming algorithm we've seen before. Inside that folder, it has created a module-info.java file that it has populated with the requires and exports declarations that it identified by scanning the classes in the JARs:

    module commons.collections4 {
requires transitive java.xml;
exports org.apache.commons.collections4;
...
}

You can run this command and point to multiple JARs, and it'll do this for every single JAR, which also benefits from any relationships between the JARs. The generated module-info.java files for those related modules will include the relationship too!

Remember to use this feature just as a starting point to define your module definitions. The platform cannot obviously guess the perfect module definition for your library just by looking at the code. It's your job as the author of the library to come up with what it requires and what it encapsulates or exports. There is also a technical limitation here. The jdeps does static code analysis, so it will not be able to catch any runtime reflective access that libraries may perform. If your library is using reflection, you'll need to manually add the exports or opens declarations to the right modules yourself.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141