ebook img

The Java Module System PDF

440 Pages·2019·10.336 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Java Module System

Nicolai Parlog Foreword by Kevlin Henney M A N N I N G The Java Module System NICOLAI PARLOG Foreword by Kevlin Henney MANNING Shelter ISland 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. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©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 Manning’s 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. Development editor: Marina Michaels 20 Baldwin Road Technical development editor: Jeanne Boyarsky PO Box 761 Review editor: Aleksandar Dragosavljevic´ Shelter Island, NY 11964 Production editor: David Novak Copy editor: Tiffany Taylor Proofreader: Melody Dolab Technical proofreader: Jean-François Morin Typesetter: Happenstance Type-O-Rama Graphics: Viseslav Radovic Cover designer: Marija Tudor ISBN 9781617294280 Printed in the United States of America Don’t it feel right like this All the pieces fall to his wish —To Gabi, to Maia brief contents Part 1 Hello, modules ............................................................ 1 1 ■ First piece of the puzzle 3 2 ■ Anatomy of a modular application 32 3 ■ Defining modules and their properties 50 4 ■ Building modules from source to JAR 85 5 ■ Running and debugging modular applications 103 Part 2 Adapting real-world projects ...............................123 6 ■ Compatibility challenges when moving to Java 9 or later 125 7 ■ Recurring challenges when running on Java 9 or later 142 8 ■ Incremental modularization of existing projects 166 9 ■ Migration and modularization strategies 191 Part 3 Advanced module system features .........................215 10 ■ Using services to decouple modules 217 11 ■ Refining dependencies and APIs 244 12 ■ Reflection in a modular world 270 13 ■ Module versions: What’s possible and what’s not 300 14 ■ Customizing runtime images with jlink 314 15 ■ Putting the pieces together 339 v contents foreword xv preface xvii acknowledgments xix about this book xxi about the author xxix about the cover illustration xxx Part 1 Hello, modules .............................................1 1 First piece of the puzzle 3 1.1 What is modularity all about? 4 Visualizing software as graphs 5 ■ The impact of design principles 6 ■ What modularity is all about 7 1.2 Module erasure before Java 9 8 1.3 Complications before Java 9 12 Unexpressed dependencies between JARs 12 ■ Shadowing classes with the same name 13 ■ Conflicts between different versions of the same project 15 ■ Complex class loading 16 ■ Weak encapsulation across JARs 16 ■ Security checks have to be handcrafted 17 ■ Poor startup performance 18 ■ Rigid Java runtime 18 vii vviiiiii contents 1.4 Bird’s-eye view of the module system 18 Everything is a module 19 ■ Your first module 21 ■ The module system in action 21 ■ Your non-modular project will be fine— mostly 24 1.5 Goals of the module system 26 Reliable configuration: Leaving no JAR behind 26 ■ Strong encapsulation: Making module- internal code inaccessible 27 ■ Automated security and improved maintainability 28 ■ Improved startup performance 28 ■ Scalable Java platform 28 ■ Non-goals 29 1.6 Skills, old and new 29 What you’ll learn 29 ■ What you should know 30 2 Anatomy of a modular application 32 2.1 Introducing ServiceMonitor 33 2.2 Modularizing ServiceMonitor 37 2.3 Cutting ServiceMonitor into modules 37 2.4 Laying out files in a directory structure 38 2.5 Declaring and describing modules 39 Declaring dependencies on other modules 40 ■ Defining a module’s public API 41 ■ Visualizing ServiceMonitor with the module graph 41 2.6 Compiling and packaging modules 43 2.7 Running ServiceMonitor 44 2.8 Extending a modular code base 45 2.9 Post mortem: Effects of the module system 46 What the module system does for you 46 ■ What else the module system can do for you 47 ■ Allowing optional dependencies 49 3 Defining modules and their properties 50 3.1 Modules: The building blocks of modular applications 51 Java modules (JMODs), shipped with the JDK 51 ■ Modular JARs: Home-grown modules 52 ■ Module declarations: Defining a module’s properties 52 ■ The many types of modules 57 contents iixx 3.2 Readability: Connecting the pieces 60 Achieving reliable configuration 61 ■ Experimenting with unreliable configurations 61 3.3 Accessibility: Defining public APIs 67 Achieving strong encapsulation 69 ■ Encapsulating transitive dependencies 71 ■ Encapsulation skirmishes 72 3.4 The module path: Letting Java know about modules 75 Module resolution: Analyzing and verifying an application’s structure 77 ■ Module graph: Representation of an application’s structure 78 ■ Adding modules to the graph 80 ■ Adding edges to the graph 82 ■ Accessibility is an ongoing effort 83 4 Building modules from source to JAR 85 4.1 Organizing your project in a directory structure 86 New proposal—new convention? 86 ■ Established directory structure 87 ■ The place for module declarations 88 4.2 Compiling a single module 89 Compiling modular code 89 ■ Modular or non-modular? 90 4.3 Compiling multiple modules 92 The naive approach 92 ■ The module source path: Informing the compiler about the project structure 93 ■ The asterisk as a token for the module name 94 ■ Multiple module source path entries 95 ■ Setting the initial module 96 ■ Is it worth it? 97 4.4 Compiler options 98 4.5 Packaging a modular JAR 99 Quick recap of jar 99 ■ Analyzing a JAR 100 ■ Defining an entry point 100 ■ Archiver options 101 5 Running and debugging modular applications 103 5.1 Launching the JVM with modules 104 Specifying the main class 104 ■ If the initial module and main module aren’t the same 105 ■ Passing parameters to the application 106 5.2 Loading resources from modules 107 Resource loading before Java 9 107 ■ Resource loading on Java 9 and later 108 ■ Loading package resources across module boundaries 109

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.