All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.internal.resolve.ModuleVersionNotFoundException Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.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.
 */
package org.gradle.internal.resolve;

import com.google.common.collect.Lists;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.component.ComponentSelector;
import org.gradle.api.artifacts.component.ModuleComponentSelector;
import org.gradle.internal.Factory;
import org.gradle.internal.logging.text.TreeFormatter;

import java.util.Collection;
import java.util.Iterator;

public class ModuleVersionNotFoundException extends ModuleVersionResolveException {
    /**
     * This is used by {@link ModuleVersionResolveException#withIncomingPaths(java.util.Collection)}.
     */
    @SuppressWarnings("UnusedDeclaration")
    public ModuleVersionNotFoundException(ComponentSelector selector, Factory message) {
        super(selector, message);
    }

    public ModuleVersionNotFoundException(ModuleComponentSelector selector, Collection attemptedLocations, Collection unmatchedVersions, Collection rejectedVersions) {
        super(selector, format(selector, attemptedLocations, unmatchedVersions, rejectedVersions));
    }

    public ModuleVersionNotFoundException(ModuleVersionIdentifier id, Collection attemptedLocations) {
        super(id, format(id, attemptedLocations));
    }

    public ModuleVersionNotFoundException(ModuleComponentSelector selector, Collection attemptedLocations) {
        super(selector, format(selector, attemptedLocations));
    }

    private static Factory format(ModuleComponentSelector selector, Collection locations, Collection unmatchedVersions, Collection rejectedVersions) {
        return () -> {
            TreeFormatter builder = new TreeFormatter();
            if (unmatchedVersions.isEmpty() && rejectedVersions.isEmpty()) {
                builder.node(String.format("Could not find any matches for %s as no versions of %s:%s are available.", selector, selector.getGroup(), selector.getModule()));
            } else {
                builder.node(String.format("Could not find any version that matches %s.", selector));
                if (!unmatchedVersions.isEmpty()) {
                    builder.node("Versions that do not match");
                    appendSizeLimited(builder, unmatchedVersions);
                }
                if (!rejectedVersions.isEmpty()) {
                    Collection byRule = Lists.newArrayListWithExpectedSize(rejectedVersions.size());
                    Collection byAttributes = Lists.newArrayListWithExpectedSize(rejectedVersions.size());
                    mapRejections(rejectedVersions, byRule, byAttributes);
                    if (!byRule.isEmpty()) {
                        builder.node("Versions rejected by component selection rules");
                        appendSizeLimited(builder, byRule);
                    }
                    if (!byAttributes.isEmpty()) {
                        builder.node("Versions rejected by attribute matching");
                        appendSizeLimited(builder, byAttributes);
                    }
                }
            }
            addLocations(builder, locations);
            return builder.toString();
        };
    }

    private static void mapRejections(Collection in, Collection outByRule, Collection outByAttributes) {
        for (RejectedVersion version : in) {
            if (version instanceof RejectedByAttributesVersion) {
                outByAttributes.add(version);
            } else {
                outByRule.add(version);
            }
        }
    }

    private static Factory format(ModuleVersionIdentifier id, Collection locations) {
        return () -> {
            TreeFormatter builder = new TreeFormatter();
            builder.node(String.format("Could not find %s.", id));
            addLocations(builder, locations);
            return builder.toString();
        };
    }

    private static Factory format(ModuleComponentSelector selector, Collection locations) {
        return () -> {
            TreeFormatter builder = new TreeFormatter();
            builder.node(String.format("Could not find any version that matches %s.", selector));
            addLocations(builder, locations);
            return builder.toString();
        };
    }

    private static void appendSizeLimited(TreeFormatter builder, Collection values) {
        builder.startChildren();
        Iterator iterator = values.iterator();
        int count = Math.min(5, values.size());
        for (int i = 0; i < count; i++) {
            Object next = iterator.next();
            if (next instanceof RejectedVersion) {
                ((RejectedVersion) next).describeTo(builder);
            } else {
                builder.node(next.toString());
            }
        }
        if (count < values.size()) {
            builder.node(String.format("+ %d more", values.size() - count));
        }
        builder.endChildren();
    }

    private static void addLocations(TreeFormatter builder, Collection locations) {
        if (locations.isEmpty()) {
            return;
        }
        builder.node("Searched in the following locations");
        builder.startChildren();
        for (String location : locations) {
            builder.node(location);
        }
        builder.endChildren();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy