Developing Petals in Eclipse IDE

This page helps to get Petals development environment working into Eclipse :

  • to have multiple java/maven/eclipse projects
  • to use the Petals code formatter to format your code
  • to configure Eclipse to sort the code members in the right order
  • to configure Eclipse to sort the imports in the right order
This configuration was initially tested under Ubuntu Linux, and maybe it requires an additional steps to configure on Windows and others OS. So adjust these instructions for your environment and give us the feedback: petals-dev (at) objectweb.org

Software and Tools

You can download the Eclipse software here. If your Eclipse version is older than the 3.2, update to the 3.2.

To make easier the development with Eclipse, you can add some plugins to it :

Preparing Eclipse

Before checking out Petals sources from the repository there are initial steps to be done. Some helps you working in a smart manner with the development environment.

Multiple Workspaces

The first thing you must know is getting work with multiple workspaces in Eclipse. For developers a workspace is a collection of related projects. You can have more than one workspace for different sets of projects. In almost cases the best way to manage these multiple workspaces is configuring Eclipse to prompt which workspace to use every startup. Otherwise, you should create various copies of Eclipse start icon/link in the desktop using the -data <workspace_dir> as an executable argument to specify your choice.

To configure this option in Eclipse you must access Window -> Preferences..., then select General -> Startup and Shutdown item in the navigable tree and check the box for Prompt for workspace on starup - if not set. Done, you'll be prompted to select a workspace in the next time you start Eclipse.

EclipseMultiproject_02.jpg

If the specified path doesn't exist Eclipse will try create the directory. You can change the workspace while Eclipse is already running - for this, use File - Switch Workspace... submenu. Also, you could start more than one Eclipse instances to work with different workspaces.

Be aware that a workspace is more than a collection of projects. Eclipse internally uses the workspace directory to store your preferences for that set of projects - and other runtime files and views. Each workspace you have specific configurations, for example: code formatting, code comments templates, JRE used, so on...

Separating the workspace directory tree from project directory tree is a good approach for better management. That way you're able to share project among various workspaces. At least you can avoid unexpected projects removal when installing a new version of Eclipse. Take a look in the example below to realize how it should work.

Example

''Eclipse binaries tree''

/usr/local/share/eclipse
/usr/local/share/eclipse/configuration
/usr/local/share/eclipse/features
/usr/local/share/eclipse/plugins
...

''Eclipse user configuration tree''

~/.eclipse
…
~/.eclipse/workspace
~/.eclipse/workspace/petals
~/.eclipse/workspace/otherproject1
~/.eclipse/workspace/otherproject2
...

''Project tree''

~/projects/petals
~/projects/otherproject1
~/projects/otherproject2

''Note: on Linux you should read ~ as your use home directory, and the /usr/local/share is the directory used to put application available to all users. Adjust this example to your environment wherever possible. :)''

Maven Classpath Variable

When a Maven project is imported into Eclipse it expects a M2_REPO classpath variable entry. It helps to reference project libraries within Maven repository. To setup this variable go to Window -> Preferences... and select Java -> Build Path -> Classpath Variables on the navigable tree.

EclipseMultiproject_03.jpg
Add a new M2_REPO entry pointing to the local Maven 2 repository. Example:

M2_REPO=~/.m2/repository

Selecting JDK and its Options

Eclipse allow a number of installed JDKs configured in your environment. If you have more than one you can easily change to use other JDK by accessing Window -> Preferences and then selecting the Java -> Installed JREs on the navigable tree. Please, choose one JDK compatible with ''Java(TM) 1.5''.

Next, a update in compiler settings is required to eliminate compilation problems due to earlier versions of Java(TM). To configure these options select the Java -> Compiler item on the navigable tree. It should look like this:

EclipseMultiproject_04.jpg

Multiple Workspaces building

Once you have recovered the Petals project from the svn (see the Developers section "Petals download and setup"), and followed the instructions to build and install the subprojects in the Maven 2 repository.

You can consider to build the multiproject configuration.

Go to the petals directory.

$ mvn eclipse:eclipse

It will automatically generate Eclipse .project , and .classpath files , and .settings directory for Petals multiprojects. After that you'll be able to import Petals into Eclipse.

Import Petals code formatter

The Petals code formatter is designed to format all the code according to the Java Sun code style recommandation with some little changes to match the configuration of Checkstyle.

To import the formatter, you have to go to Window -> Preferences , then select Java -> Code Style -> Formatter , click on Import... and select the petals-formatter.xml file in the etc directory of the Petals project.

import.png

Now you can format all the code you write by doing a right click on the java file and selecting Source -> Format.

Configure Eclipse to sort members in the right way

In the default configuration Eclipse sort all the static members at the top of the class which is not right according to the Java Sun code style recommendation.

We must change the order of the members to put all the fields at the top of the class.

To change that, you have to go to Window -> Preferences , then select Java -> Appearance -> Members Sort Order. The default configuration is like that : SortMembersDefault.png

Using the Up, and Down buttons, you must change the order to this order and check the Sort member in the same category by visibility check box :

SortMembers.png

To sort the members of a java file in the Package Explore view, you have to select a java file, then do a right click and select Source -> Sort Members.

Configure Eclipse to sort the imports in the right way

In the default configuration Eclipse does not sort the static imports from the project and the others imports. Thus we have to add a rule to the Organize imports configuration.

The static imports from our project (with a package starting with org.objectweb.petals) must be sorted with the other imports instead of being the first imports in the list.

To change that, you have to go to Window -> Preferences, then select Java -> Code Style -> Organize imports. Then click on New Static... and enter the name "org.objectweb.petals". Then valid.

You must have : OrganizeImports.png

Final Configurations

Importing Projects into Eclipse

You'll open a blank workspace and import the Petals project. This is done by accessing the File -> Import -> Existing Projects into Workspace command. You can choose to import all petals and related projects by selecting the root folder of the Petals project.

Your workspace shoud look like this:

EclipseMultiproject_05.jpg

Server Launcher

The next configuration you need to create is a Server Launch configuration that runs as a local application.

From the menu, select Run --> Run....

In the wizard, select Java Application and hit New to create a launch configuration named Petals - Server. For project choose:

petals-standalone

For main class enter:

org.objectweb.petals.system.launch.StandaloneLauncher

project-conf.png

Select the Classpath tab. Select Add Jars... to be added to the User Entries list. Add the following entry:

server.jar - /petals-standalone/target/petals-standalone-X.X-SNAPSHOT-dev.dir/petals-X.X-SNAPSHOTbin

You must remove all other entries in the User Entries list. Your screen should look like the following...

EclipseMultiproject_13.jpg