Search in sources :

Example 1 with Configuration

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

the class TDependencyManager method resolveDependencyForApplicationConfig.

private void resolveDependencyForApplicationConfig(@NonNull final VariantDependencies variantDeps, @Nullable VariantDependencies testedVariantDeps, @Nullable String testedProjectPath, @NonNull Multimap<AndroidLibrary, Configuration> reverseLibMap) {
    boolean needPackageScope = true;
    if (AndroidGradleOptions.buildModelOnly(project)) {
        // if we're only syncing (building the model), then we only need the package
        // scope if we will actually pass it to the IDE.
        Integer modelLevelInt = AndroidGradleOptions.buildModelOnlyVersion(project);
        int modelLevel = AndroidProject.MODEL_LEVEL_0_ORIGNAL;
        if (modelLevelInt != null) {
            modelLevel = modelLevelInt;
        }
        needPackageScope = modelLevel >= AndroidProject.MODEL_LEVEL_2_DEP_GRAPH;
    }
    Configuration compileClasspath = variantDeps.getCompileConfiguration();
    Configuration packageClasspath = variantDeps.getPackageConfiguration();
    if (DEBUG_DEPENDENCY) {
        System.out.println(">>>>>>>>>>");
        System.out.println(project.getName() + ":" + compileClasspath.getName() + "/" + packageClasspath.getName());
    }
    Set<String> resolvedModules = Sets.newHashSet();
    Map<ModuleVersionIdentifier, List<ResolvedArtifact>> artifacts = Maps.newHashMap();
    collectArtifacts(compileClasspath, artifacts);
    collectArtifacts(packageClasspath, artifacts);
    // 不使用官方的扁平化的依赖处理,改用自己处理树状的依赖关系;对于application的依赖,我们只取compile的依赖
    ResolvedDependencyContainer compileResolvedDependencyContainer = new ResolvedDependencyContainer(project);
    Set<ModuleVersionIdentifier> directDependencies = new HashSet<ModuleVersionIdentifier>();
    Set<? extends DependencyResult> projectDependencies = compileClasspath.getIncoming().getResolutionResult().getRoot().getDependencies();
    for (DependencyResult dependencyResult : projectDependencies) {
        if (dependencyResult instanceof ResolvedDependencyResult) {
            ModuleVersionIdentifier moduleVersion = ((ResolvedDependencyResult) dependencyResult).getSelected().getModuleVersion();
            CircleDependencyCheck circleDependencyCheck = new CircleDependencyCheck(moduleVersion);
            if (!directDependencies.contains(moduleVersion)) {
                directDependencies.add(moduleVersion);
                resolveDependency(compileResolvedDependencyContainer, null, ((ResolvedDependencyResult) dependencyResult).getSelected(), artifacts, variantDeps, 0, circleDependencyCheck, circleDependencyCheck.getRootDependencyNode(), resolvedModules);
            }
        }
    }
    AndroidDependencyTree androidDependencyTree = compileResolvedDependencyContainer.reslovedDependencies().toAndroidDependency();
    AtlasBuildContext.androidDependencyTrees.put(variantDeps.getName(), androidDependencyTree);
    //output tree file only once
    if (project.getLogger().isInfoEnabled()) {
        project.getLogger().info("[dependencyTree" + variantDeps.getName() + "]" + JSON.toJSONString(androidDependencyTree.getDependencyJson(), true));
    }
    // 设置reverseMap
    for (AndroidLibrary libInfo : androidDependencyTree.getAarBundles()) {
        reverseLibMap.put(libInfo, variantDeps.getCompileConfiguration());
    }
    Set<String> currentUnresolvedDependencies = Sets.newHashSet();
    // records the artifact we find during package, to detect provided only dependencies.
    Set<String> artifactSet = Sets.newHashSet();
    // start with package dependencies, record the artifacts
    DependencyContainer packagedDependencies;
    if (needPackageScope) {
        packagedDependencies = gatherDependencies(packageClasspath, variantDeps, reverseLibMap, currentUnresolvedDependencies, testedProjectPath, artifactSet, ScopeType.PACKAGE);
    } else {
        packagedDependencies = DependencyContainerImpl.getEmpty();
    }
    // then the compile dependencies, comparing against the record package dependencies
    // to set the provided flag.
    // if we have not compute the package scope, we disable the computation of
    // provided bits. This disables the checks on impossible provided libs (provided aar in
    // apk project).
    ScopeType scopeType = needPackageScope ? ScopeType.COMPILE : ScopeType.COMPILE_ONLY;
    DependencyContainer compileDependencies = gatherDependencies(compileClasspath, variantDeps, reverseLibMap, currentUnresolvedDependencies, testedProjectPath, artifactSet, scopeType);
    if (extraModelInfo.getMode() != STANDARD && compileClasspath.getResolvedConfiguration().hasError()) {
        for (String dependency : currentUnresolvedDependencies) {
            extraModelInfo.handleSyncError(dependency, SyncIssue.TYPE_UNRESOLVED_DEPENDENCY, String.format("Unable to resolve dependency '%s'", dependency));
        }
    }
    // validate the dependencies.
    if (needPackageScope) {
        variantDeps.getChecker().validate(compileDependencies, packagedDependencies, testedVariantDeps);
    }
    if (DEBUG_DEPENDENCY) {
        System.out.println("*** COMPILE DEPS ***");
        for (AndroidLibrary lib : compileDependencies.getAndroidDependencies()) {
            System.out.println("LIB: " + lib);
        }
        for (JavaLibrary jar : compileDependencies.getJarDependencies()) {
            System.out.println("JAR: " + jar);
        }
        for (JavaLibrary jar : compileDependencies.getLocalDependencies()) {
            System.out.println("LOCAL-JAR: " + jar);
        }
        System.out.println("*** PACKAGE DEPS ***");
        for (AndroidLibrary lib : packagedDependencies.getAndroidDependencies()) {
            System.out.println("LIB: " + lib);
        }
        for (JavaLibrary jar : packagedDependencies.getJarDependencies()) {
            System.out.println("JAR: " + jar);
        }
        for (JavaLibrary jar : packagedDependencies.getLocalDependencies()) {
            System.out.println("LOCAL-JAR: " + jar);
        }
        System.out.println("***");
    }
    variantDeps.setDependencies(compileDependencies, packagedDependencies);
    configureBuild(variantDeps);
    if (DEBUG_DEPENDENCY) {
        System.out.println(project.getName() + ":" + compileClasspath.getName() + "/" + packageClasspath.getName());
        System.out.println("<<<<<<<<<<");
    }
}
Also used : Configuration(org.gradle.api.artifacts.Configuration) DependencyResult(org.gradle.api.artifacts.result.DependencyResult) UnresolvedDependencyResult(org.gradle.api.artifacts.result.UnresolvedDependencyResult) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) ResolvedDependencyContainer(com.taobao.android.builder.dependency.ResolvedDependencyContainer) DependencyContainer(com.android.builder.dependency.DependencyContainer) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) ResolvedDependencyContainer(com.taobao.android.builder.dependency.ResolvedDependencyContainer) JavaLibrary(com.android.builder.model.JavaLibrary) AndroidLibrary(com.android.builder.model.AndroidLibrary) CircleDependencyCheck(com.taobao.android.builder.dependency.CircleDependencyCheck) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) HashSet(java.util.HashSet)

