Search in sources :

Example 36 with ComponentIdentifier

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

the class AtlasDependencyGraph method handleSources.

private static void handleSources(@NonNull Project project, @NonNull Set<ComponentIdentifier> artifacts, @NonNull Consumer<SyncIssue> failureConsumer) {
    final DependencyHandler dependencies = project.getDependencies();
    try {
        ArtifactResolutionQuery query = dependencies.createArtifactResolutionQuery();
        query.forComponents(artifacts);
        @SuppressWarnings("unchecked") Class<? extends Artifact>[] artifactTypesArray = (Class<? extends Artifact>[]) new Class<?>[] { SourcesArtifact.class };
        query.withArtifacts(JvmLibrary.class, artifactTypesArray);
        query.execute().getResolvedComponents();
    } catch (Throwable t) {
        DependencyFailureHandlerKt.processDependencyThrowable(t, s -> null, (data, messages) -> failureConsumer.accept(new SyncIssueImpl(SyncIssue.TYPE_GENERIC, SyncIssue.SEVERITY_WARNING, null, String.format("Unable to download sources: %s", messages.get(0)), messages)));
    }
}
Also used : ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) AtlasPlugin(com.taobao.android.builder.AtlasPlugin) AtlasAndroidArtifacts(com.android.build.gradle.internal.publishing.AtlasAndroidArtifacts) CreatingCache(com.android.ide.common.caching.CreatingCache) Matcher(java.util.regex.Matcher) FileUtils(com.android.utils.FileUtils) ResolvedVariantResult(org.gradle.api.artifacts.result.ResolvedVariantResult) com.google.common.collect(com.google.common.collect) MavenCoordinatesImpl(com.android.builder.dependency.MavenCoordinatesImpl) Artifact(org.gradle.api.component.Artifact) GraphItem(com.android.builder.model.level2.GraphItem) Project(org.gradle.api.Project) Nullable(com.android.annotations.Nullable) AndroidArtifacts(com.android.build.gradle.internal.publishing.AndroidArtifacts) BooleanOption(com.android.build.gradle.options.BooleanOption) ComponentArtifactIdentifier(org.gradle.api.artifacts.component.ComponentArtifactIdentifier) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) Spec(org.gradle.api.specs.Spec) NonNull(com.android.annotations.NonNull) VariantType(com.android.builder.core.VariantType) ARTIFACT_TYPE(com.android.build.gradle.internal.publishing.AndroidArtifacts.ARTIFACT_TYPE) com.android.build.gradle.internal.ide.level2(com.android.build.gradle.internal.ide.level2) Pattern(java.util.regex.Pattern) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) EMPTY_DEPENDENCY_GRAPH(com.android.build.gradle.internal.ide.ModelBuilder.EMPTY_DEPENDENCY_GRAPH) JvmLibrary(org.gradle.jvm.JvmLibrary) java.util(java.util) SdkConstants(com.android.SdkConstants) OpaqueComponentArtifactIdentifier(org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier) VariantAttr(com.android.build.gradle.internal.dependency.VariantAttr) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ConfigurationDependencyGraphs(com.android.build.gradle.internal.dependency.ConfigurationDependencyGraphs) VariantScope(com.android.build.gradle.internal.scope.VariantScope) EMPTY_DEPENDENCIES_IMPL(com.android.build.gradle.internal.ide.ModelBuilder.EMPTY_DEPENDENCIES_IMPL) Configuration(org.gradle.api.artifacts.Configuration) ImmutableCollectors(com.android.utils.ImmutableCollectors) DependencyHandler(org.gradle.api.artifacts.dsl.DependencyHandler) AttributeContainer(org.gradle.api.attributes.AttributeContainer) com.android.builder.model(com.android.builder.model) ArtifactResolutionQuery(org.gradle.api.artifacts.query.ArtifactResolutionQuery) SourcesArtifact(org.gradle.language.base.artifact.SourcesArtifact) Action(org.gradle.api.Action) Library(com.android.builder.model.level2.Library) File(java.io.File) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) Consumer(java.util.function.Consumer) ArtifactCollectionWithExtraArtifact(com.android.build.gradle.internal.dependency.ArtifactCollectionWithExtraArtifact) JavaLibraryImpl(com.android.build.gradle.internal.ide.level2.JavaLibraryImpl) Preconditions(com.google.common.base.Preconditions) DependencyGraphs(com.android.builder.model.level2.DependencyGraphs) DependencyHandler(org.gradle.api.artifacts.dsl.DependencyHandler) ArtifactResolutionQuery(org.gradle.api.artifacts.query.ArtifactResolutionQuery) Artifact(org.gradle.api.component.Artifact) SourcesArtifact(org.gradle.language.base.artifact.SourcesArtifact) ArtifactCollectionWithExtraArtifact(com.android.build.gradle.internal.dependency.ArtifactCollectionWithExtraArtifact)

