Is there a way to determine the order in which gradle dependencies (as in the Eclipse IDE) are ordered in the classpath (compile and runtime)?
Asked Answered
H

1

6

I have a critical classpath order issue due to which I need to ensure a JAR 1 comes before JAR 2 in the classpath.

Is there a way to enforce this in the GRADLE_DEPENDENCIES in the Eclipse IDE.

Any insights into how the same can be achieved during packaging the application (distribution) ?

Heppman answered 11/1, 2015 at 14:53 Comment(1)
Yes, you can do it by dependencies, but notice two rules of the higher priority: https://mcmap.net/q/751391/-can-i-force-the-order-of-dependencies-in-my-classpath-with-gradleRadioactivity
S
7

Ideally you should fix the dependencies in your projects so that you have exactly the right set of jars in the classpath. Take a look at how to exclude dependencies in Dependency Management chapter in the Gradle documentation.

If you still want to modify classpath entries for eclipse, here is one possible solution.

You can tinker with the .classpath file generated by the eclipse task in Gradle using the following:

eclipse.classpath.file {
    withXml {
        // This returns a the classpath XML root node (groovy.util.Node)
        def node = it.asNode()

        // Do the re-rodering of classpath entries by modifying the node object
    }
}

Take a look at groovy.util.Node for methods you can use to view/modify the XML node.

Note that you will most likely not have any control over the classpath ordering done when you run the packaged application distribution in a JVM, which will pull you back to square one at runtime when the application actually runs.

Hence, the best solution is to find out the source of the jar dependency that you don't want in your classpath and eliminate it from there, rather than relying on classpath ordering which is risky and unreliable.

Shadow answered 11/1, 2015 at 19:57 Comment(1)
Thanks for the well rounded answer. I have realized that the problem is due to a version conflict of a jar which is being added as a transitive dependency. I have solved the problem by excluding it in the dependency declaration using the "exclude" functionality. Thanks again for the informative answer.Heppman

© 2022 - 2024 — McMap. All rights reserved.