Extract gradle configurations

Hello Gradle community,

I’m trying to implement gradle plugin (in groovy) and I need to retrieve gradle configurations present in build.gradle (in dependencies{} block) without retrieving extended or inherited configurations.
Is there any way to do that?
I have tried :

project.configurations.each { conf ->
    //to filter only on configurations that contains dependencies and eliminate the empty ones
    if ( !conf.dependencies.isEmpty() ) {
        println conf
    }
}

the output contain configurations present in build.gradle and its extended/inherited

What do you try to do?

For one, you should not use each on a Gradle container as you might miss elements added after your code was executed. If you for example use all or configureEach you will also get elements added after your code executed.

Also right away looking at dependencies might not be what you want as dependencies could be added later, unless this is something you do at execution time, which would then be bad-practice. :slight_smile:

What the code you showed should do is indeed listing all configurations that directly have dependencies added, as dependencies give you these, opposed to allDependencies which would give you also the dependencies declared on extended configurations.

Thank’s a lot for your help,

What I am trying to do is to have the list gradle configuration used in my project without having the extended configuration for example, If I have in my build.gradle:

dependencies {
     implementation "groupId:artifactId:version"
     compileClasspath "groupId1:artifactId1:version"
}

I want inside my custom plugin which is applied in my project to loop on only implementation and compileClasspath configurations but not its extended/inherited configurations (compileOnly and implementation for compileClasspath)
I hope that I’m clair :slight_smile:

While I still think this might be a very questionable action, I think it should work like you showed. If it does not work as expected, please provide an MCVE.

Btw. compileClasspath is not meant to declare dependencies on it, only to resolve dependencies.
If you want something only at compile time, use compileOnly. Newer Gradle versions might even prevent this technically.