Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

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) ?

like image 365
acthota Avatar asked Jan 11 '15 14:01

acthota


People also ask

Does the order of Gradle dependencies matter?

Dependency ordering is preserved, but for example all compile dependencies will be before testCompile dependencies no matter of how you order them. Depending on the nature of your conflict you could also exclude the dependency from 'testCompile' rather than force the correct one higher on the classpath.

What does -> mean in Gradle dependencies?

It means that dependency graph contains multiple dependencies with the same group and module but different versions for e.g. org.

How do you check if a dependency is being used Gradle?

To get started with it, add the following two things to your Gradle build script. Once those are in place, run gradle analyze . If there are unused dependencies, you'll get a build failure that shows output similar to the text below, plus a list of the unused dependencies (both declared and transitive).


1 Answers

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.

like image 73
Invisible Arrow Avatar answered Oct 06 '22 16:10

Invisible Arrow