Maven / eclipse,eclipse如何尊重maven运行时范围? [英] Maven/eclipse, How can eclipse honor maven runtime scope?
问题描述
在pom.xml中,我包含logback& SLF4J如下所示,使用maven build可以很好地工作。如果我直接从logback导入,它将给出编译错误。
In the pom.xml, i include logback & SLF4J like below, this works perfectly fine using maven build. It will give compilation error if i import directly from logback.
<dependencyManagement>
<dependencies>
<!-- We want to have slf4j with scope compile -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- logback we only want runtime, compiletime we want SLF4J -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
但是我如何让eclipse尊重logback依赖的运行时范围并阻止那里的import-advice?
But how can I make eclipse honor the runtime scope of logback dependency and prevent import-suggestions from there?
推荐答案
不幸的是,在正常构建的Eclipse上似乎不可能,如@ A4L所提到的,这是一个已知的bug ,请查看错误414645 和错误376616 。 Eclipse(m2e)无法正确管理Maven依赖项范围。
Unfortunately it seems not to be possible on Eclipse with a normal build, as mentioned by @A4L, 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>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</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.
这篇关于Maven / eclipse,eclipse如何尊重maven运行时范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!