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)));
}
}
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);
}
}
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() + "'.");
}
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);
}
}
}
Aggregations