Eclipse:从构建路径中排除“运行时”maven依赖项 [英] Eclipse: exclude a 'runtime' maven dependency from the build path

查看:609
本文介绍了Eclipse:从构建路径中排除“运行时”maven依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要依赖iText 5.5.2和iText 2.1.7的项目(Primefaces在运行时需要这个特定版本,由于许可证问题,不能与iText 5一起使用)。

I have a project that needs a dependency on iText 5.5.2 and on iText 2.1.7 (Primefaces needs this specific version at runtime and won't work with iText 5 due to license issues).

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

So I have this in my 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。*包)中的类。

The problem is that I don't want our developers to be able to import classes from iText 2.1.7 (com.lowagie.* package). I want to force them to use classes from iText 5.5.2 (com.itextpdf.* package).

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

Although iText 2.1.7 is in 'runtime' scope, Eclipse still adds the jar file in the build path, allowing developers to import the wrong package (com.lowagie instead of com.itextpdf).

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

Is there a way to exclude it from the build path ?

推荐答案

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

Unfortunately it seems not to be possible on Eclipse with a normal build, it is a known bug, check Bug 414645 and Bug 376616. Eclipse (m2e) can't properly manage Maven dependencies scope.

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

However, if you place the runtime dependencies on a profile, then Eclipse will not add them to the classpath (the profile shouldn't be active by default, though). I just tested it on Eclipse Mars and it works perfectly.

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

Hence, in your case you could add to your 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 运行。

As such, it can't be used to compile on Eclipse. However, your build would then need to use it at runtime, running with -Pruntime in this case.

虽然调整你的POM并构建一个IDE的问题可能不是理想的,它可能是实现你的目标的一个很好的妥协。

Although adapting your POM and build to an issue of an IDE might not be ideal, it could be a good compromise to achieve your goal.

这篇关于Eclipse:从构建路径中排除“运行时”maven依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