How to define a plugin to create a configuration like "implementation" correctly

I’ve been trying to make a plugin to solve a problem, that annotationProcessor will not be able to access version information form implementation(platform())

Here is my original script in build.gradle.kts, which works fine

val bom by configurations.creating
configurations {
    matching {
        it.name.contains("classpath") || it.name.contains("annotationProcessor")
    }
        .forEach { it.extendsFrom(bom) }
}

And here is my plugin code in Java, I’m trying to copy the same logic to Java code, but it just not works

public class BomPlugin implements Plugin<Project> {

    @Override
    public void apply(Project project) {
        ConfigurationContainer configurations = project.getConfigurations();
        configurations.create("bom", bom -> {
            bom.setCanBeConsumed(true);
            bom.setCanBeResolved(false);
            bom.setDescription("a dependency configuration which include traditional classpath like implementation, " +
                    "and other additional classpath like annotationProcessor");
            configurations
                    .matching(it -> it.getName().contains("classpath")
                            || it.getName().equals("annotationProcessor"))
                    .all(it -> it.extendsFrom(bom));
        });
    }
}

There always is an error:

Configuration with name ‘bom’ not found.

And I also tried to find some documents. I dont know if it is my misunderstanding, I tried to change the canBeConsumed and canBeResolved settings. But still, it is not working.

https://docs.gradle.org/current/userguide/declaring_dependencies.html#sec:resolvable-consumable-configs

Also, if I tried to create a configuration named “bom”, there will be another exception:

Cannot add a configuration with name ‘bom’ as a configuration with that name already exists

Oh, I’m confused. Is it existed or not ?

Help me, my friend. How can I create a configuration like “implementation”

canBeResolved and canBeConsumed should both be set to false for this case, but it has nothing to do with your problem.

You have the configurations.matching...all... within the lambda that configures bom, so at the time this is executed the configuration is about to be created, but not present yet.

Save the result of calling configurations.create in a variable and move the configurations.matching... out of that lambda, then it should probably work as expected.

Thx mate. But I found why it not works, it’s my fault. I import it by the plugins {} block, but I forget to configure it in the subprojects. When I used it in the subprojects {} block, it never be found.

You are pretty a life saver.

1 Like