Thursday, August 20, 2009

Using Eclipse With Large Code Bases, Part I

The project I'm currently working on has more than a million lines of source code. Some of the code was written as long ago as 1998, so as odd as it sounds to call anything involving Java "legacy", this is a legacy Java codebase.

I wanted to bring the code into Eclipse, but not as one giant million-line project. Instead, I wanted to break it up into smaller projects.

But there were complications:
  1. The source tree has a single root directory
  2. Eclipse can't nest projects
  3. Some source files used throughout the code are located in the top of the source tree
  4. Packages and layers have mutual dependencies (for example, business logic in the UI layer), but Eclipse treats cycles among projects as compile errors
  5. JAXB is used to generate some .class files into a runtime directory "rt", but that same directory contains all of the .class files for the system
  6. None of this can be changed, at least not any time soon
Items #1 and #2 mean that the Eclipse projects have to be located outside the source tree, and point to source in the source tree.

Fortunately, Eclipse supports linked source, so I cre
ated the Eclipse projects in a different location, and set their build paths to have linked-source entries that pointed to the source tree.

The first step is to create a global linked-resource variable that points to the root of the source tree:


Then right-click on each project and select
Build Path → Configure Build Path... Link Source... → Variables..., and select the global linked-resource variable.

Saving the changes results in a .classpath entry like this:
<classpathentry kind="src" path="src"/>
and a .project entry like this:
<linkedResources>
<link>
<name>src</name>
<type>2</type>
<locationURI>src</locationURI>
</link>
</linkedResources>
for each project.

(
Because I had so many projects to manage, I edited the .project files directly, instead of interactively.)

Unfortunately, item #1 complicated linking to the source, because every Eclipse project linked to the same root directory, which meant every Eclipse project saw the same source instead of just seeing the source for that project.

For example, if the source tree looks like:
C:\src\
com\
parent\
Parent.java
child1\
Child1.java
child2\
Child.java
and we want two Eclipse projects, "Child1" and "Child2", they both have to start their source trees at C:\src. So Child1 sees Child2's code in the child2\ directory, and Child2 sees Child1's code in the child1\ directory:


Fortunately, Eclipse supports source exclusion. To exclude source, right-click on a project and select Build Path → Configure Build Path... Source, Excluded Edit... Exclusion Patterns: Add..., and add every package and/or file you don't want included.

This modifies the .classpath files to have entries like:
<classpathentry kind="src" path="src"
excluding="com/parent/child1/"/>
Using this approach, I was able to exclude unwanted packages from each project. Because there are a lot of packages, this was tedious and took hours, but it worked:


Unfortunately, once everything was configured and I launched Eclipse, it took 35 minutes to load.

35. Minutes.

It turned out that Eclipse bogs down if it has to import a lot of source code and then filter it out. Ideally it would filter it out first and only load the remainder, but it doesn't seem to do that.

After reporting this problem
, I switched to plan B, which is described in Part II.


No comments:

Post a Comment