Can I force the order of dependencies in my classpath with Gradle?

微笑、不失礼 提交于 2019-11-28 14:14:50

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.

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.

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'
    ...
}

It's the app engine classloader I should have been investigating, not gradle...

App Engine allows you to customise the class loader JAR ordering with a little bit of xml in your appengine-web.xml. In my case:

<class-loader-config>
    <priority-specifier filename="my-hack-0.0.1.jar"/>
</class-loader-config>

This places my-hack-0.0.1.jar as the first JAR file to be searched for classes, barring those in the directory war/WEB-INF/classes/.

...Thanks to a nudge in the right direction from @Danilo Tommasina :)

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!