Example 2 with Configuration

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

the class AtlasPlugin method createLibCompenents.

private void createLibCompenents() {
    Configuration compileConfiguration = project.getConfigurations().getByName(COMPILE_CONFIGURATION_NAME);
    project.getComponents().add(new AndroidComponent(compileConfiguration, compileConfiguration.getAllDependencies()));
}
Also used : Configuration(org.gradle.api.artifacts.Configuration)

Example 3 with Configuration

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

the class DependencyManager method parseDependencyTree.

protected AndroidDependencyTree parseDependencyTree(@NonNull VariantDependencies variantDeps) {
    Configuration compileClasspath = variantDeps.getCompileConfiguration();
    Configuration packageClasspath = variantDeps.getPackageConfiguration();
    // TODO - shouldn't need to do this - fix this in Gradle
    ensureConfigured(compileClasspath);
    ensureConfigured(packageClasspath);
    Set<String> currentUnresolvedDependencies = Sets.newHashSet();
    // TODO - defer downloading until required -- This is hard to do as we need the info to build the variant
    // config.
    Map<ModuleVersionIdentifier, List<ResolvedArtifact>> artifacts = Maps.newHashMap();
    collectArtifacts(compileClasspath, artifacts);
    collectArtifacts(packageClasspath, artifacts);
    // 不使用官方的扁平化的依赖处理,改用自己处理树状的依赖关系;对于application的依赖,我们只取compile的依赖
    ResolvedDependencyContainer compileResolvedDependencyContainer = new ResolvedDependencyContainer();
    Set<ModuleVersionIdentifier> directDependencies = new HashSet<ModuleVersionIdentifier>();
    Set<? extends DependencyResult> projectDependencies = compileClasspath.getIncoming().getResolutionResult().getRoot().getDependencies();
    for (DependencyResult dependencyResult : projectDependencies) {
        if (dependencyResult instanceof ResolvedDependencyResult) {
            ModuleVersionIdentifier moduleVersion = ((ResolvedDependencyResult) dependencyResult).getSelected().getModuleVersion();
            CircleDependencyCheck circleDependencyCheck = new CircleDependencyCheck(moduleVersion);
            if (!((HashSet<ModuleVersionIdentifier>) directDependencies).contains(moduleVersion)) {
                ((HashSet<ModuleVersionIdentifier>) directDependencies).add(moduleVersion);
                resolveDependency(compileResolvedDependencyContainer, null, ((ResolvedDependencyResult) dependencyResult).getSelected(), artifacts, variantDeps, 0, circleDependencyCheck, circleDependencyCheck.getRootDependencyNode());
            }
        } else if (dependencyResult instanceof UnresolvedDependencyResult) {
            ComponentSelector attempted = ((UnresolvedDependencyResult) dependencyResult).getAttempted();
            if (attempted != null) {
                ((HashSet<String>) currentUnresolvedDependencies).add(attempted.toString());
            }
        }
    }
    AndroidDependencyTree androidDependencyTree = compileResolvedDependencyContainer.reslovedDependencies().toAndroidDependency();
    return androidDependencyTree;
}
Also used : UnresolvedDependencyResult(org.gradle.api.artifacts.result.UnresolvedDependencyResult) Configuration(org.gradle.api.artifacts.Configuration) ComponentSelector(org.gradle.api.artifacts.component.ComponentSelector) DependencyResult(org.gradle.api.artifacts.result.DependencyResult) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) UnresolvedDependencyResult(org.gradle.api.artifacts.result.UnresolvedDependencyResult) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) List(java.util.List) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) HashSet(java.util.HashSet)

