如果无法解决依赖关系,如何告诉Maven忽略它呢? [英] How to tell Maven to ignore a dependency if failed to resolve it?
问题描述
我有一个依赖关系,该依赖关系已安装在Maven本地存储库中,并在本地使用,但在部署服务器上不可用.我使用Class.forName(...)
使用该依赖项,因此如果部署服务器上的类路径中缺少它,将不会有任何问题.
I have a dependency which I have installed in Maven local repository and is being used locally but is not available on deployment server. I use that dependency using Class.forName(...)
so there will be no problem if it's missed from classpath on deployment server.
是否有任何方法可以告诉Maven如果无法解决依赖关系,那就忽略它吗?
Is there any way to tell Maven to ignore a dependency if it failed to resolve it?
我似乎认为<scope>
或<optional>
不能解决此问题,但是如果有任何方法可以根据依赖项的可用性来激活/停用配置文件,则可以使用<profiles>
来解决. /p>
I doesn't seem that <scope>
or <optional>
can solve this problem, but it may be possible to do it with <profiles>
if there is any way to activate/deactivate a profile based on dependencies availability.
推荐答案
简短答案:否.
长答案:一旦声明了依赖关系(无论是在公共部分还是在活动配置文件中),当Maven尝试进行依赖时,它都必须是可解析的;否则,构建将失败.
Long answer: Once dependency is declared - either in common part or in an active profile - it must be resolvable when Maven attempts it; otherwise the build fails.
如果maven允许请求的行为,则构建的可重复性将受到很大影响.
If maven allowed the requested behavior, the reproducibility of build would suffer a lot.
如果晦涩难懂和不可复制性对您来说不是问题,那么这里有一个提示:
If obscurity and irreproducibility is not an issue for you, here is a hint how to do it:
- 使用exec-maven-plugin或maven-antrun-plugin从pom.xml调用外部蚂蚁
- 在蚂蚁代码中,使用Maven Ant中的 artifact:dependencies 任务
- 将其包装在ant-contrib的 trycatch 块 中
- call external ant from your pom.xml, using either exec-maven-plugin or maven-antrun-plugin
- in the ant code, use artifact:dependencies from Maven Ant Tasks
- wrap it in ant-contrib's trycatch block
无论如何,我强烈不鼓励将这些东西包括进Maven构建中.将其作为单独的功能(通过命令行中的ant调用)通常就足够了.
In any case, I strongly discourage including such things into maven build. Having it as separate functionality, invoked via ant from commandline, might often be enough.
这篇关于如果无法解决依赖关系,如何告诉Maven忽略它呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!