且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Quarkus 中为外部模块中的类创建 Jandex 索引

更新时间:2023-01-08 23:27:30

Quarkus 自动索引主模块,但是,当您有包含 CDI bean、实体、序列化为 JSON 的对象的附加模块时,您需要显式索引它们.

Quarkus automatically indexes the main module but, when you have additional modules containing CDI beans, entities, objects serialized as JSON, you need to explicitly index them.

>

有几个不同的(易于实施)选项可以做到这一点.

There are a couple of different (easy to implement) options to do so.

使用 Jandex Maven 插件

只需将以下内容添加到附加模块 pom.xml 中:

Just add the following to the additional module pom.xml:

<build>
  <plugins>
    <plugin>
      <groupId>org.jboss.jandex</groupId>
      <artifactId>jandex-maven-plugin</artifactId>
      <version>1.1.0</version>
      <executions>
        <execution>
          <id>make-index</id>
          <goals>
            <goal>jandex</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

如果您的依赖项在项目外部并且您想一劳永逸地构建索引,这是最有益的选择.

It's the most beneficial option if your dependency is external to your project and you want to build the index once and for all.

使用 Gradle Jandex 插件

如果你使用 Gradle,有一个第三方插件可以生成 Jandex 索引:https://github.com/kordamp/jandex-gradle-plugin .

If you are using Gradle, there is a third party plugin allowing to generate a Jandex index: https://github.com/kordamp/jandex-gradle-plugin .

添加一个空的 META-INF/beans.xml

如果您在附加模块 src/main/resources 中添加一个空的 META-INF/beans.xml 文件,类也将被索引.

If you add an empty META-INF/beans.xml file in the additional module src/main/resources, the classes will also be indexed.

这些类将由 Quarkus 本身索引.

The classes will be indexed by Quarkus itself.

索引其他依赖项

如果您无法修改依赖项(例如,考虑第三方依赖项),您仍然可以通过在 application.properties 中添加一个条目来索引它:

If you can't modify the dependency (think of a third-party dependency, for instance), you can still index it by adding an entry to your application.properties:

quarkus.index-dependency.<name>.group-id=
quarkus.index-dependency.<name>.artifact-id=
quarkus.index-dependency.<name>.classifier=(this one is optional)

是您选择用来标识您的依赖项的名称.

with <name> being a name you choose to identify your dependency.