Custom dependency configuration

I want to create a custom dependency configuration in my gradle plugin.

Is it possible to write a custom dependency configuration (example below), and resolve dependencies inside my plugin? For instance, If I mention ‘audit’, my plugin resolve it as implementation('com.audit:audit:xxx).

plugins {
    id 'custom-plugin'
}

dependencies {
    customImplementation()
    customImplementation {
        ['audit', 'session']
    }
}

How about having some class / enum / constant in your plugin classes that you then use in the build script?

I created extension with list property and after evaluate resolve dependencies. But I want to strict using my custom dsl only in dependencies block. And overall an extension doesn’t look as elegant solution.

I didn’t say an extension.
Just have some class with constant AUDIT and value "com.audit:audit:xxx", then use implementation(MyConstants.AUDIT).

While actually using a version catalog might be a better choice.
If you make your plugin a settings plugin, it can define a version catalog and then you can just use that version catalog: Sharing dependency versions between projects

Constant doesn’t fit, because I want to make dependencies block in build script shorter

implementation(customDependency)
implementation(customDependency) {
    capabilities {
        requireCapability(customDependency-audit)
    }
}
implementation(customDependency) {
    capabilities {
        requireCapability(customDependency-session)
    }
}
...
dependencies {
    customImplementation {
        ['audit', 'session']
    }
}

Version catalog is also doesn’t fit, because lots of customers work on 6+ Gradle versions.

And if you just add a custom extension with respective methods?
Then you would have something like:

yourExtension {
    useAudit()
    useSession()
}

and the functions in the extension then add the respective dependencies.

Yes, thank you :slight_smile: I already did it. But can’t find solution how to restrict using my extension.
I want to make using this extention only in dependencies.

dependencies {
    myExtension {
        useAudit()
    }
}

Restrict using in random place in build script.

Add the extension not to the project, but to the dependencies container.