Getting started
Maven Build
The best way to use HK2 in your builds is to add the following dependency in your maven build:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>myModule</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2</artifactId>
<version>2.5.0-b36</version>
</dependency>
</dependencies>
</project>
The org.glassfish.hk2:hk2 dependency has a dependency on all of the HK2 jars. However, this may be more than you want, since it includes configuration, run-level services and some osgi support that your application may not need. If instead you wanted the absolute minimum working profile for hk2 you would instead have your project look like this:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>myModule</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-locator</artifactId>
<version>2.5.0-b36</version>
</dependency>
</dependencies>
</project>
The hk2-locator project contains the implementation of the hk2 API, with no other bells and whistles. In particular, the ability to automatically detect services is not available, and so all HK2 objects must be added programmatically and gotten with the HK2 API. However, the above is perfect for small projects that want to play with the HK2 API to see how it works.
Automatic Service Population
In order for HK2 to automatically find services at runtime it can read files called inhabitant files. These are usually placed in your JAR file at location META-INF/hk2-locator. Normally the file is named default. (You can however use a different file name or location(s) by using more specific API). HK2 has a tool for automatically creating these files based on class files annotated with @Service. There is also a simple API for creating and populating a ServiceLocator with services found in these files.
In order to have your Maven build generate the META-INF files that hk2 reads in order to populate a ServiceLocator use the hk2-inhabitant-generator. This tool can be used from the command line, or it can be put into your maven or ant builds.
In order to have your program automatically load the files generated with the hk2-inhabitant-generator you can use the createAndPopulateServiceLocator method near the start of your main method, like this:
public static void main(String argv[]) {
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
MyService myService = locator.getService(MyService.class);
...
}