Example 4 with Configuration

use of org.gradle.api.artifacts.Configuration in project intellij-community by JetBrains.

the class ModelBuildScriptClasspathBuilderImpl method buildAll.

@Nullable
@Override
public Object buildAll(final String modelName, final Project project) {
    BuildScriptClasspathModelImpl buildScriptClasspath = cache.get(project.getPath());
    if (buildScriptClasspath != null)
        return buildScriptClasspath;
    if (mySourceSetFinder == null)
        mySourceSetFinder = new SourceSetCachedFinder(project);
    buildScriptClasspath = new BuildScriptClasspathModelImpl();
    final File gradleHomeDir = project.getGradle().getGradleHomeDir();
    buildScriptClasspath.setGradleHomeDir(gradleHomeDir);
    buildScriptClasspath.setGradleVersion(GradleVersion.current().getVersion());
    boolean downloadJavadoc = false;
    boolean downloadSources = true;
    final IdeaPlugin ideaPlugin = project.getPlugins().findPlugin(IdeaPlugin.class);
    if (ideaPlugin != null) {
        final IdeaModule ideaModule = ideaPlugin.getModel().getModule();
        downloadJavadoc = ideaModule.isDownloadJavadoc();
        downloadSources = ideaModule.isDownloadSources();
    }
    Project parent = project.getParent();
    if (parent != null) {
        BuildScriptClasspathModelImpl parentBuildScriptClasspath = (BuildScriptClasspathModelImpl) buildAll(modelName, parent);
        if (parentBuildScriptClasspath != null) {
            for (ClasspathEntryModel classpathEntryModel : parentBuildScriptClasspath.getClasspath()) {
                buildScriptClasspath.add(classpathEntryModel);
            }
        }
    }
    Configuration classpathConfiguration = project.getBuildscript().getConfigurations().findByName(CLASSPATH_CONFIGURATION_NAME);
    if (classpathConfiguration == null)
        return null;
    Collection<ExternalDependency> dependencies = new DependencyResolverImpl(project, false, downloadJavadoc, downloadSources, mySourceSetFinder).resolveDependencies(classpathConfiguration);
    for (ExternalDependency dependency : new DependencyTraverser(dependencies)) {
        if (dependency instanceof ExternalLibraryDependency) {
            final ExternalLibraryDependency libraryDep = (ExternalLibraryDependency) dependency;
            buildScriptClasspath.add(new ClasspathEntryModelImpl(pathSet(libraryDep.getFile()), pathSet(libraryDep.getSource()), pathSet(libraryDep.getJavadoc())));
        }
        if (dependency instanceof ExternalMultiLibraryDependency) {
            ExternalMultiLibraryDependency multiLibraryDependency = (ExternalMultiLibraryDependency) dependency;
            buildScriptClasspath.add(new ClasspathEntryModelImpl(pathSet(multiLibraryDependency.getFiles()), pathSet(multiLibraryDependency.getSources()), pathSet(multiLibraryDependency.getJavadoc())));
        }
        if (dependency instanceof FileCollectionDependency) {
            FileCollectionDependency fileCollectionDependency = (FileCollectionDependency) dependency;
            buildScriptClasspath.add(new ClasspathEntryModelImpl(pathSet(fileCollectionDependency.getFiles()), new HashSet<String>(), new HashSet<String>()));
        }
    }
    cache.put(project.getPath(), buildScriptClasspath);
    return buildScriptClasspath;
}
Also used : BuildScriptClasspathModelImpl(org.jetbrains.plugins.gradle.tooling.internal.BuildScriptClasspathModelImpl) Configuration(org.gradle.api.artifacts.Configuration) IdeaPlugin(org.gradle.plugins.ide.idea.IdeaPlugin) SourceSetCachedFinder(org.jetbrains.plugins.gradle.tooling.util.SourceSetCachedFinder) DependencyTraverser(org.jetbrains.plugins.gradle.tooling.util.DependencyTraverser) ClasspathEntryModelImpl(org.jetbrains.plugins.gradle.tooling.internal.ClasspathEntryModelImpl) Project(org.gradle.api.Project) IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) DependencyResolverImpl(org.jetbrains.plugins.gradle.tooling.util.DependencyResolverImpl) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with Configuration

