Quarkus support for Quarkus libraries/components?

482 views
Skip to first unread message

David Hoffer

unread,
Apr 19, 2021, 10:21:28 AM4/19/21
to Quarkus Development mailing list
We have several Quarkus apps now in development that have duplicate code.  We want to move that duplicate code to separate standalone Maven modules as per standard practice for Java modules, components.

However some of this shared, duplicate code uses Quarkus features.  Specifically it uses CDI @Inject in a few places and @Inject @PersistenceUnit("PU") in one place for the ORM bindings.  In addition this shared module will have several @Entities with all the typical JPA, Jackson, Validation annotations. 

So how will making a shared component work in Quarkus?  Some questions/concerns are:

1. Will Quarkus find/apply the Quarkus annotations in the compiled Java jar file just as it does now for source code?

2. Is it preferable to not include Quarkus @Inject, @PersistenceUnit in the component?  E.g. @Inject can be replaced with normal Java object creation and @PersistenceUnit could be externalized so the EntityManagerFactory that it was injecting is done in the application and then pass the EntityManagerFactory to the component.

Anything else I should be aware of making shared Quarkus components?

-Dave


Georgios Andrianakis

unread,
Apr 19, 2021, 10:26:09 AM4/19/21
to David Hoffer, Quarkus Development mailing list
On Mon, Apr 19, 2021 at 5:21 PM David Hoffer <dhof...@gmail.com> wrote:
We have several Quarkus apps now in development that have duplicate code.  We want to move that duplicate code to separate standalone Maven modules as per standard practice for Java modules, components.

However some of this shared, duplicate code uses Quarkus features.  Specifically it uses CDI @Inject in a few places and @Inject @PersistenceUnit("PU") in one place for the ORM bindings.  In addition this shared module will have several @Entities with all the typical JPA, Jackson, Validation annotations. 

So how will making a shared component work in Quarkus?  Some questions/concerns are:

1. Will Quarkus find/apply the Quarkus annotations in the compiled Java jar file just as it does now for source code?

It will work as long as the library is indexed, see https://stackoverflow.com/a/55513723/2504224 for all the options on how to do it.

2. Is it preferable to not include Quarkus @Inject, @PersistenceUnit in the component?  E.g. @Inject can be replaced with normal Java object creation and @PersistenceUnit could be externalized so the EntityManagerFactory that it was injecting is done in the application and then pass the EntityManagerFactory to the component.

This is really depends on where the libraries are going to be used IMHO

Anything else I should be aware of making shared Quarkus components?

-Dave


--
You received this message because you are subscribed to the Google Groups "Quarkus Development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quarkus-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/674ba54b-7440-4893-a260-7fe93d8c7ee8n%40googlegroups.com.

David Hoffer

unread,
Apr 28, 2021, 11:47:06 AM4/28/21
to Quarkus Development mailing list
Using jandex-maven-plugin does not seem to work.  I'm finding that @Inject, @PostConstruct don't seem to get processed in the component.

Could it be that the plugin creates /META-INF/jandex.idx in each component?  How would Quarkus find all these files since all have the same path/name?  Do I need to create unique names for these index files?  Does the plugin support that?

-Dave

Paul Carter-Brown

unread,
Apr 28, 2021, 11:55:38 AM4/28/21
to David Hoffer, Quarkus Development mailing list
We use your use case a lot. Just ensure the jar has a META-INF/beans.xml and Quarkus will scan and run the build time processing of the annotations. You don't need Jandex for this.

We have projects that have no source code and are just a container for dependencies and the Quarkus packaging process to run.



Alexey Loubyansky

unread,
Apr 28, 2021, 12:07:28 PM4/28/21
to pa...@ukheshe.com, David Hoffer, Quarkus Development mailing list
Just in case, the advantage of having META-INF/jandex.idx is that the index would simply be deserialized instead of re-calculated for the dependencies during the augmentation phase.

Alexey Loubyansky

unread,
Apr 28, 2021, 12:09:24 PM4/28/21
to David Hoffer, Quarkus Development mailing list
Are you configuring the jandex plugin in the project/module, which is a dependency of the main module of the application?

David Hoffer

unread,
Apr 28, 2021, 12:15:21 PM4/28/21
to Quarkus Development mailing list
I have jandex-maven-plugin in each component (currently 3 components).  Those components are in separate component builds, these are added as Maven dependencies in our application build.

I switched to using empty /META-INF/beans.xml file instead and that does seem to work.

-Dave

Alexey Loubyansky

unread,
Apr 28, 2021, 12:19:35 PM4/28/21
to David Hoffer, Quarkus Development mailing list
Just to clarify, by working are you talking about the dev mode or the build/tests? If it's dev mode, which command are you using to launch it?

David Hoffer

unread,
Apr 28, 2021, 12:25:25 PM4/28/21
to Alexey Loubyansky, Quarkus Development mailing list
I am currently not using dev mode.

I am launching the fat/uber jar from the command line.

-Dave

Alexey Loubyansky

unread,
Apr 28, 2021, 12:30:00 PM4/28/21
to David Hoffer, Quarkus Development mailing list
The fact that Jandex doesn't work for you sounds strange then.

Guillaume Smet

unread,
Apr 28, 2021, 12:52:52 PM4/28/21
to Alexey Loubyansky, David Hoffer, Quarkus Development mailing list
I think that's expected.

If you want CDI to consider the external components, you need to have an empty `META-INF/beans.xml`. I should probably update my answer.

Alexey Loubyansky

unread,
Apr 28, 2021, 1:42:30 PM4/28/21
to Guillaume Smet, David Hoffer, Quarkus Development mailing list

Loïc MATHIEU

unread,
Apr 29, 2021, 3:28:47 AM4/29/21
to Alexey Loubyansky, Guillaume Smet, David Hoffer, Quarkus Development mailing list
Jandex plugin without empty bean.xml do works to discover beans, the documentation is OK and the stackoverflow answer also.
I use it in some of my libraries without issue.
And as Alexey already pointed out, it's better than an empty bean as the library will be indexed once at build time of the library instead of each time the dependent module is build.

David, if you can share a reproducer or provides your pom.xml we may understand what's going on.

Reply all
Reply to author
Forward
0 new messages