Is it possible to scan @Service from another library with HK2?

112 views Asked by At

I have a service, that uses hk2 Autoscan feature with help of hk2-inhabitant-generator plugin. It works with the classes I have in my source code. But is there a way to add packages from dependencies to be scaned as well? For example if I have dependency:

<dependency>
    <groupId>my.dependency</groupId>
    <artifactId>A</artifactId>
</dependency>

and it contains @Service class.

Now I have to add this class to DI container using binding, but can hk2-inhabitant-generator scan it, so that Autoscan feature would be able to get it from META-INF/... file?

1

There are 1 answers

1
jwells131313 On

I think the best thing is to use the CommandLine tool over your third-party jar. It's on the page you referenced in the comment above. I'll past the relevant parts here:

Command Line Tool

The HK2 Inhabitants Genertor can be run from the command line like this:

java org.jvnet.hk2.generator.HabitatGenerator

By default the HabibatGenerator will attempt to analyze the first element of the classpath and replace that element (if it is a JAR) with a new JAR that has an inhabitants file in it describing all of the classes marked with @Service. If the first element of the classpath is a directory it will attempt to create a new inhabitants file in that directory describing all of the classes marked with @Service.

You can modify this behavior by using command line options. Here is the usage statement for HabibatGenerator:

java org.jvnet.hk2.generator.HabitatGenerator [--file jarFileOrDirectory] [--outjar jarFile] [--locator locatorName] [--verbose]

The –file option allows the user to pick a directory or JAR file to analyze for classes marked with @Service.

The –outjar option allows the user to pick the output JAR file that the generator should create with the inhabitants file in it.

The –locator option allows the user to name the locator that these services should go into. This value is "default" by default.

The –verbose option make the generator print extra information as it does its work.

This command line utility will call System.exit when it is done with a 0 code if it was able to work properly and a non-zero value if it failed in some way.