【问题标题】:make library use jpa annotations without depends on any implementation使库使用 jpa 注释而不依赖于任何实现
【发布时间】:2015-07-28 20:13:59
【问题描述】:

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

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

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

【问题讨论】:

    标签: java maven jpa maven-3


    【解决方案1】:

    不幸的是,没有只提供注释/接口的标准包(例如,在 servlet 规范中)。每个 ORM 都有自己的包,但它们遵循 jpa 标准。你可以做的是将依赖声明为optional

    对于 Eclipse 链接

    <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>
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多