use of org.gradle.api.artifacts.Configuration in project intellij-community by JetBrains.

the class ModuleExtendedModelBuilderImpl method buildAll.

@Nullable
@Override
public Object buildAll(String modelName, Project project) {
    final String moduleName = project.getName();
    final String moduleGroup = project.getGroup().toString();
    final String moduleVersion = project.getVersion().toString();
    final File buildDir = project.getBuildDir();
    String javaSourceCompatibility = null;
    for (Task task : project.getTasks()) {
        if (task instanceof JavaCompile) {
            JavaCompile javaCompile = (JavaCompile) task;
            javaSourceCompatibility = javaCompile.getSourceCompatibility();
            if (task.getName().equals("compileJava"))
                break;
        }
    }
    final ModuleExtendedModelImpl moduleVersionModel = new ModuleExtendedModelImpl(moduleName, moduleGroup, moduleVersion, buildDir, javaSourceCompatibility);
    final List<File> artifacts = new ArrayList<File>();
    for (Task task : project.getTasks()) {
        if (task instanceof Jar) {
            Jar jar = (Jar) task;
            artifacts.add(jar.getArchivePath());
        }
    }
    moduleVersionModel.setArtifacts(artifacts);
    final Set<String> sourceDirectories = new HashSet<String>();
    final Set<String> testDirectories = new HashSet<String>();
    final Set<String> resourceDirectories = new HashSet<String>();
    final Set<String> testResourceDirectories = new HashSet<String>();
    final List<File> testClassesDirs = new ArrayList<File>();
    for (Task task : project.getTasks()) {
        if (task instanceof Test) {
            Test test = (Test) task;
            testClassesDirs.add(test.getTestClassesDir());
            if (test.hasProperty(TEST_SRC_DIRS_PROPERTY)) {
                Object testSrcDirs = test.property(TEST_SRC_DIRS_PROPERTY);
                if (testSrcDirs instanceof Iterable) {
                    for (Object dir : Iterable.class.cast(testSrcDirs)) {
                        addFilePath(testDirectories, dir);
                    }
                }
            }
        }
    }
    IdeaCompilerOutputImpl compilerOutput = new IdeaCompilerOutputImpl();
    if (project.hasProperty(SOURCE_SETS_PROPERTY)) {
        Object sourceSets = project.property(SOURCE_SETS_PROPERTY);
        if (sourceSets instanceof SourceSetContainer) {
            SourceSetContainer sourceSetContainer = (SourceSetContainer) sourceSets;
            for (SourceSet sourceSet : sourceSetContainer) {
                SourceSetOutput output = sourceSet.getOutput();
                if (SourceSet.TEST_SOURCE_SET_NAME.equals(sourceSet.getName())) {
                    compilerOutput.setTestClassesDir(output.getClassesDir());
                    compilerOutput.setTestResourcesDir(output.getResourcesDir());
                }
                if (SourceSet.MAIN_SOURCE_SET_NAME.equals(sourceSet.getName())) {
                    compilerOutput.setMainClassesDir(output.getClassesDir());
                    compilerOutput.setMainResourcesDir(output.getResourcesDir());
                }
                for (File javaSrcDir : sourceSet.getAllJava().getSrcDirs()) {
                    boolean isTestDir = isTestDir(sourceSet, testClassesDirs);
                    addFilePath(isTestDir ? testDirectories : sourceDirectories, javaSrcDir);
                }
                for (File resourcesSrcDir : sourceSet.getResources().getSrcDirs()) {
                    boolean isTestDir = isTestDir(sourceSet, testClassesDirs);
                    addFilePath(isTestDir ? testResourceDirectories : resourceDirectories, resourcesSrcDir);
                }
            }
        }
    }
    File projectDir = project.getProjectDir();
    IdeaContentRootImpl contentRoot = new IdeaContentRootImpl(projectDir);
    final Set<String> ideaSourceDirectories = new HashSet<String>();
    final Set<String> ideaTestDirectories = new HashSet<String>();
    final Set<String> ideaGeneratedDirectories = new HashSet<String>();
    final Set<File> excludeDirectories = new HashSet<File>();
    enrichDataFromIdeaPlugin(project, excludeDirectories, ideaSourceDirectories, ideaTestDirectories, ideaGeneratedDirectories);
    if (ideaSourceDirectories.isEmpty()) {
        sourceDirectories.clear();
        resourceDirectories.clear();
    }
    if (ideaTestDirectories.isEmpty()) {
        testDirectories.clear();
        testResourceDirectories.clear();
    }
    ideaSourceDirectories.removeAll(resourceDirectories);
    sourceDirectories.removeAll(ideaTestDirectories);
    sourceDirectories.addAll(ideaSourceDirectories);
    ideaTestDirectories.removeAll(testResourceDirectories);
    testDirectories.addAll(ideaTestDirectories);
    // ensure disjoint directories with different type
    resourceDirectories.removeAll(sourceDirectories);
    testDirectories.removeAll(sourceDirectories);
    testResourceDirectories.removeAll(testDirectories);
    for (String javaDir : sourceDirectories) {
        contentRoot.addSourceDirectory(new IdeaSourceDirectoryImpl(new File(javaDir), ideaGeneratedDirectories.contains(javaDir)));
    }
    for (String testDir : testDirectories) {
        contentRoot.addTestDirectory(new IdeaSourceDirectoryImpl(new File(testDir), ideaGeneratedDirectories.contains(testDir)));
    }
    for (String resourceDir : resourceDirectories) {
        contentRoot.addResourceDirectory(new IdeaSourceDirectoryImpl(new File(resourceDir)));
    }
    for (String testResourceDir : testResourceDirectories) {
        contentRoot.addTestResourceDirectory(new IdeaSourceDirectoryImpl(new File(testResourceDir)));
    }
    for (File excludeDir : excludeDirectories) {
        contentRoot.addExcludeDirectory(excludeDir);
    }
    moduleVersionModel.setContentRoots(Collections.<ExtIdeaContentRoot>singleton(contentRoot));
    moduleVersionModel.setCompilerOutput(compilerOutput);
    ConfigurationContainer configurations = project.getConfigurations();
    SortedMap<String, Configuration> configurationsByName = configurations.getAsMap();
    Map<String, Set<File>> artifactsByConfiguration = new HashMap<String, Set<File>>();
    for (Map.Entry<String, Configuration> configurationEntry : configurationsByName.entrySet()) {
        Set<File> files = configurationEntry.getValue().getAllArtifacts().getFiles().getFiles();
        artifactsByConfiguration.put(configurationEntry.getKey(), files);
    }
    moduleVersionModel.setArtifactsByConfiguration(artifactsByConfiguration);
    return moduleVersionModel;
}
Also used : Task(org.gradle.api.Task) SourceSet(org.gradle.api.tasks.SourceSet) Configuration(org.gradle.api.artifacts.Configuration) IdeaContentRootImpl(org.jetbrains.plugins.gradle.tooling.internal.IdeaContentRootImpl) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) SourceSetOutput(org.gradle.api.tasks.SourceSetOutput) ModuleExtendedModelImpl(org.jetbrains.plugins.gradle.tooling.internal.ModuleExtendedModelImpl) SourceSet(org.gradle.api.tasks.SourceSet) Test(org.gradle.api.tasks.testing.Test) IdeaSourceDirectoryImpl(org.jetbrains.plugins.gradle.tooling.internal.IdeaSourceDirectoryImpl) IdeaCompilerOutputImpl(org.jetbrains.plugins.gradle.tooling.internal.IdeaCompilerOutputImpl) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) Jar(org.gradle.api.tasks.bundling.Jar) File(java.io.File) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Configuration (org.gradle.api.artifacts.Configuration)192 File (java.io.File)41 Project (org.gradle.api.Project)36 ConfigurationContainer (org.gradle.api.artifacts.ConfigurationContainer)32 SourceSet (org.gradle.api.tasks.SourceSet)23 DeprecatableConfiguration (org.gradle.internal.deprecation.DeprecatableConfiguration)23 Task (org.gradle.api.Task)22 Dependency (org.gradle.api.artifacts.Dependency)20 SourceSetContainer (org.gradle.api.tasks.SourceSetContainer)19 Action (org.gradle.api.Action)18 Plugin (org.gradle.api.Plugin)17 FileCollection (org.gradle.api.file.FileCollection)17 Usage (org.gradle.api.attributes.Usage)16 Callable (java.util.concurrent.Callable)15 JavaPlugin (org.gradle.api.plugins.JavaPlugin)15 List (java.util.List)14 ResolvedConfiguration (org.gradle.api.artifacts.ResolvedConfiguration)13 TaskProvider (org.gradle.api.tasks.TaskProvider)13 Jar (org.gradle.api.tasks.bundling.Jar)13 Collections (java.util.Collections)12