Example 37 with ComponentIdentifier

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

the class AtlasDependencyGraph method createDependencies.

/**
 * Create a level 1 dependency list.
 */
@NonNull
public DependenciesImpl createDependencies(@NonNull VariantScope variantScope, boolean downloadSources, @NonNull Consumer<SyncIssue> failureConsumer) {
    try {
        ImmutableList.Builder<String> projects = ImmutableList.builder();
        ImmutableList.Builder<AndroidLibrary> androidLibraries = ImmutableList.builder();
        ImmutableList.Builder<JavaLibrary> javaLibrary = ImmutableList.builder();
        // get the runtime artifact. We only care about the ComponentIdentifier so we don't
        // need to call getAllArtifacts() which computes a lot more many things.
        // Instead just get all the jars to get all the dependencies.
        ArtifactCollection runtimeArtifactCollection = computeArtifactList(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.ALL, AtlasAndroidArtifacts.AtlasArtifactType.JAR);
        // build a list of the artifacts
        Set<ComponentIdentifier> runtimeIdentifiers = new HashSet<>(runtimeArtifactCollection.getArtifacts().size());
        for (ResolvedArtifactResult result : runtimeArtifactCollection.getArtifacts()) {
            runtimeIdentifiers.add(result.getId().getComponentIdentifier());
        }
        Set<AtlasDependencyGraph.HashableResolvedArtifactResult> artifacts = getAllArtifacts(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.COMPILE_CLASSPATH, dependencyFailureHandler);
        for (AtlasDependencyGraph.HashableResolvedArtifactResult artifact : artifacts) {
            ComponentIdentifier id = artifact.getId().getComponentIdentifier();
            boolean isProvided = !runtimeIdentifiers.contains(id);
            boolean isSubproject = id instanceof ProjectComponentIdentifier;
            String projectPath = isSubproject ? ((ProjectComponentIdentifier) id).getProjectPath() : null;
            if (artifact.getDependencyType() == DependencyType.JAVA) {
                if (projectPath != null) {
                    projects.add(projectPath);
                    continue;
                }
                // FIXME: Dependencies information is not set correctly.
                javaLibrary.add(new com.android.build.gradle.internal.ide.JavaLibraryImpl(artifact.getFile(), null, ImmutableList.of(), /* dependencies */
                null, /* requestedCoordinates */
                Preconditions.checkNotNull(sMavenCoordinatesCache.get(artifact)), false, /* isSkipped */
                isProvided));
            } else {
                if (artifact.isWrappedModule()) {
                    // force external dependency mode.
                    projectPath = null;
                }
                final File explodedFolder = artifact.getFile();
                // noinspection VariableNotUsedInsideIf
                androidLibraries.add(new com.android.build.gradle.internal.ide.AndroidLibraryImpl(// FIXME: Dependencies information is not set correctly.
                Preconditions.checkNotNull(sMavenCoordinatesCache.get(artifact)), projectPath, artifact.bundleResult != null ? artifact.bundleResult.getFile() : explodedFolder, // fallback so that the value is non-null
                explodedFolder, /*exploded folder*/
                getVariant(artifact), isProvided, false, /* dependencyItem.isSkipped() */
                ImmutableList.of(), /* androidLibraries */
                ImmutableList.of(), /* javaLibraries */
                findLocalJarsAsFiles(explodedFolder)));
            }
        }
        // force download the source artifacts of the compile classpath only.
        if (downloadSources) {
            Set<ComponentIdentifier> ids = Sets.newHashSetWithExpectedSize(artifacts.size());
            for (AtlasDependencyGraph.HashableResolvedArtifactResult artifact : artifacts) {
                ids.add(artifact.getId().getComponentIdentifier());
            }
            handleSources(variantScope.getGlobalScope().getProject(), ids, failureConsumer);
        }
        return new DependenciesImpl(androidLibraries.build(), javaLibrary.build(), projects.build());
    } finally {
        dependencyFailureHandler.collectIssues().forEach(failureConsumer);
    }
}
Also used : ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) File(java.io.File) NonNull(com.android.annotations.NonNull)

