Can't find replacement to Configuration fileCollection(Dependency...)

Hi

We are in process of preparing to Gradle 9
We are now working on eliminating the use of Configuration#fileCollection(org.gradle.api.artifacts.Dependency…)

The document says:

Deprecated
Use getIncoming().artifactView(Action) with a componentFilter instead.

But it is not quite the same

conf.getIncoming().artifactView(view -> view.componentFilter(component -> {
                if (component instanceof ModuleComponentIdentifier md) {

                    return md.getGroup().equals(dep.getGroup()) &&
                           md.getVersion().equals(dep.getVersion()) &&
                           md.getModule().equals(dep.getName());
                }

                return false;
            }))

Behaves differnetly, is seems - there is not a lot of documnetation - that is select artifacts after resolution, so:

  1. transitive are ignored
  2. if dep was upgradle - due to conflict resolution - it will not be selected.

I hope I was clear :slight_smile:

Can you help me implement it correctly - select artifacts that are resolved for given dependency request ?

Thanks
Boaz

According to the last paragraph at Filtered Configuration file and fileCollection methods are deprecated this seems to be intentional and because “This allows for more granular control over which artifacts are selected.”

“more granular” probably means PITA to achieve the same result.

I’m not sure, but I guess you need something like

val ResolvedComponentResult.allComponents: List<ResolvedComponentResult>
    get() {
        return dependencies
            .filterIsInstance<ResolvedDependencyResult>()
            .filterNot { it.isConstraint }
            .flatMap { it.selected.allComponents } + this
    }
val ResolvedComponentResult.allDependencies: List<ComponentIdentifier>
    get() {
        return dependencies
            .filterIsInstance<ResolvedDependencyResult>()
            .filterNot { it.isConstraint }
            .flatMap { it.selected.allDependencies } + id
    }
runtimeClasspath
    .incoming
    .resolutionResult
    .rootComponent
    .flatMap {
        val wantedComponents = it
            .allComponents
            .filter { (it.id as? ModuleComponentIdentifier)?.let { (it.group == "wanted-group") && (it.module == "wanted-module") } ?: false }
            .flatMap { it.allDependencies }
        runtimeClasspath
            .incoming
            .artifactView { componentFilter { it in wantedComponents } }
            .files
            .elements
    }

to achieve the same result.

From a quick try this seems to produce the intended result.

You should probably open a feature request ticket to get something built-in that behaves like the old method or at least get some documentation about how to do it properly.

Appreciate it, I will try