// Copyright (C) 2023 Gradle, Inc.
//
// Licensed under the Creative Commons Attribution-Noncommercial-ShareAlike 4.0 International License.;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[artifact-views]]
= Artifact Views

In some cases, it is desirable to customize the artifact resolution process.
The `ArtifactView` API is the primary mechanism for influencing artifact selection in Gradle.

An `ArtifactView` operates on top of the resolved graph but allows you to apply _different attributes_.
It lets you retrieve artifacts that match a new set of criteria, even if they were not part of the original graph resolution.

An `ArtifactView` can:

1. **Select Alternative Variants** such as sources or javadoc, for an entire resolution: Normally, an artifact must match both the graph's attributes and the `ArtifactView` attributes. With `withVariantReselection`, you can select artifacts from any available variants within a component.
2. **Perform Lenient Artifact Selection** and resolution: Using `ArtifactView` with `lenient=true` allows you to ignore missing dependencies and other errors.
3. **Filter Selected Artifacts**: Using `ArtifactView` with `componentFilter` lets you exclude specific components from the selected artifacts.
4. **Trigger Transforms**: Trigger an `ArtifactTransform` to change an artifact from one type to another.

NOTE: The `ArtifactView` can produce results as both a `FileCollection` and an `ArtifactCollection`.
The examples below only demonstrate using a `FileCollection` as the output.

[[sec:performing_variant_reselection]]
== 1. Performing variant reselection

Standard artifact selection can only select between variants of the component selected by the result of graph selection.
However, in some cases, it may be desirable to select artifacts from a variant _parallel_ to the graph node being selected.

Consider the example component structure below, describing a typical local Java library with sources and javadoc:

[source,text]
----
variant 'apiElements'
    artifact set 'jar'
    artifact set 'classes'
    artifact set 'resources'
variant 'runtimeElements'
    artifact set 'jar'
    artifact set 'classes'
    artifact set 'resources'
variant 'javadocElements'
    artifact set 'jar'
variant 'sourcesElements'
    artifact set 'jar'
----

Resolving a Java runtime classpath will select the `runtimeElements` variant from the above example component.
During standard artifact selection, Gradle will select solely from the artifact sets under `runtimeElements`.

However, it is common to want to select all sources or all javadoc for every node in the graph.
Consider the following example which selects all sources for a given runtime classpath:

NOTE: This example uses incubating APIs.

====
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution/kotlin",files="build.gradle.kts[tags=variant-reselection]"]
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution/groovy",files="build.gradle[tags=variant-reselection]"]
====

Using the `ArtifactView.withVariantReselection()` API, Gradle will optionally perform graph variant selection again _before_ performing artifact selection on the new selected variant.
When Gradle selects artifacts for the `runtimeElements` node, it will use the attributes specified on the `ArtifactView` to reselect the graph variant, thus selecting the `sourcesElements` variant instead.
Then, traditional artifact selection will be performed on the `sourcesElements` variant to select the `jar` artifact set.

As a result, the sources jar is resolved for each node:

[source,text]
----
include::{snippetsPath}/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution/tests/resolve-sources.out[]
----

When this API is used, the attributes used for variant reselection are specified solely by the `ArtifactView.getAttributes()` method.
The graph resolution attributes specified on the configuration are completely ignored during variant reselection.

[[sec:performing_lenient_artifact_selection]]
== 2. Performing lenient artifact selection and resolution

The `ArtifactView` API can also be used to perform lenient artifact resolution.
This allows artifact resolution to be performed on a graph that contains failures -- for example when a requested module was not found, the requested module version did not exist, or a conflict was not resolved.
Furthermore, lenient artifact resolution can be used to resolve artifacts when the graph was successfully resolved, but the corresponding artifacts could not be downloaded.

Consider the following example, where some dependencies may not exist:

====
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-lenient/kotlin",files="build.gradle.kts[tags=lenient-resolution-dependencies]"]
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-lenient/groovy",files="build.gradle[tags=lenient-resolution-dependencies]"]
====

Lenient resolution is performed by using the `ArtifactView.lenient()` method:

====
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-lenient/kotlin",files="build.gradle.kts[tags=resolve-lenient]"]
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-lenient/groovy",files="build.gradle[tags=resolve-lenient]"]
====

We can see that the task succeeds with the failing artifact omitted:

[source,text]
----
include::{snippetsPath}/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-lenient/tests/resolve-lenient.out[]
----

[[sec:filtering_selected_artifacts]]
== 3. Filtering selected artifacts

The `ArtifactView` API can be used to filter specific artifacts from the resulting `FileCollection` or `ArtifactCollection`.

`ArtifactViews` allow results to be filtered on a per-component basis.
Using the `ArtifactView.componentFilter(Action)` method, artifacts from the selected variant can be filtered from the resolved result.
The action is passed the `ComponentIdentifier` of the component that owns the variant that artifacts are being selected for.

Consider the following example, where we have one project dependency and one external dependency:

====
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-componentFilter/kotlin",files="build.gradle.kts[tags=component-filter-dependencies]"]
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-componentFilter/groovy",files="build.gradle[tags=component-filter-dependencies]"]
====

Using the `componentFilter` method, we can specify filters that select only artifacts of a certain type:

====
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-componentFilter/kotlin",files="build.gradle.kts[tags=resolve-component-filter]"]
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-componentFilter/groovy",files="build.gradle[tags=resolve-component-filter]"]
====

Notice how we resolve project dependencies and module dependencies separately:

[source,text]
----
include::{snippetsPath}/reference/dependency-management/controlling-dependency-resolution/performingResolution-artifactResolution-componentFilter/tests/resolve-component-filter.out[]
----

[[sec:triggering_artifact_transforms]]
== 4. Triggering artifact transforms

An `ArtifactView` can be used to trigger artifact selection using _attributes_ different from those used to resolve the graph.

For each node in the graph, artifact selection is performed for that node.
Most commonly, this API is used to request attributes that are not present on the selected artifact.
When Gradle cannot find a matching set of artifacts from the node in question, it will attempt to satisfy the request by transforming the available artifact using the artifact transforms registered on the project.

Below, we use the unzip example from the <<artifact_transforms.adoc#sec:implementing-artifact-transforms,artifact transforms>> chapter to demonstrate how to use the `ArtifactView` API to request attributes that trigger a transform:

====
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/artifactTransforms-unzip/kotlin",files="build.gradle.kts[tags=resolve-transformed-files]"]
include::sample[dir="snippets/reference/dependency-management/controlling-dependency-resolution/artifactTransforms-unzip/groovy",files="build.gradle[tags=resolve-transformed-files]"]
====

Gradle performs artifact selection using the graph resolution attributes specified on the configuration, concatenated with the attributes specified in the `attributes` block of the `ArtifactView`.

The task output shows that the artifacts have been transformed:

[source,text]
----
include::{snippetsPath}/reference/dependency-management/controlling-dependency-resolution/artifactTransforms-unzip/tests/artifactTransformUnzip.out[]
----

The transform code (`ArtifactTransform`) used uncompress the JAR file (from `ZIP` to `UNZIP`) can be seen in the next chapter.

[.text-right]
**Next Step:** <<artifact_transforms.adoc#artifact-transforms,Learn about Artifact Transforms>> >>