Example 38 with ComponentIdentifier

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

the class AtlasDependencyGraph method computeMavenCoordinates.

@NonNull
private static MavenCoordinates computeMavenCoordinates(@NonNull ResolvedArtifactResult artifact) {
    // instance should be a hashable.
    AtlasDependencyGraph.HashableResolvedArtifactResult hashableResult = (AtlasDependencyGraph.HashableResolvedArtifactResult) artifact;
    ComponentIdentifier id = artifact.getId().getComponentIdentifier();
    final File artifactFile = artifact.getFile();
    final String fileName = artifactFile.getName();
    String extension = hashableResult.getDependencyType().getExtension();
    if (id instanceof ModuleComponentIdentifier) {
        ModuleComponentIdentifier moduleComponentId = (ModuleComponentIdentifier) id;
        final String module = moduleComponentId.getModule();
        final String version = moduleComponentId.getVersion();
        String classifier = null;
        if (!artifact.getFile().isDirectory()) {
            // attempts to compute classifier based on the filename.
            String pattern = "^" + module + "-" + version + "-(.+)\\." + extension + "$";
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(fileName);
            if (m.matches()) {
                classifier = m.group(1);
            }
        }
        return new MavenCoordinatesImpl(moduleComponentId.getGroup(), module, version, extension, classifier);
    } else if (id instanceof ProjectComponentIdentifier) {
        return new MavenCoordinatesImpl("artifacts", ((ProjectComponentIdentifier) id).getProjectPath(), "unspecified");
    } else if (id instanceof OpaqueComponentArtifactIdentifier) {
        // We have a file based dependency
        if (hashableResult.getDependencyType() == DependencyType.JAVA) {
            return getMavenCoordForLocalFile(artifactFile);
        } else {
            // local aar?
            assert artifactFile.isDirectory();
            return getMavenCoordForLocalFile(artifactFile);
        }
    }
    throw new RuntimeException("Don't know how to compute maven coordinate for artifact '" + artifact.getId().getDisplayName() + "' with component identifier of type '" + id.getClass() + "'.");
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) MavenCoordinatesImpl(com.android.builder.dependency.MavenCoordinatesImpl) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) OpaqueComponentArtifactIdentifier(org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier) File(java.io.File) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) NonNull(com.android.annotations.NonNull)

Example 39 with ComponentIdentifier

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

the class DependencyResolver method resolveDependency.

/**
 * Analytical dependence
 *  @param parent
 * @param resolvedComponentResult
 * @param artifacts
 * @param configDependencies
 * @param indent
 * @param mainBundle
 */
