我可以使用Gradle在我的类路径中强制依赖的顺序吗? [英] Can I force the order of dependencies in my classpath with Gradle?

查看:241
本文介绍了我可以使用Gradle在我的类路径中强制依赖的顺序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我非常反对谷物的声音,但是在这里我出来了...



我有一个运行在Google App Engine上的项目。该项目具有依赖性,因为安全限制(它不在白名单)。我的(非常冒险的)解决方案是将该类的修改版本复制到我的项目中(匹配原始类的名称和包),而不需要受限制的类。这对开发人员和生活都有效,我假设我的源代码出现在我的外部依赖关系之前的类路径中。



为了使它更清洁一点,我决定把我的修改这个类的版本放入它自己的项目中,可以打包成一个jar文件,并发布给任何其他人使用,如果他们面临这个问题。



这是我的build.gradle:

  //我的jar有'固定'版本的Class。 
编译文件('path / to / my-hack-0.0.1.jar')

//包含不会在appengine上运行的类的依赖项
编译' org.elasticsearch:elasticsearch:1.4.4'

在我的本地开发服务器上,这个工作正常,代码首先在运行时查找我的黑客版本的类。在生活中,由于某种未知的原因,弹性搜索依赖项中的版本首先被加载。



我知道在classpath中有两个相同类的版本并不理想,我希望能够可靠地强制我的版本在类路径的开始。有任何想法吗?或者,有没有更好的方法可以解决这个问题? 解决方案

gradle dependencies documentation ,依赖关系的顺序定义了类路径中的顺序。所以,我们可以简单地把库放在依赖关系中。



但是要小心!这里有两个优先级更高的规则:


  • 对于动态版本,较高静态版本优先于较低版本。

  • 模块描述符文件(Ivy或POM文件)声明的模块优于仅包含工件文件的模块。


I know I'm going very much against the grain hear but here me out...

I have a project that runs on Google App Engine. The project has dependency that uses a class that can't be invoked on App Engine due to security constraints (it's not on the whitelist). My (very hacky) solution was to just copy a modified version of that class into my project (matching the original Class's name and package) that doesn't need the restricted class. This works on both dev and live, I assume because my source appears in the classpath before my external dependencies.

To make it a bit cleaner, I decided to put my modified version of that class into it's own project that can be packaged up in a jar and published for anyone else to use should they face this problem.

Here's my build.gradle:

// my jar that has 'fixed' version of Class.
compile files('path/to/my-hack-0.0.1.jar')

// dependency that includes class that won't run on appengine
compile 'org.elasticsearch:elasticsearch:1.4.4'

On my local dev server, this works fine, the code finds my hacked version of the class first at runtime. On live, for some unknown reason, the version in the elasticsearch dependency is loaded first.

I know having two versions of the same class in the classpath isn't ideal but I was hoping I could reliably force my version to be at the start of the classpath. Any ideas? Alternatively, is there a better way to solve this problem?

解决方案

According to gradle dependencies documentation, the order of dependencies defines the order in the classpath. So, we can simply put the libraries in the correct order in "dependencies".

But beware! here are two rules with higher priorities:

  • For a dynamic version, a 'higher' static version is preferred over a 'lower' version.
  • Modules declared by a module descriptor file (Ivy or POM file) are preferred over modules that have an artifact file only.

这篇关于我可以使用Gradle在我的类路径中强制依赖的顺序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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