Publish custom component with dependecies

I’m trying to publish a custom component in my plugin using SoftwareComponentFactory.adhoc(). I have created a variant using an outgoing configurations which contains an artifact. This artifact is built by a task, which transitively depends on a resolvable configuration, which contains external module dependencies. Is Gradle supposed to figure out that the variant depends on these external module dependencies and to list them as dependencies in the Maven POM and the Gradle module metadata when publishing? If not, how would I get Gradle to do this?

Here are some details on how everything is wired together:

  • attaching an artifact to the outgoing configuration
project.getArtifacts().add(hdvlSourcesArchiveElements.getName(), hdvlSourcesArchive.getArchiveFile(), artifact -> {
    artifact.builtBy(hdvlSourcesArchive);
});

// hdvlSourcesArchiveElements is an outgoing configuration
// hdvlSourcesArchive is a Zip task which indirectly depends on the resolvable configuration
  • tie hdvlSourcesArchive to task that depends on resolvable configuration
project.getTasks().register("hdvlSourcesArchive", Zip.class, zip -> {
    zip.from(writeDependenciesFile.getDestination(), it -> {
        it.into(".gradle-hdvl");
    });
    // ... configuration of the output file location
});

// writeDependenciesFile is a task that just prints out the names of the files resolved from the configuration
  • configure writeDependenciesFile task to depend on resolvable configuration
project.getTasks().register("writeDependenciesFile", WriteDependenciesFile.class, writeDependenciesFile -> {
    writeDependenciesFile.getXrunArgsFiles().from(project.getConfigurations().getByName("xrunArgsFiles"));
    // ... configuration of output file location
});

// xrunArgsFile is a resolvable configuration that extends a declarable configuration

When I call the hdvlSourcesArchive task, I see Gradle resolve the configuration and call writeDependenciesFile.

I enable Maven publishing using an adhoc component:

AdhocComponentWithVariants hdvlLibrary =  softwareComponentFactory.adhoc("hdvlLibrary");
project.getComponents().add(hdvlLibrary);
hdvlLibrary.addVariantsFromConfiguration(hdvlSourcesArchiveElements, configurationVariantDetails -> {});
mavenPublication.from(hdvlLibrary);

When publishing to a Maven repository, I see the zip produced by hdvlSourcesArchive being uploaded, a POM being created and a Gradle metadata file. The POM and the metadata files don’t contain any information about the external module dependencies that are used.

I had a look through the Gradle code and in ConfigurationSoftwareComponentVariant (which is what gets used by adhoc software components) there is a line that extracts dependencies from the configuration that defines the variant:

dependencies = configuration.getIncoming().getDependencies().withType(ModuleDependency.class);

My issue is that my outgoing configuration doesn’t contain any incoming dependencies, even though the dependency chain between artifacts, tasks and resolvable configuration seems to be defined.

I also had a look at the JavaBase plugin. I figure that the runtimeElements configuration is similar to my configuration (i.e. it is outgoing). There I see that it explicitly extends from the declarable configurations of the plugin:

runtimeElements.extendsFrom(implementation, runtimeOnly);

Am I wrong in assuming that Gradle would figure out incoming dependencies of an outgoing configuration through the artifact’s (task) dependency chain?

Am I wrong in assuming

I think so, yes.
It would probably be quite dangerous.
Just that a task needs some configuration as input does not mean it is a dependency for the outgoing configuration.
For example imagine that to produce your artifact the task depends on some configuration that contains the tool to actually create the artifact.
That doesn’t mean it is a dependency, not for the outgoing.

So yeah, you probably want hdvlSourcesArchiveElements.extendsFrom(xrunArgsFiles) if that is what you need.