private void resolveDependency(ResolvedDependencyInfo parent, ResolvedComponentResult resolvedComponentResult, Map<ModuleVersionIdentifier, List<ResolvedArtifact>> artifacts, VariantDependencies configDependencies, int indent, CircleDependencyCheck circleDependencyCheck, CircleDependencyCheck.DependencyNode node, Multimap<String, ResolvedDependencyInfo> dependenciesMap, Set<String> resolvedDependencies, boolean mainBundle) {
    ModuleVersionIdentifier moduleVersion = resolvedComponentResult.getModuleVersion();
    // }
    if (!ids.contains(moduleVersion.getGroup() + ":" + moduleVersion.getName())) {
        artifacts.put(moduleVersion, Lists.newArrayList((DependencyGroup.bundleCompileId.get(moduleVersion))));
    }
    // 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;
    // If you can find multiple dependencies at the same time, you can't judge for the time being that it's really useful
    if (null != moduleArtifacts) {
        for (ResolvedArtifact resolvedArtifact : moduleArtifacts) {
            String key = moduleVersion.getGroup() + ":" + moduleVersion.getName();
            // remove android.jar
            if (key.equals("com.google.android:android")) {
                continue;
            }
            if (mainDependencies.contains(key)) {
                continue;
            }
            if (resolvedDependencies.contains(key)) {
                continue;
            }
            // 
            resolvedDependencies.add(key);
            boolean isAwbBundle = bundleProvidedMap.containsKey(key);
            Set<String> providedDirectDep = bundleProvidedMap.get(key);
            ResolvedDependencyInfo resolvedDependencyInfo = new ResolvedDependencyInfo(moduleVersion.getVersion(), moduleVersion.getGroup(), moduleVersion.getName(), !mainBundle ? "awb" : resolvedArtifact.getType(), resolvedArtifact.getClassifier());
            resolvedDependencyInfo.setIndent(indent);
            resolvedDependencyInfo.setGradlePath(gradlePath);
            resolvedDependencyInfo.setResolvedArtifact(resolvedArtifact);
            String path = AtlasDepHelper.computeArtifactPath(moduleVersion, resolvedArtifact);
            String name = AtlasDepHelper.computeArtifactName(moduleVersion, resolvedArtifact);
            MavenCoordinates mavenCoordinates = DependencyConvertUtils.convert(resolvedArtifact, !mainBundle ? DependencyConvertUtils.Type.AWB : DependencyConvertUtils.Type.AAR);
            File explodedDir = DependencyLocationManager.getExploreDir(project, mavenCoordinates, resolvedArtifact.getFile(), !mainBundle ? "awb" : resolvedArtifact.getType().toLowerCase(), path);
            resolvedDependencyInfo.setExplodedDir(explodedDir);
            resolvedDependencyInfo.setDependencyName(name);
            if (null == parent) {
                parent = resolvedDependencyInfo;
            } else {
                resolvedDependencyInfo.setParent(parent);
                parent.getChildren().add(resolvedDependencyInfo);
            }
            Set<DependencyResult> dependencyResults = null;
            Set<? extends DependencyResult> dependencies = (Set<DependencyResult>) resolvedComponentResult.getDependencies();
            if (bundleCompileMap.containsKey(resolvedDependencyInfo.getGroup() + ":" + resolvedDependencyInfo.getName())) {
                dependencyResults = bundleCompileMap.get(parent.getGroup() + ":" + parent.getName());
            }
            Set<DependencyResult> combineDependencies = combine(dependencyResults, dependencies);
            if (null != combineDependencies) {
                for (DependencyResult dep : combineDependencies) {
                    if (dep instanceof ResolvedDependencyResult) {
                        ResolvedComponentResult childResolvedComponentResult = ((ResolvedDependencyResult) dep).getSelected();
                        if (isAwbBundle && providedDirectDep.contains(childResolvedComponentResult.getModuleVersion().getGroup() + ":" + childResolvedComponentResult.getModuleVersion().getName())) {
                            continue;
                        }
                        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(parent, ((ResolvedDependencyResult) dep).getSelected(), artifacts, configDependencies, indent + 1, circleDependencyCheck, childNode, dependenciesMap, resolvedDependencies, true);
                        }
                    }
                }
            }
            addDependencyInfo(resolvedDependencyInfo, null, dependenciesMap);
        }
    }
}
Also used : ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) ResolvedDependencyInfo(com.taobao.android.builder.dependency.parser.ResolvedDependencyInfo) DependencyResult(org.gradle.api.artifacts.result.DependencyResult) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) MavenCoordinates(com.android.builder.model.MavenCoordinates) CircleDependencyCheck(com.taobao.android.builder.dependency.parser.CircleDependencyCheck) 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)

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