Search in sources :

Example 1 with ComponentIdentifier

use of org.gradle.api.artifacts.component.ComponentIdentifier in project atlas by alibaba.

the class TDependencyManager method addDependency.

private void addDependency(@NonNull ResolvedComponentResult resolvedComponentResult, @NonNull VariantDependencies configDependencies, @NonNull Configuration configuration, @NonNull Collection<LibraryDependency> outLibraries, @NonNull List<JarDependency> outJars, @NonNull Map<ModuleVersionIdentifier, List<LibraryDependency>> alreadyFoundLibraries, @NonNull Map<ModuleVersionIdentifier, List<JarDependency>> alreadyFoundJars, @NonNull Map<ModuleVersionIdentifier, List<ResolvedArtifact>> artifacts, @NonNull Multimap<AndroidLibrary, Configuration> reverseLibMap, @NonNull Set<String> currentUnresolvedDependencies, @Nullable String testedProjectPath, @NonNull List<String> projectChain, @NonNull Set<String> artifactSet, @NonNull ScopeType scopeType, boolean forceProvided, int indent) {
    ModuleVersionIdentifier moduleVersion = resolvedComponentResult.getModuleVersion();
    if (configDependencies.getChecker().checkForExclusion(moduleVersion)) {
        return;
    }
    if (moduleVersion.getName().equals("support-annotations") && moduleVersion.getGroup().equals("com.android.support")) {
        configDependencies.setAnnotationsPresent(true);
    }
    List<LibraryDependency> libsForThisModule = alreadyFoundLibraries.get(moduleVersion);
    List<JarDependency> jarsForThisModule = alreadyFoundJars.get(moduleVersion);
    if (libsForThisModule != null) {
        if (DEBUG_DEPENDENCY) {
            printIndent(indent, "FOUND LIB: " + moduleVersion.getName());
        }
        outLibraries.addAll(libsForThisModule);
        for (AndroidLibrary lib : libsForThisModule) {
            reverseLibMap.put(lib, configuration);
        }
    } else if (jarsForThisModule != null) {
        if (DEBUG_DEPENDENCY) {
            printIndent(indent, "FOUND JAR: " + moduleVersion.getName());
        }
        outJars.addAll(jarsForThisModule);
    } else {
        if (DEBUG_DEPENDENCY) {
            printIndent(indent, "NOT FOUND: " + moduleVersion.getName());
        }
        // new module! Might be a jar or a library
        // get the associated gradlepath
        ComponentIdentifier id = resolvedComponentResult.getId();
        String gradlePath = (id instanceof ProjectComponentIdentifier) ? ((ProjectComponentIdentifier) id).getProjectPath() : null;
        // check if this is a tested app project (via a separate test module).
        // In which case, all the dependencies must become provided.
        boolean childForceProvided = forceProvided;
        if (scopeType == ScopeType.COMPILE && testedProjectPath != null && testedProjectPath.equals(gradlePath)) {
            childForceProvided = true;
        }
        // get the nested components first.
        List<LibraryDependency> nestedLibraries = Lists.newArrayList();
        List<JarDependency> nestedJars = Lists.newArrayList();
        Set<? extends DependencyResult> dependencies = resolvedComponentResult.getDependencies();
        for (DependencyResult dependencyResult : dependencies) {
            if (dependencyResult instanceof ResolvedDependencyResult) {
                ResolvedComponentResult selected = ((ResolvedDependencyResult) dependencyResult).getSelected();
                List<String> newProjectChain = projectChain;
                ComponentIdentifier identifier = selected.getId();
                if (identifier instanceof ProjectComponentIdentifier) {
                    String projectPath = ((ProjectComponentIdentifier) identifier).getProjectPath();
                    int index = projectChain.indexOf(projectPath);
                    if (index != -1) {
                        projectChain.add(projectPath);
                        String path = Joiner.on(" -> ").join(projectChain.subList(index, projectChain.size()));
                        throw new CircularReferenceException("Circular reference between projects: " + path);
                    }
                    newProjectChain = Lists.newArrayList();
                    newProjectChain.addAll(projectChain);
                    newProjectChain.add(projectPath);
                }
                addDependency(selected, configDependencies, configuration, nestedLibraries, nestedJars, alreadyFoundLibraries, alreadyFoundJars, artifacts, reverseLibMap, currentUnresolvedDependencies, testedProjectPath, newProjectChain, artifactSet, scopeType, childForceProvided, indent + 1);
            } else if (dependencyResult instanceof UnresolvedDependencyResult) {
                ComponentSelector attempted = ((UnresolvedDependencyResult) dependencyResult).getAttempted();
                if (attempted != null) {
                    currentUnresolvedDependencies.add(attempted.toString());
                }
            }
        }
        if (DEBUG_DEPENDENCY) {
            printIndent(indent, "BACK2: " + moduleVersion.getName());
            printIndent(indent, "NESTED LIBS: " + nestedLibraries.size());
            printIndent(indent, "NESTED JARS: " + nestedJars.size());
        }
        // now loop on all the artifact for this modules.
        List<ResolvedArtifact> moduleArtifacts = artifacts.get(moduleVersion);
        if (moduleArtifacts != null) {
            for (ResolvedArtifact artifact : moduleArtifacts) {
                MavenCoordinates mavenCoordinates = createMavenCoordinates(artifact);
                boolean provided = forceProvided;
                String coordKey = computeVersionLessCoordinateKey(mavenCoordinates);
                if (scopeType == ScopeType.PACKAGE) {
                    artifactSet.add(coordKey);
                } else if (scopeType == ScopeType.COMPILE) {
                    provided |= !artifactSet.contains(coordKey);
                }
                if (EXT_LIB_ARCHIVE.equals(artifact.getExtension())) {
                    if (DEBUG_DEPENDENCY) {
                        printIndent(indent, "TYPE: AAR");
                    }
                    if (libsForThisModule == null) {
                        libsForThisModule = Lists.newArrayList();
                        alreadyFoundLibraries.put(moduleVersion, libsForThisModule);
                    }
                    String path = computeArtifactPath(moduleVersion, artifact);
                    String name = computeArtifactName(moduleVersion, artifact);
                    if (DEBUG_DEPENDENCY) {
                        printIndent(indent, "NAME: " + name);
                        printIndent(indent, "PATH: " + path);
                    }
                    File explodedDir = project.file(project.getBuildDir() + "/" + FD_INTERMEDIATES + "/exploded-aar/" + path);
                    @SuppressWarnings("unchecked") LibraryDependency LibraryDependency = new LibraryDependency(artifact.getFile(), explodedDir, nestedLibraries, nestedJars, name, artifact.getClassifier(), gradlePath, null, /*requestedCoordinates*/
                    mavenCoordinates, provided);
                    libsForThisModule.add(LibraryDependency);
                    outLibraries.add(LibraryDependency);
                    reverseLibMap.put(LibraryDependency, configuration);
                } else if (EXT_JAR.equals(artifact.getExtension())) {
                    if (DEBUG_DEPENDENCY) {
                        printIndent(indent, "TYPE: JAR");
                    }
                    nestedLibraries.clear();
                    // check this jar does not have a dependency on an library, as this would not work.
                    if (!nestedLibraries.isEmpty()) {
                        // can detect this an accept it.
                        if (testedProjectPath != null && testedProjectPath.equals(gradlePath)) {
                            // if this is a package scope, then skip the dependencies.
                            if (scopeType == ScopeType.PACKAGE) {
                                recursiveLibSkip(nestedLibraries);
                            } else {
                                // if it's compile scope, make it optional.
                                provided = true;
                            }
                            outLibraries.addAll(nestedLibraries);
                        } else {
                            configDependencies.getChecker().handleIssue(createMavenCoordinates(artifact).toString(), SyncIssue.TYPE_JAR_DEPEND_ON_AAR, SyncIssue.SEVERITY_ERROR, String.format("Module '%s' depends on one or more Android Libraries but is a jar", moduleVersion));
                        }
                    }
                    if (jarsForThisModule == null) {
                        jarsForThisModule = Lists.newArrayList();
                        alreadyFoundJars.put(moduleVersion, jarsForThisModule);
                    }
                    JarDependency jarDependency = new JarDependency(artifact.getFile(), nestedJars, mavenCoordinates, gradlePath, provided);
                    // app module then skip it.
                    if (scopeType == ScopeType.PACKAGE && testedProjectPath != null && testedProjectPath.equals(gradlePath)) {
                        jarDependency.skip();
                        //noinspection unchecked
                        recursiveJavaSkip((List<JarDependency>) jarDependency.getDependencies());
                    }
                    if (DEBUG_DEPENDENCY) {
                        printIndent(indent, "JAR-INFO: " + jarDependency.toString());
                    }
                    jarsForThisModule.add(jarDependency);
                    outJars.add(jarDependency);
                } else if (EXT_ANDROID_PACKAGE.equals(artifact.getExtension())) {
                    String name = computeArtifactName(moduleVersion, artifact);
                    configDependencies.getChecker().handleIssue(name, SyncIssue.TYPE_DEPENDENCY_IS_APK, SyncIssue.SEVERITY_ERROR, String.format("Dependency %s on project %s resolves to an APK archive " + "which is not supported as a compilation dependency. File: %s", name, project.getName(), artifact.getFile()));
                } else if ("apklib".equals(artifact.getExtension())) {
                    String name = computeArtifactName(moduleVersion, artifact);
                    configDependencies.getChecker().handleIssue(name, SyncIssue.TYPE_DEPENDENCY_IS_APKLIB, SyncIssue.SEVERITY_ERROR, String.format("Packaging for dependency %s is 'apklib' and is not supported. " + "Only 'aar' libraries are supported.", name));
                } else if ("awb".equals(artifact.getExtension())) {
                    break;
                } else if ("solib".equals(artifact.getExtension())) {
                    break;
                } else {
                    String name = computeArtifactName(moduleVersion, artifact);
                    logger.warning(String.format("Unrecognized dependency: '%s' (type: '%s', extension: '%s')", name, artifact.getType(), artifact.getExtension()));
                }
            }
        }
        if (DEBUG_DEPENDENCY) {
            printIndent(indent, "DONE: " + moduleVersion.getName());
        }
    }
}
Also used : UnresolvedDependencyResult(org.gradle.api.artifacts.result.UnresolvedDependencyResult) JarDependency(com.android.builder.dependency.JarDependency) ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Set(java.util.Set) HashSet(java.util.HashSet) ComponentSelector(org.gradle.api.artifacts.component.ComponentSelector) DependencyResult(org.gradle.api.artifacts.result.DependencyResult) UnresolvedDependencyResult(org.gradle.api.artifacts.result.UnresolvedDependencyResult) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) CircularReferenceException(org.gradle.api.CircularReferenceException) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) MavenCoordinates(com.android.builder.model.MavenCoordinates) AndroidLibrary(com.android.builder.model.AndroidLibrary) LibraryDependency(com.android.builder.dependency.LibraryDependency) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ResolvedComponentResult(org.gradle.api.artifacts.result.ResolvedComponentResult) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) File(java.io.File)

Example 2 with ComponentIdentifier

use of org.gradle.api.artifacts.component.ComponentIdentifier in project atlas by alibaba.

the class DependencyManager method resolveDependency.

/**
     * 解析依赖
     *
     * @param resolvedDependencyContainer
     * @param parent
     * @param resolvedComponentResult
     * @param artifacts
     * @param configDependencies
     * @param indent
     */
private void resolveDependency(ResolvedDependencyContainer resolvedDependencyContainer, ResolvedDependencyInfo parent, ResolvedComponentResult resolvedComponentResult, Map<ModuleVersionIdentifier, List<ResolvedArtifact>> artifacts, VariantDependencies configDependencies, int indent, CircleDependencyCheck circleDependencyCheck, CircleDependencyCheck.DependencyNode node) {
    ModuleVersionIdentifier moduleVersion = resolvedComponentResult.getModuleVersion();
    if (configDependencies.getChecker().checkForExclusion(moduleVersion)) {
        return;
    }
    if (moduleVersion.getName().equals("support-annotations") && moduleVersion.getGroup().equals("com.android.support")) {
        configDependencies.setAnnotationsPresent(true);
    }
    // now loop on all the artifact for this modules.
    List<ResolvedArtifact> moduleArtifacts = artifacts.get(moduleVersion);
    ComponentIdentifier id = resolvedComponentResult.getId();
    String gradlePath = (id instanceof ProjectComponentIdentifier) ? ((ProjectComponentIdentifier) id).getProjectPath() : null;
    // 如果同时找到多个依赖,暂时没法判断是那个真正有用
    for (ResolvedArtifact resolvedArtifact : moduleArtifacts) {
        ResolvedDependencyInfo resolvedDependencyInfo = new ResolvedDependencyInfo(moduleVersion.getVersion(), moduleVersion.getGroup(), moduleVersion.getName(), resolvedArtifact.getType(), resolvedArtifact.getClassifier());
        resolvedDependencyInfo.setIndent(indent);
        resolvedDependencyInfo.setGradlePath(gradlePath);
        resolvedDependencyInfo.setResolvedArtifact(resolvedArtifact);
        String path = computeArtifactPath(moduleVersion, resolvedArtifact);
        String name = computeArtifactName(moduleVersion, resolvedArtifact);
        File explodedDir = project.file(project.getBuildDir().getAbsolutePath() + "/" + FD_INTERMEDIATES + "/exploded-" + resolvedArtifact.getType().toLowerCase() + "/" + path);
        resolvedDependencyInfo.setExplodedDir(explodedDir);
        resolvedDependencyInfo.setDependencyName(name);
        if (null == parent) {
            parent = resolvedDependencyInfo;
        } else {
            resolvedDependencyInfo.setParent(parent);
            parent.getChildren().add(resolvedDependencyInfo);
        }
        Set<? extends DependencyResult> dependencies = resolvedComponentResult.getDependencies();
        for (DependencyResult dep : dependencies) {
            if (dep instanceof ResolvedDependencyResult) {
                ResolvedComponentResult childResolvedComponentResult = ((ResolvedDependencyResult) dep).getSelected();
                CircleDependencyCheck.DependencyNode childNode = circleDependencyCheck.addDependency(childResolvedComponentResult.getModuleVersion(), node, indent + 1);
                CircleDependencyCheck.CircleResult circleResult = circleDependencyCheck.checkCircle(logger);
                if (circleResult.hasCircle) {
                    logger.warning("[CircleDependency]" + StringUtils.join(circleResult.detail, ";"));
                } else {
                    resolveDependency(resolvedDependencyContainer, parent, ((ResolvedDependencyResult) dep).getSelected(), artifacts, configDependencies, indent + 1, circleDependencyCheck, childNode);
                }
            }
        }
        resolvedDependencyContainer.addDependency(resolvedDependencyInfo);
    }
}
Also used : ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) DependencyResult(org.gradle.api.artifacts.result.DependencyResult) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) UnresolvedDependencyResult(org.gradle.api.artifacts.result.UnresolvedDependencyResult) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) ResolvedComponentResult(org.gradle.api.artifacts.result.ResolvedComponentResult) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) File(java.io.File) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult)

Example 3 with ComponentIdentifier

use of org.gradle.api.artifacts.component.ComponentIdentifier in project gradle by gradle.

the class LocalFileDependencyBackedArtifactSet method snapshot.

@Override
public ResolvedArtifactSet snapshot() {
    ComponentIdentifier componentIdentifier = dependencyMetadata.getComponentId();
    if (componentIdentifier != null && !componentFilter.isSatisfiedBy(componentIdentifier)) {
        return ResolvedArtifactSet.EMPTY;
    }
    Set<File> files;
    try {
        files = dependencyMetadata.getFiles().getFiles();
    } catch (Throwable throwable) {
        return new FailingResolvedArtifactSetSnapshot(throwable);
    }
    List<ResolvedVariant> variants = Lists.newArrayListWithCapacity(files.size());
    for (File file : files) {
        ComponentArtifactIdentifier artifactIdentifier;
        if (componentIdentifier == null) {
            artifactIdentifier = new OpaqueComponentArtifactIdentifier(file);
            if (!componentFilter.isSatisfiedBy(artifactIdentifier.getComponentIdentifier())) {
                continue;
            }
        } else {
            artifactIdentifier = new ComponentFileArtifactIdentifier(componentIdentifier, file.getName());
        }
        AttributeContainerInternal variantAttributes = DefaultArtifactAttributes.forFile(file, attributesFactory);
        ResolvedVariant variant = new SingletonFileResolvedVariant(file, artifactIdentifier, variantAttributes);
        variants.add(selector.select(Collections.singleton(variant)));
    }
    return new ResolvedVariantBackedArtifactSetSnapshot(variants, dependencyMetadata);
}
Also used : OpaqueComponentArtifactIdentifier(org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier) ComponentArtifactIdentifier(org.gradle.api.artifacts.component.ComponentArtifactIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) AttributeContainerInternal(org.gradle.api.internal.attributes.AttributeContainerInternal) ComponentFileArtifactIdentifier(org.gradle.internal.component.local.model.ComponentFileArtifactIdentifier) OpaqueComponentArtifactIdentifier(org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier) File(java.io.File)

