For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity.
Manning Publications Co.
2019 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
Manning Publications Co.
foreword
The motivation and desire for modularity isnt new. In the proceedings of the 1968 NATO Software Engineering Conference, the landmark conference that played a major role in popularizing a vision of software components and the term software engineering, E. E. David outlined an approach for the development of large systems:
Define a subset of the system which is small enough to be manageable, then build on that subsystem. This strategy requires that the system be designed in modules which can be realized, tested, and modified independently, apart from conventions for intermodule communication.
At the same conference, H. R. Gillette described how modularity supported a systems evolution:
Modularity helps to isolate functional elements of the system. One module may be debugged, improved, or extended with minimal personnel interaction or system discontinuity.
Nothing new. It just took languages and practices a while to catch on and explore variations on these themes.
The story of Java modularity is scattered like pieces of a jigsaw puzzle across time and space, carefully coded into history. Javas first and most fundamental realization of a module was the class. In other languages, a class represents a unification of modularity and a type, affording a type, its operations, and its details some privacy and cohesion. Java took this one step further, projecting the class from source artifact into binary component.
Alas, in answering the question How small is small enough to be manageable? the class turns out to be too small. As Marx and Engels observed in 1848:
The history of all hitherto existing society is the history of class struggles.
The Communist Manifesto
For any but the smallest of code bases or most debt-ridden of classes, the class is not the large-scale component architecture you were looking for.
Java also arrived with the false promise of the package, a word with all the right connotationssomething that is sealed and sent, a coherent and wrapped indivisible whole ready to gobut none of the follow-through: namespaces for organizing code into folders, open plan rather than compartmentalized, more indiscreet than discrete, labeled and weighed down by trendy-at-the-time but ultimately impractical domain names.
And then came Pandoras moment, a myth made manifest. The Greek myth of Pandora, the girl with all the gifts, is typically mistold: she is described as opening a box that releases ills on all of humanity. It wasnt a box: it was a jar. What she opened was a pithos, a jar, but this was mistranslated to pyxis, a box. Just as in code, names matter.
JARs were a foot in the door of a component model, but that door had not been opened much beyond zipping class files together. To combat the resulting JAR hell, many approachesperhaps most visibly, build tools and OSGi bundlesextended the JAR model and its manifest to take us further along the road of modularity.
But all thats past. What now? What of the future?
The answer is in front of you. Thats why youre reading this book.
Java 9 brought many of the pieces of the jigsaw puzzle together in its modules, a system woven into the core of the platform rather than an extension beyond it. Javas module system has to negotiate the past. It has to retain the investment in a wealth of existing code, to not mess with an existing ecosystem, yet at the same time offer something that makes sense for code yet to be written in an ever-changing world.
At the mechanical level, you need to understand the nature of a module, the nature of dependencies, and the details of syntax and componentization. From a design perspective, you need to know the good, the bad, and the ugly of working with modules. As with any construct or concept, modularity is not a magic sauce you simply add to your development; it requires care, skill, and attention. You need answers to the question of what happens to your existing code in a more modular world, to the question of how it will affect your deployment and your development, and to the questions you have yet to discover you need answers to.
Thats a lot of questions. Thats why youre reading this book.
Nicolai is here to answer these questions and more. He has tracked modules since they appeared on the radar. He has dived into the depths and swum the shallows of JSRs and implementations. He has gone into details you dont want to, so you dont have to. His care and attention to detail allow you to make good on his distillation of knowledgefrom theory to practice, from entry level to advanced.
This is the book with all the gifts. Open, read, enjoy.
Kevlin Henney, Curbralan
preface
The module system and I met one early morning in April 2015. Before going to work, I checked the OpenJFX mailing list and skimmed a message from a JavaFX user who was concerned about private APIs becoming unavailable due to modularity restrictions. No way, I remember thinking; Java would never undergo such an incompatible change. I wrote it off as a misunderstanding and headed out to work.
There, after lunch, I had a small dispute with a colleague. Nothing consequential, but it left me somewhat disgruntled, so I decided to go home early and enjoy the sunny spring day. Out on the balcony with a cool beer, I needed something to read. But what? Out of curiosity, I started going through the replies to the mail I skimmed that morningand they sucked me right in!
Over the following weeks I devoured every piece of information I could find about Project Jigsaw, the roof under which the module system was being developed. It turned out that the JavaFX users concerns were absolutely justified.
In the beginning, I mostly focused on all the things that might break on Java 9. The potential benefits, on the other hand, were a little less obvious. Fortunately, at the time, I was working on a large Java application, and it slowly began to dawn on me how the module system could be used to improve and maintain its overall structure. More and more pieces fell into place, and after a couple of weeks I was sold on the idea of introducing modules into the ecosystemeven if that meant breaking a few things.