使库使用 jpa 注释而不依赖于任何实现

make library use jpa annotations without depends on any implementation

我正在构建一个库,我在其中扫描 class 并检查它们的字段是否为 OneToManyManyToOne 注释。我目前添加了 eclipselink 3.6 作为我模块的依赖项,就像这样

<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>eclipselink</artifactId>
  <version>2.6.0</version>
</dependency>

但我不想让我的库依赖于 eclipselink,我希望它能够与任何 JPA 实现一起使用。我该怎么做?

不幸的是,没有仅提供 annotations/interface 的标准包(例如,在 servlet 规范中)。每个 ORM 都有自己的包,但它们 遵循 jpa 标准。您可以做的是将依赖项声明为 optional.

对于 Eclipselink

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.1.0</version>
    <optional>true</optional>
</dependency>

您的测试可能需要依赖 eclipselink,因此您可以将原始依赖标记为仅用于测试...

<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>eclipselink</artifactId>
  <version>2.6.0</version>
  <scope>test</scope>
</dependency>