How to modularize a (large) Java App?

I have a rather large (several MLOC) application at hand that I'd like to split up into more maintainable separate parts. Currently the product is comprised of about 40 Eclipse projects, many of them having inter-dependencies. This alone makes a continuous build system unfeasible, because it would have to rebuild very much with each checkin.

Is there a "best practice" way of how to

  • identify parts that can immediately be separated
  • document inter-dependencies visually
  • untangle the existing code
  • handle "patches" we need to apply to libraries (currently handled by putting them in the classpath before the actual library)
  • If there are (free/open) tools to support this, I'd appreciate pointers.

    Even though I do not have any experience with Maven it seems like it forces a very modular design. I wonder now whether this is something that can be retrofitted iteratively or if a project that was to use it would have to be layouted with modularity in mind right from the start.

    Edit 2009-07-10

    We are in the process of splitting out some core modules using Apache Ant/Ivy. Really helpful and well designed tool, not imposing as much on you as maven does.

    I wrote down some more general details and personal opinion about why we are doing that on my blog - too long to post here and maybe not interesting to everyone, so follow at your own discretion: www.danielschneller.com


    Using OSGi could be a good fit for you. It would allow to create modules out of the application. You can also organize dependencies in a better way. If you define your interfaces between the different modules correctly, then you can use continuous integration as you only have to rebuild the module that you affected on check-in.

    The mechanisms provided by OSGi will help you untangle the existing code. Because of the way the classloading works, it also helps you handle the patches in an easier way.

    Some concepts of OSGi that seem to be a good match for you, as shown from wikipedia:

    The framework is conceptually divided into the following areas:

  • Bundles - Bundles are normal jar components with extra manifest headers.
  • Services - The services layer connects bundles in a dynamic way by offering a publish-find-bind model for plain old Java objects(POJO).
  • Services Registry - The API for management services (ServiceRegistration, ServiceTracker and ServiceReference).
  • Life-Cycle - The API for life cycle management (install, start, stop, update, and uninstall bundles).
  • Modules - The layer that defines encapsulation and declaration of dependencies (how a bundle can import and export code).
  • Security - The layer that handles the security aspects by limiting bundle functionality to pre-defined capabilities.

  • First: good luck & good coffee. You'll need both.

    I once had a similiar problem. Legacy code with awful circular dependencies, even between classes from different packages like org.example.pkg1.A depends on org.example.pk2.B and vice versa.

    I started with maven2 and fresh eclipse projects. First I tried to identify the most common functionalities (logging layer, common interfaces, common services) and created maven projects. Each time I was happy with a part, I deployed the library to the central nexus repository so that it was almost immediately available for other projects.

    So I slowly worked up through the layers. maven2 handled the dependencies and the m2eclipse plugin provided a helpful dependency view. BTW - it's usually not too difficult to convert an eclipse project into a maven project. m2eclipse can do it for you and you just have to create a few new folders (like src/main/java) and adjust the build path for source folders. Takes just a minute or two. But expect more difficulties, if your project is an eclipse plugin or rcp application and you want maven not only to manage artifacts but also to build and deploy the application.

    To opinion, eclipse, maven and nexus (or any other maven repository manager) are a good basis to start. You're lucky, if you have a good documentation of the system architecture and this architecture is really implemented ;)


    I had a similar experience in a small code base (40 kloc). There are no °rules":

  • compiled with and without a "module" in order to see it's usage
  • I started from "leaf modules", modules without other dependencies
  • I handled cyclic dependencies (this is a very error-prone task)
  • with maven there is a great deal with documentation (reports) that can be deployed in your CI process
  • with maven you can always see what uses what both in the site both in netbeans (with a
    very nice directed graph)
  • with maven you can import library code in your codebase, apply source patches and compile with your products (sometimes this is very easy sometimes it is very difficult)
  • Check also Dependency Analyzer: http://www.javalobby.org/servlet/JiveServlet/download/17-96956-92153685-7907/screenshot.jpg

    Netbeans:

    http://dm.zimmer428.net/wp-content/uploads/2009/02/67pre-dependency-graph.jpg

    链接地址: http://www.djcxy.com/p/30680.html

    上一篇: 如何使代码模块化?

    下一篇: 如何模块化(大型)Java应用程序?