'Changing default gradle dependency file extension

I'm creating a gradle plugin. In my plugin I add a new configuration:

project.getConfigurations().create("custom");

All dependencies in the 'custom' configuration have extension 'custom'. If I declare my dependencies like this:

dependencies {
    custom group: 'my-custom', name: 'my-custom', version: '1.0.00'
}

It will fail with an error message like:

Could not resolve all files for configuration ':custom'.
> Could not find my-custom:my-custom:1.0.00.
  Searched in the following locations:
      file:.../my-custom-1.0.00.jar

Note the '.jar' extension. I need to explicitly set the extension like below:

dependencies {
    custom group: 'my-custom', name: 'my-custom', version: '1.0.00', ext: 'custom'
}

However, the user experience isn't great if they have to always specify the extension. Is it possible to change the default extension for my configuration to 'custom'. This way a user would only need to explicitly specify the extension if it differs from the default (which in this case is '.custom')



Solution 1:[1]

Unfortunately this does not seem to be fully supported, ideally this would be done in a similar way as described here: https://github.com/gradle/gradle/issues/1340 (replacing configurations.all with configurations.custom, and ext instead of classifier)

For a work around and hack you can create a closure/method to append the ext and add to custom configuration:

configurations { custom }
def cust = { project.dependencies.custom it << [ext: it.ext?:'custom'] }

dependencies {
    cust group: 'my-custom', name: 'my-custom', version: '1.0.00'
    cust group: 'my-custom', name: 'no-custom', version: '1.0.00', ext: 'override'
}
//note this also works outside dependencies, its not actually used
cust group: 'my-custom', name: 'oh-no-custom', version: '1.0.00'

for a plugin you might just put that in project ext and name it something like customDependency instead of the actual configuration name.

Solution 2:[2]

I just came across the same problem in my (Java-based) Gradle plugin and solved it as follows:

myConfiguration.resolutionStrategy(strategy ->
        strategy.eachDependency(resolveDetails ->
                resolveDetails.artifactSelection(selDetails ->
                        selDetails.selectArtifact("custom", null, null))));

Or alternatively:

myConfiguration.resolutionStrategy(strategy ->
        strategy.dependencySubstitution(substitutions ->
                substitutions.all(depSubst ->
                        depSubst.artifactSelection(selDetails ->
                                selDetails.selectArtifact(
                                        "custom", null, null)))));

With either approach, custom is set as the type which then also determines the default extension (as it is not set explicitly here).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aarjav
Solution 2