Eclipse plugin development with Maven.

Introduction

Setting up new Eclipse project for plugin development is very ease if you use maven. I have created simple project that you can use as an example with you development.
You don’t really need to install any plugins like JDT (Eclipse Java Development Tools), for your eclipse, or install specific version of eclipse with integrated JDT.
When you import maven project from specific pom, needed plugins will automatically install into your eclipse. On the other hand if you do not want to use eclipse as your IDE while development you can just compile it form command line with maven commands. More on that later.
 
To summarize you need maven installed in your system, if you want to develop using eclipse, you just need version for java development, with m2e plugin (Maven support in eclipse).


What will we have


Clicking on Java project to see context menu, then on item „Show Class Graph”…
context_menu
shows dialog window with project path as message:
dialog


Starting up

Checkout

For full review you can just clone git project from the folowing location https://github.com/tmorcinek/eclipse-plugin-with-maven. Checkout to tag v0.0.1 where you have the first relevant commit with working plugin.

Eclipse Import

You can import this project into eclipse by going:
(from context menu on Package Explorer) Import->Maven->Existing Maven Projects
Then select location of cloned project’s pom file.

Maven Building

You can also build it using maven by:
mvn clean package
This command will build plugin in your target folder, plugin in this case will have name: ‚com.morcinek.uml.class-graph-plugin-eclipse-0.0.1.jar’, where ‚com.morcinek.uml.class-graph-plugin-eclipse’ – artifact name from pom file, ‚0.0.1’ version from pom file.


Maven Project

As you can see this maven project has one module which is responsible for plugin development. Making modules in such small projects doesn’t make sense, but it will in the future when you want to add reference projects or extra libraries.
So lets start with root pom.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.morcinek.uml</groupId>
	<artifactId>uml-parent</artifactId>
	<version>0.0.1</version>
	<packaging>pom</packaging>
	<name>Uml parent project</name>
	<modules>
		<module>com.morcinek.uml.class-graph-plugin-eclipse</module>
	</modules>
	<properties>
	    <java-version>1.6</java-version>
		<tycho-version>0.15.0</tycho-version>
	</properties>

	<repositories>
		<repository>
			<id>juno</id>
			<layout>p2</layout>
			<url>http://download.eclipse.org/releases/juno</url>
		</repository>
		<repository>
			<id>maven2</id>
			<url>http://repo1.maven.org/maven2</url>
		</repository>
	</repositories>
</project>

Important are declaration of eclipse repositories from where maven downloads plugins and dependencies while building.

The plugin pom file.

...
	<packaging>eclipse-plugin</packaging>
...
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>${java-version}</source>
					<target>${java-version}</target>
				</configuration>
			</plugin>
			<plugin>
	            <groupId>org.eclipse.tycho</groupId>
	            <artifactId>tycho-maven-plugin</artifactId>
	            <version>${tycho-version}</version>
	            <extensions>true</extensions>
	         </plugin>
	         <plugin>
				<groupId>org.eclipse.tycho</groupId>
				<artifactId>target-platform-configuration</artifactId>
				<version>${tycho-version}</version>
				<configuration>
					<environments>
						<environment>
							<os>linux</os>
							<ws>gtk</ws>
							<arch>x86</arch>
						</environment>
						<environment>
							<os>linux</os>
							<ws>gtk</ws>
							<arch>x86_64</arch>
						</environment>
						<environment>
							<os>win32</os>
							<ws>win32</ws>
							<arch>x86</arch>
						</environment>
						<environment>
							<os>win32</os>
							<ws>win32</ws>
							<arch>x86_64</arch>
						</environment>
						<environment>
							<os>macosx</os>
							<ws>cocoa</ws>
							<arch>x86_64</arch>
						</environment>
					</environments>
				</configuration>
			</plugin>
		</plugins>
	</build>
...

