【问题标题】:Eclipse: exclude a 'runtime' maven dependency from the build pathEclipse:从构建路径中排除“运行时”maven 依赖项
【发布时间】:2015-12-01 16:36:21
【问题描述】:

我有一个项目需要依赖于 iText 5.5.2 和 iText 2.1.7(Primefaces 在运行时需要此特定版本,并且由于许可证问题而无法与 iText 5 一起使用)。

所以我的 pom.xml 中有这个:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.2</version>
    <scope>compile</scope>
</dependency>

<!-- iText 2.1.7 is necessary at runtime to have the 'Export to PDF' function of Primeface work -->
<!-- It won't conflict with iText 5 as the packages are different -->
<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
    <scope>runtime</scope>
</dependency>

问题是我不希望我们的开发人员能够从 iText 2.1.7(com.lowagie.* 包)导入类。我想强制他们使用 iText 5.5.2(com.itextpdf.* 包)中的类。

虽然 iText 2.1.7 处于“运行时”范围内,但 Eclipse 仍然在构建路径中添加 jar 文件,允许开发人员导入错误的包(com.lowagie 而不是 com.itextpdf)。

有没有办法从构建路径中排除它?

【问题讨论】:

标签: java eclipse maven primefaces itext


【解决方案1】:

不幸的是,在 Eclipse 上使用正常构建似乎是不可能的,这是一个已知的错误,请检查 Bug 414645Bug 376616。 Eclipse (m2e) 无法正确管理 Maven 依赖范围。

但是,如果您将运行时依赖项放在配置文件中,则 Eclipse 不会将它们添加到类路径中(尽管默认情况下配置文件不应处于活动状态)。我刚刚在 Eclipse Mars 上测试过,效果很好。

因此,在您的情况下,您可以添加到您的 POM:

<profiles>
    <profile>
        <id>runtime</id>
        <dependencies>
            <dependency>
               <groupId>com.lowagie</groupId>
               <artifactId>itext</artifactId>
               <version>2.1.7</version>
               <scope>runtime</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

因此,它不能用于在 Eclipse 上编译。但是,您的构建需要在运行时使用它,在这种情况下使用 -Pruntime 运行。

虽然调整您的 POM 并针对某个 IDE 问题进行构建可能并不理想,但它可能是实现目标的一个很好的折衷方案。

【讨论】:

  • 由于重复的问题没有任何赞成票或任何答案,我无法将此问题标记为重复,这就是我最终回答这两个问题的原因。我想知道在这种特殊情况下,这里的最佳做法是什么。
猜你喜欢
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 2011-10-29
  • 1970-01-01
相关资源
最近更新 更多