How can I change the returned order of dependencies inside a configuration?


I develop custom addons for a rather large application. I have no control over the dependencies that are provided by the software/pom and I’m currently trying to adapt our Gradle build script so our devs can use both Eclipse or IDEA.The application will first load all jars my addon needs and then its own jars in almost alphabetical order, meaning CAPITAL letters have precedence over lower case letters.

I’m using a custom “provided” configuration to handle the application dependencies.

So far so good. However, when I try to run the application together with my addons in IDEA the returned order of dependencies breaks the application.

As a workaround I used this in the gradle dependencies block:

compile fileTree(dir: “$lib”, include: ‘*.jar’).

This returns the jars in the order I need. However, it takes away the possibility to jump to the sources of the application both in Eclipse and in IDEA.

I have been trying to sort the dependencies and creating a new configuration on the fly but so far no luck. What I need to do is this (I think):

  • fill a custom configuration with dependencies (more than 217 jars), all managed with maven - get the paths and file names - sort by file name - create new configuration with paths in alphabetical order - make sure, that sources still can be browsed

So, what is the best way to sort a pom controlled configuration? Or alternatively, is there a way to tell gradle where to find the sources for the jars in the lib folder so that Eclipse and IDEA recognize them? At the moment this is only a problem in IDEA, so the next thing I wanted to try is the whenMerged hook and sort the entries in there, hoping that the IDEA Gradle plugin will not revert my changes.

Any Ideas?


‘Configuration’ itself cannot be sorted. Depending on your needs, you could resolve the configuration and sort the resulting collection of files. To enforce a certain dependency order in the IDE, you might have to manipulate the ‘.iml’ file, either on the object level or the XML level. (See ‘IdeaModule’ in the Gradle Build Language Reference.)

Yeah, I’ll look into the .iml manipulation next…

Regarding the sort, of course I can do this: configurations.provided.files.sort { }

But then how do I tell Gradle to use that in the classPath? Currently I have

main.compileClasspath += configurations.provided

You can’t do it this way. You’ll have to manipulate the ‘.iml’.