In this file packaging is important, it says to maven that we want to build eclipse plugin. In build section plugin declarations are used while building, thanks to them project is build correctly. Packaging and plugin declarations are required for maven to build eclipse plugin.


Build properties

Another important file is build.properties, which looks like this:

source.. = src/main/java,\
		   src/main/resources
output.. = target/
bin.includes = plugin.xml,\
               META-INF/,\

This simple example as you can see describes where is the source code, output, and which files you also want do include in plugin without building (bin.includes). In further development when you want to add reference project or libraries this file will be relevant.


Plugin configuration

More complicated is plugin.xml file:

<plugin>
    <extension point="org.eclipse.ui.menus">
        <menuContribution locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
            <command commandId="com.morcinek.uml.class_graph.plugin.convert"
                     label="Show Class Graph"
                     style="push">
                <visibleWhen
                        checkEnabled="false">
                    <and>
                        <count value="1"/>
                        <with
                                variable="activeMenuSelection">
                            <iterate>
                                <adapt type="org.eclipse.core.resources.IProject">
                                    <test property="org.eclipse.core.resources.projectNature"
                 						value="org.eclipse.jdt.core.javanature"/>
                                </adapt>
                            </iterate>
                        </with>
                    </and>
                </visibleWhen>
            </command>
        </menuContribution>
    </extension>

    <extension point="org.eclipse.ui.commands">
        <command defaultHandler="com.morcinek.uml.class_graph.plugin.Convert"
                 id="com.morcinek.uml.class_graph.plugin.convert"
                 name="Convert">
        </command>
    </extension>
</plugin>

This is the description on where and when should the option be visible, and how it should be handled.
From file you can find out that option appears in contect menu while clicking on the Java Project element in PackageExplorer. Clicking on the option with name Show Class Graph will be handled by class:
com.morcinek.uml.class_graph.plugin.Convert.


Manifest File

There is an MANIFEST.MF file located in folder META-INF/. I have used following file:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Class Graph Display
Bundle-SymbolicName: com.morcinek.uml.class-graph-plugin-eclipse;singleton:=true
Bundle-Version: 0.0.1
Bundle-Vendor: MORCINEK
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime,
 org.eclipse.core.expressions
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Eclipse-LazyStart: true
Import-Package: 
 org.eclipse.core.internal.resources,
 org.eclipse.core.resources
Export-Package: com.morcinek.uml.class_graph.plugin
Bundle-ClassPath: .

As you can see file contains important data about plugin. The most important and relevant on this stage are:

  • ‚Bundle-SymbolicName’ – needs to be the same as artifact name in pom file.
  • ‚Require-Bundle’,’Import-Package’ – I am not sure about that, and I am to lazy to check, those names give a clue of that those packages are responsible for. Thing is that if you want to use jdt classes from those packages you need to declare them in one of those sections. (you can check JDT help form more informations)
  • ‚Export-Package’ – package in which you have your source code.
  • ‚Bundle-ClassPath’ – build classes or libraries you want to also include to plugin binary file (will be extended further).


Handler class

The last but not least is java class for handling the click action described in plugin.xml.

public class Convert extends AbstractHandler {

	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveMenuSelection(arg0);
		try {
			Object firstElement = selection.getFirstElement();
			IProjectNature resource = (IProjectNature) firstElement;
			IPath path = resource.getProject().getLocation();

			IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(arg0);
			MessageDialog.openInformation(window.getShell(), "Selected Project", path.toOSString());
		} catch (Exception e) {
			e.printStackTrace();
			showErrorMessage(e);
		}
		return null;
	}

	private void showErrorMessage(Exception e) {
		showErrorMessage(e.getMessage());
	}

	private void showErrorMessage(String message) {
		MessageDialog.openError(new Shell(), "Eclipse Maven Plugin", message);
	}

}

This simple code just shows dialog with project path message in it.


Summarize

Reklamy

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj / Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj / Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj / Zmień )

Connecting to %s