The previous chapters focused on migrating applications to the module system. Many of these lessons apply when migrating an existing library as well. Still, several issues affect library migration more than application migration. In this chapter, we identify those issues and their solutions.
The biggest difference between migrating a library and migrating an application is that libraries are used by many applications. These applications may run on different versions of Java, so libraries often need to work across a range of Java versions. It’s not realistic to expect users of your library to switch to Java 9 at the same time your library switches. Fortunately, a combination of new features in Java 9 enables a seamless experience for both library maintainers and users.
The goal is to migrate an existing library in incremental steps to a modular library. You don’t have to be the author of a popular open source project for this to be interesting. If you write code that is shared with other teams in a company, you’re in the same boat.
A migration process for libraries consists of the following steps:
Ensure that the library can run as an automatic module on Java 9.
Compile the library with the Java 9 compiler (targeting your desired minimum Java version), without using new Java 9 features just yet.
Add a module descriptor and turn the library into an explicit module.
Optionally, refactor the structure of the library to increase encapsulation, ...