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

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

问题描述

一个项目在 Google App Engine 上运行.由于安全限制,该项目具有使用无法在 App Engine 上调用的类的依赖项(它不在 白名单).我的(非常hacky)解决方案是将该类的修改版本复制到我的项目中(匹配原始类的名称和包),不需要受限类.这适用于 dev 和 live,我假设是因为我的源出现在我的外部依赖项之前的类路径中.

A project 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.

为了让它更简洁,我决定将该类的修改版本放入它自己的项目中,该项目可以打包在一个 jar 中并发布给其他遇到此问题的人使用.

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.

这是我的 build.gradle:

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'

在我的本地开发服务器上,这工作正常,代码在运行时首先找到我的类的黑客版本.在直播中,由于某种未知原因,首先加载了 elasticsearch 依赖项中的版本.

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?

推荐答案

不太确定这是否是访问此问题的人正在寻找的内容,但这就是我的问题以及我找到的解决方案.罐子 A:包含类 XYZJar B:也包含类 XYZ我的项目在 Jar A 之前需要类路径上的 Jar B 才能被编译.

Not really sure if this is what people visiting this question were looking for, but this was what my problem and a solution that I reached at. Jar A: contains class XYZ Jar B: also contains class XYZ My Project needs Jar B on the classpath before Jar A to be able to get compiled.

问题是 Gradle 在解决依赖项后根据字母顺序对依赖项进行排序,这意味着 Jar B 将在生成的类路径中出现在 Jar A 之后,导致编译时出错.

Problem is Gradle sorts the dependencies based on alphabetical order post resolving them which meant Jar B will be coming after Jar A in the generated classpath leading to error while compiling.

解决办法:声明一个自定义配置并修补 compileClasspath.这就是 build.gradle 的相关部分的样子.

Solution: Declare a custom configuration and patch the compileClasspath. This is how the relevant portion of build.gradle might look like.

configurations {
    priority
    sourceSets.main.compileClasspath = configurations.priority + sourceSets.main.compileClasspath
}

dependencies {
    priority 'org.blah:JarB:2.3'
    compile 'org.blah:JarA:2.4'
    ...
}

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

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