Tutorial – Developing a HK2 module with NetBeans 6.0
- tor 10 jan, 2008 kl 13:47
- 2 kommentarer
- Java
(JSolutions.se is a Swedish blog, this post is however intentionally written in English)
This tutorial will show you how to develop the famous ”Hello World” as a HK2 module using NetBeans 6.0/maven.
The very first step is to create a new Maven Project:
as a Maven Quickstart Archetyp:
We also must supply the artifact id, group id and the version.
NetBeans actually creates a Hello world source file named App in the package se.javasolutions.hk2.HelloWorld.
The next thing we need to do is to add the maven repository containing HK2. The pom is located in the Project Files folder. Add the following to pom.xml:
Next we need to add the dependency to the hk2-maven-plugin. To do this, select Add Library on the Libraries folder in the project. Add the following:
We also need to add a dependency to hk2 iteself:
Now we must change the pom file to use the hk2-maven plugin to build and package the the project.
By default NetBeans assumes that we are using Java 1.4 and will therefor not recognize annotations. Select Properties on the project folder and the category source. Change Source/Binary Format to 1.5/1.6. Close and re-open the project.
Now it is time to create the contract for our hello world service:
As you can see is an ordinary interface with an @Contract annotation.
The Service implementation is just as simple:
The App class is modified to implement the interface ModuleStartup.
The service instance is injected to the service field by the HK2 runtime container.
And the very final thing we must to is to modify run project action to (note the Set properties):
Now you can run the project.
The complete NetBeans project with sources can be downloaded here.
Feel free to send commends to rikard.thulin(at)ibs.se
References:
Hi,
My name is James Branam and I’m the NetBeans Community Docs Manager. Your blog entry would make a fantastic tutorial for our Community Docs wiki (http://wiki.netbeans.org/wiki/view/CommunityDocs). Would you be willing to contribute it? If you need any help or have any questions, please contact me at james.branam@sun.com. I look forward to hearing from you.
Hi,
very helpfull tutorial. Runninf this with NB6.1 and maven 3.1.1 gives the following exception:
org.apache.maven.plugin.PluginManagerException: The PluginDescriptor for the plugin com.sun.enterprise:hk2-maven-plugin was not found. Should have been in realm: ClassRealm[/plugins/com.sun.enterprise:hk2-maven-plugin:0.2-SNAPSHOT@48/thread:Module-Actions, parent: null]
Any idea?!