Resolving CppApplication dependencies

How I do iterate CppApplication dependencies? Also, how do I limit it to include only immediate dependencies and ignore transitive ones? I tried the following and it works for a library but not for application.

Set<String> names = new TreeSet<>();

// This works
CppLibrary library = ...;
library.getApiDependencies().withDependencies(ds ->
    ds.forEach(d -> names.add(d.getName())));

// But this doesn't
CppApplication application = ...;
application.getImplementationDependencies().withDependencies(ds ->
    ds.forEach(d -> names.add(d.getName())));

The latter throws this exception -

Caused by: org.gradle.api.InvalidUserDataException: Cannot change dependencies of configuration ':app:implementation' after it has been included in dependency resolution

Appreciate some help!

@Shahji Thanks for this question. Could you provide a bit more information, more specifically a sample that reproduce the exception out-of-the-box. I can’t seems to make it fail. Also, could you either include a build scan or a stacktrace to the exception.

@Daniel_L I can’t seem to be able to reproduce the exception itself anymore. I was trying different things and one among them threw that exception. Anyways, the problem is still there.

Apply this plugin to the static-library project in samples -

public class LogDependenciesPlugin implements Plugin<Project> {
  @Override
  public void apply(Project project) {
    project.getPluginManager().withPlugin("cpp-library", discard -> {
      CppLibrary library = project.getExtensions().getByType(CppLibrary.class);

      library.getApiDependencies().withDependencies(ds ->
          ds.forEach(d -> project.getLogger().lifecycle("Api Dependency: " + d.getName())));

      library.getImplementationDependencies().withDependencies(ds ->
          ds.forEach(d -> project.getLogger().lifecycle("Implementation Dependency: " + d.getName())));
    });

    project.getPluginManager().withPlugin("cpp-application", discard -> {
      CppApplication application = project.getExtensions().getByType(CppApplication.class);

      application.getImplementationDependencies().withDependencies(ds ->
          ds.forEach(d -> project.getLogger().lifecycle("Implementation Dependency: " + d.getName())));
    });
  }
}

It prints the expected result for libraries but the application’s list of dependencies is always empty.
In specific examples, for utilities project, it prints list as a dependency but for app, it prints nothing.

I’m unsure what is not working here. I copied the code over, converted in Groovy since lambdas aren’t supported in Groovy, added to display the project name and got the following result:

[app] Implementation Dependency: utilities
[utilities] Api Dependency: list

It all seems to be correct. What is the issue?

Nevermind @Daniel_L It turned out to be a user error on our part. The logic was picking up a version of plugin from internal maven repo instead of from maven local. Our changes were basically being ignored.

Apologies for dragging you along this path.