September 19th, 2010
9:50 pm
Library Configuration and Deployment with Eclipse

Posted under Eclipse
Tags , , ,

I have experimented with the following techniques for managing user defined collections of library jars in a Java EE 6 web application within Eclipse Helios. Much of this follows from this chapter in Eclipse Distilled:-

  1. An interesting method described in “Eclipse Distilled” is to use a local simple project in the workspace as a library project just to hold eclipse folders to plant the jar collections in. This then allows all other projects in the workspace to reference the jars using relative workspace references, which gives complete portability to the workspace across different locations and operating systems. It does not however allow the library collections to be designated via a single name in the way that user libraries do – you end up with a just a pile of jars on the build path of a referencing project. You can however at least add them all at once via multi select of the jars from a folder in the library project.
  2. Another way is to use User Libraries, again as in the above chapter. They have the benefit of allowing a single name to designate the collection of jars, they can be used from anywhere in the workspace, and they can also be exported and imported between workspaces along with other workspace settings. The drawback is that the library location must be defined as an absolute file system location, which means that unlike the above method, you can’t just copy a workspace with all the libraries in it to somewhere completely different and have it ‘just work’ – you have to set up the library locations. However, you also have to typically do this for JSF and JRE libraries, so unless you have a lot of them it is not such a chore.
  3. Another mechanism is to use eclipse Classpath Variables, which allow a folder location in the filesystem to be defined and then used via a name. Again, the definitions need to be set up on each different system/location that you migrate the workspace to.
  4. Classpath Containers are a dynamic form of reference where a named reference is intercepted by code in a class. This is used by plugins, and allows the definition to change dynamically in response to other environmental changes detected by the plugin code.

On balance, in the absence of a plugin/Classpath Container approach, my favoured option is to use User Libraries for tools like Primefaces as they are convenient and look tidy on the build path even when it gets large. In an ideal world, it would be helpful if User Libraries allowed workspace relative references. This would permit the libraries to be packaged within the workspace which would mean no setup is needed to port the workspace. The downside is that when multiple workspaces are used, you end up with library duplication unless you use an absolute location outside the workspaces. However, you could have a libraries folder under a parent folder above all the workspaces. Then if a relative reference like ..\libraries was permitted you get your cake and eat it – no duplication and a portable collection of workspaces with a single copy of all the associated libraries.

However for now, it’s User Libraries for me. To create a User Library for a project in Eclipse, do the following :-

  1. Select project properties/build path/Configure build path, then select the Libraries tab in the right hand pane.
  2. Click Add Library…, select User Libraries, and hit Next.
  3. Click User Libraries… which will display the User libraries page.
  4. Click New to add a library, then add jar(s) to it with Add Jars…
  5. Having added a jar, you can expand its definition using the tree control in the middle pane, and then double click on javadoc location, and/or Source attachment to add a jar or zip containing the javadocs/source for the jar.
  6. The library is then accessible from all the projects in your Workspace. You can export it to other workspaces by using File/Export/Preferences.
  7. Note that typically you need to tell Eclipse to deploy the libraries to WEB-INF\lib when deploying. Under Eclipse Helios, you do this using the Deployment Assembly options page under the project properties, adding an entry for each library you need deploying. When adding, you can elect to add from the java build path, which makes the most sense if you are deploying libraries you have added to the build path. Note that in previous versions of Eclipse, this option on the project properties was designated J2EE Module Dependencies.

No Comments »