Example 4 with ComponentIdentifier

use of org.gradle.api.artifacts.component.ComponentIdentifier in project gradle by gradle.

the class DefaultProjectLocalComponentProvider method getLocalComponentMetaData.

private LocalComponentMetadata getLocalComponentMetaData(ProjectInternal project) {
    Module module = project.getModule();
    ModuleVersionIdentifier moduleVersionIdentifier = moduleIdentifierFactory.moduleWithVersion(module.getGroup(), module.getName(), module.getVersion());
    ComponentIdentifier componentIdentifier = newProjectId(project);
    DefaultLocalComponentMetadata metaData = new DefaultLocalComponentMetadata(moduleVersionIdentifier, componentIdentifier, module.getStatus(), (AttributesSchemaInternal) project.getDependencies().getAttributesSchema());
    metaDataBuilder.addConfigurations(metaData, project.getConfigurations().withType(ConfigurationInternal.class));
    return metaData;
}
Also used : ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) DefaultLocalComponentMetadata(org.gradle.internal.component.local.model.DefaultLocalComponentMetadata) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) Module(org.gradle.api.internal.artifacts.Module) ConfigurationInternal(org.gradle.api.internal.artifacts.configurations.ConfigurationInternal)

Example 5 with ComponentIdentifier

use of org.gradle.api.artifacts.component.ComponentIdentifier in project gradle by gradle.

