Friday, July 31, 2009

Customized ClassPath Contributor- An Eclipse Plug-in

When i first started using java (JDK 1.1), classpath management was an issue for me. Setting up a classpath in command line and then running a java program was so error-prone. Luckily, IDE's, such such as eclipse and netbeans, make a developer's life is more happier. Eclipse (actually eclipse framework) provides various functionalities for fast, productive, efficent, and happy programming, not just for Java, but also many other langues (C/C++, Php, Flex, ...)

Classpath management for java in eclipse , of course, is more easer than bare command line. But it still lacks of some functionalities. Profile based shared repository is one of these missing functionalities. So often in my current work, in eclipse, i find myself arranging classpaths after SVN branching or merging or changing workspace or checking out from SVN. My personal settings during these operations conflicts with other developers settings. There is not any profile (indiviual) based mechanism for settings and classpath ( I am awera that maven provides profile and central reposisty based but our projects has legacy ant scripts)

Therefore, We decided to create a central setting project (Settings) wh
ich contributes automatically and independently to our projects (for example, Beee & Booo) based on developer's profiles. Figure 1 depicts a sample workspace which contains two sample projects (Beee & Booo) and a setting project (Settings) which contains profiles (/Settings/profiles) and central shared libraries (/Settings/libs)


Figure 1: Settings projects and Sample projects (Beee & Booo ) in workspace

Let me talk a bit about Settings project. Setting project is a centralized configuration project and holds shared and indivual settings and libraries for each developer. Therefore, structure of the settings project would vary. But a setting project has following main directories:
  • Settings/libs: Contains common, share libraries
  • Settings/profiles: Holds indivual profiles.
Each profile (ex: deniz.turan) directory under "Settings/profiles" directory holds developer specific settings. Name of profiles directories are same as user's system (windows or linux) login names in order to discover automaticly profiles for developer.

Each profile has following subdirectories which would have specific or custom sub directories (such ants, libs)

  • Settings/profiles/deniz.turan/ants: Holds ant script related indivual properties and custom build scrtips: There are two important file here:
  • Settings/profiles/deniz.turan/libs: Holds profile based libraries. User can create sub library directories (with prefiex "lib_", ex: lib_1, lib_2, lib_3 ....) to arrange class path orders. Library class path order is natural order of String Class such as : lib_1 >lib_2> lib_3> ... > Settings/profiles/deniz.turan/libs (See figure 4 and 5)

Imprtant note: Profiles' libraries are in higher order in class path as demonstrated in below

Having a central setting project is not enough alone, unless the content of profiles (/Settings/profiles) and central common libraries (/Settings/libs) are automatically contributed to main projects for each developer independently. Therefore, i decided to develop an eclipse plug-in to contribute to classpath of a project from a setting project and profile, with a automatic manner. Of course, it is possible to add libraries or folders to project's build path in eclipse, but as i pointed out above, it causes conflicts during merges, branches or in new workspace or on other machines.

To develop plug-in, following two extension points from eclipse rcp framework are used:
  • org.eclipse.jdt.core.classpathContainerInitializer : Initialize and manage class path container (ex: "Deniz's Classpath", in sample source project, Figure 2 , 4 and 5)
  • org.eclipse.jdt.ui.classpathContainerPage : Configuration page for classpath container (Figure 3)
For an overview of JDT in eclipse, please click here

Below you can see some screen shots from Classpath Manager project and a sample workspace.

Figure 2: Classpath Container ("Deniz's Classpath Manager")


Figure 3: Classpath Container Page


Figure 4: After Classpath Manager contributes to a project's build path


Figure 5: Final workspace after Classpath Manager contributes resources from Settings project and profile (deniz.turan)


As figure 5 illustrates, content of deniz.turan profile in Settings project (/Settings/profiles/deniz.turan) and common libraries (/Settings/libs) are contributed to sample "Booo" project. As explained above, order of contributed libraries in classpath are based on first profile sub libraries (/Settings/profiles/deniz.turan/libs/lib_1), and then profiles libraray (/Settings/profile/deniz.turan/libs) and finally common libraries (/Settings/libs)

You can download source code of Classpath Manager eclipse plugin project with sample setting project. The source code is a prototype and implements basic idea of the classpathContainerInitializer and classpathContainerPage extensions. It is tested with eclipse 3.4 and 3.5.

Click here to download ource code.


No comments: