Alternative to `conf2ScopeMappings` for maven-publish

I’m developing a Gradle plugin that automatically tweaks a POM file generated by Upload tasks with mavenDeployer – by tweaking conf2ScopeMappings.

(JFYI details: It adds a new custom configuration, adds direct dependencies on the new configuration, and registers the new configuration for Maven’s runtime scope. Directly tweaking Gradle’s runtime configuration does not work unfortunately because the added dependencies are calculated from existing runtime dependencies.)


But unfortunately, conf2ScopeMappings does not work for maven-publish. And, Upload and conf2ScopeMappings are expected to be deprecated in future.

The maven plugin (together with the core Upload task) are being replaced by the maven-publish plugin. As such, we don’t plan on making changes to improve the (now legacy) Conf2ScopeMapping.
Expect to see this type deprecated in Gradle 6.x, for possible removal in Gradle 7.0.


Then, I’m looking for an alternative way to conf2ScopeMappings for maven-publish to tweak a mapping from Gradle configurations to Maven scopes.

I tried adding an attribute Usage.JAVA_RUNTIME to the new configuration, but it didn’t work.

alternativeRuntimeConfiguration.getAttributes().attribute(
    Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.JAVA_RUNTIME));

Does anyone know any way?

I finally dived into the Gradle code, and cocluded the following works for maven-publish:

// The Configuration to be additionally processed as Maven's "runtime" scope.
final Configuration targetConfiguration = ...;

final SoftwareComponent component = project.getComponents().getByName("java");  // components.java
if (component instanceof AdhocComponentWithVariants) {
    ((AdhocComponentWithVariants) component).addVariantsFromConfiguration(targetConfiguration, details -> {
        details.mapToMavenScope("runtime");
    });
} else {
    throw new GradleException("Failed to configure components.java because it is not AdhocComponentWithVariants.");
}