the class DefaultResolvedArtifactsBuilder method visitArtifacts.

@Override
public void visitArtifacts(DependencyGraphNode from, DependencyGraphNode to, int artifactSetId, ArtifactSet artifacts) {
    // Don't collect build dependencies if not required
    if (!buildProjectDependencies) {
        artifacts = new NoBuildDependenciesArtifactSet(artifacts);
    } else {
        ConfigurationMetadata configurationMetadata = to.getMetadata();
        if (configurationMetadata instanceof LocalConfigurationMetadata) {
            // For a dependency from _another_ build to _this_ build, don't make the artifact buildable
            // Making these artifacts buildable leads to poor error reporting due to direct task dependency cycle (losing the intervening build dependencies)
            ComponentIdentifier incomingId = from.getOwner().getComponentId();
            ComponentIdentifier outgoingId = to.getOwner().getComponentId();
            if (incomingId instanceof ProjectComponentIdentifier && outgoingId instanceof ProjectComponentIdentifier) {
                if (!isCurrentBuild(incomingId) && isCurrentBuild(outgoingId)) {
                    artifacts = new NoBuildDependenciesArtifactSet(artifacts);
                }
            }
        }
    }
    collectArtifacts(artifactSetId, artifacts);
}
Also used : ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) LocalConfigurationMetadata(org.gradle.internal.component.local.model.LocalConfigurationMetadata) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) LocalConfigurationMetadata(org.gradle.internal.component.local.model.LocalConfigurationMetadata) ConfigurationMetadata(org.gradle.internal.component.model.ConfigurationMetadata)

Aggregations

ComponentIdentifier (org.gradle.api.artifacts.component.ComponentIdentifier)39 ProjectComponentIdentifier (org.gradle.api.artifacts.component.ProjectComponentIdentifier)17 ModuleComponentIdentifier (org.gradle.api.artifacts.component.ModuleComponentIdentifier)15 File (java.io.File)14 ModuleVersionIdentifier (org.gradle.api.artifacts.ModuleVersionIdentifier)10 OpaqueComponentArtifactIdentifier (org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier)7 NonNull (com.android.annotations.NonNull)6 ArtifactCollection (org.gradle.api.artifacts.ArtifactCollection)6 ResolvedArtifactResult (org.gradle.api.artifacts.result.ResolvedArtifactResult)6 FilteredArtifactCollection (com.android.build.gradle.internal.dependency.FilteredArtifactCollection)5 DependencyResult (org.gradle.api.artifacts.result.DependencyResult)5 ResolvedComponentResult (org.gradle.api.artifacts.result.ResolvedComponentResult)5 ResolvedDependencyResult (org.gradle.api.artifacts.result.ResolvedDependencyResult)5 Module (org.gradle.api.internal.artifacts.Module)5 ResolvedArtifact (org.gradle.api.artifacts.ResolvedArtifact)4 ComponentArtifactIdentifier (org.gradle.api.artifacts.component.ComponentArtifactIdentifier)4 ResolutionResult (org.gradle.api.artifacts.result.ResolutionResult)4 ResolvedVariantResult (org.gradle.api.artifacts.result.ResolvedVariantResult)4 UnresolvedDependencyResult (org.gradle.api.artifacts.result.UnresolvedDependencyResult)4 DefaultModuleComponentIdentifier (org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier)4