Java Magazine, July/August 2018
ORACLE COM JAVAMAGAZINE JULY AUGUST 2018 61 libraries Modularizing Your Library The final step into the future that Java 9 brought is to turn your projects JAR file into a modular JAR file Thats actually fairly simple create a module declaration a file module info java in your projects root source folder and use it to define your librarys module name dependencies exports and services for example module name requires module exports package uses service provides service with provider Ill go through the diferent properties shortly but before I do I want to discuss what else you need to do nothing If your tools are up to date they will automatically detect that youre building a module and do the right thing IDEs and build tools will include the module declaration in the list of files to compile put dependencies on the module path and package the resulting module info class into the JAR file theyre building There usually isnt anything explicit you need to do With that said Ill turn to the details of modularizing your library Note that this is not a module system tutorial I expect you to know how the module system works If you dont read up on it in this article Ed Picking a module name Just like package names module names should be globally unique The easiest way to achieve that is to take the same approach as with package names pick a domain that is associated with the project and reverse it If this results in your module name being a prefix of your package names you did it right Declaring dependencies By default each nontest dependency in your build configuration should result in a requires directive in the module declaration Note that you can also have
You must have JavaScript enabled to view digital editions.