Search in sources :

Example 26 with ModuleComponentSelector

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

the class RepositoryChainDependencyToComponentIdResolver method resolve.

public void resolve(DependencyMetadata dependency, ResolvedVersionConstraint resolvedVersionConstraint, BuildableComponentIdResolveResult result) {
    ComponentSelector componentSelector = dependency.getSelector();
    if (componentSelector instanceof ModuleComponentSelector) {
        ModuleComponentSelector module = (ModuleComponentSelector) componentSelector;
        if (resolvedVersionConstraint == null) {
            // TODO:DAZ This shouldn't be required, but `ExternalResourceResolverDescriptorParseContext` does not provide a resolved constraint
            VersionConstraint raw = module.getVersionConstraint();
            resolvedVersionConstraint = new DefaultResolvedVersionConstraint(raw, versionSelectorScheme);
        }
        VersionSelector preferredSelector = resolvedVersionConstraint.getPreferredSelector();
        if (preferredSelector.isDynamic()) {
            dynamicRevisionResolver.resolve(toModuleDependencyMetadata(dependency), preferredSelector, resolvedVersionConstraint.getRejectedSelector(), result);
        } else {
            String version = resolvedVersionConstraint.getPreferredVersion();
            ModuleComponentIdentifier id = new DefaultModuleComponentIdentifier(module.getGroup(), module.getModule(), version);
            ModuleVersionIdentifier mvId = moduleIdentifierFactory.moduleWithVersion(module.getGroup(), module.getModule(), version);
            result.resolved(id, mvId);
        }
    }
}
Also used : ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) DefaultResolvedVersionConstraint(org.gradle.api.internal.artifacts.dependencies.DefaultResolvedVersionConstraint) VersionConstraint(org.gradle.api.artifacts.VersionConstraint) ResolvedVersionConstraint(org.gradle.api.internal.artifacts.ResolvedVersionConstraint) ComponentSelector(org.gradle.api.artifacts.component.ComponentSelector) ModuleComponentSelector(org.gradle.api.artifacts.component.ModuleComponentSelector) ModuleComponentSelector(org.gradle.api.artifacts.component.ModuleComponentSelector) DefaultResolvedVersionConstraint(org.gradle.api.internal.artifacts.dependencies.DefaultResolvedVersionConstraint) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) DefaultModuleComponentIdentifier(org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier) DefaultModuleComponentIdentifier(org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier) VersionSelector(org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.VersionSelector)

Example 27 with ModuleComponentSelector

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

the class VcsDependencyResolver method resolve.

@Override
public void resolve(DependencyMetadata dependency, ResolvedVersionConstraint versionConstraint, BuildableComponentIdResolveResult result) {
    if (dependency.getSelector() instanceof ModuleComponentSelector) {
        final ModuleComponentSelector depSelector = (ModuleComponentSelector) dependency.getSelector();
        VersionControlSpec spec = vcsResolver.locateVcsFor(depSelector);
        // TODO: Need failure handling, e.g., cannot clone repository
        if (spec != null) {
            VersionControlSystem versionControlSystem = versionControlSystemFactory.create(spec);
            VersionRef selectedVersion = selectVersion(depSelector, spec, versionControlSystem);
            if (selectedVersion == null) {
                result.failed(new ModuleVersionNotFoundException(depSelector, Collections.singleton(spec.getDisplayName())));
                return;
            }
            File dependencyWorkingDir = new File(populateWorkingDirectory(baseWorkingDir, spec, versionControlSystem, selectedVersion), spec.getRootDir());
            // TODO: This shouldn't rely on the service registry to find NestedBuildFactory
            IncludedBuildRegistry includedBuildRegistry = serviceRegistry.get(IncludedBuildRegistry.class);
            NestedBuildFactory nestedBuildFactory = serviceRegistry.get(NestedBuildFactory.class);
            IncludedBuildInternal includedBuild = includedBuildRegistry.addImplicitBuild(((AbstractVersionControlSpec) spec).getBuildDefinition(dependencyWorkingDir), nestedBuildFactory);
            Collection<Pair<ModuleVersionIdentifier, ProjectComponentIdentifier>> moduleToProject = includedBuild.getAvailableModules();
            Pair<ModuleVersionIdentifier, ProjectComponentIdentifier> entry = CollectionUtils.findFirst(moduleToProject, new Spec<Pair<ModuleVersionIdentifier, ProjectComponentIdentifier>>() {

                @Override
                public boolean isSatisfiedBy(Pair<ModuleVersionIdentifier, ProjectComponentIdentifier> entry) {
                    ModuleVersionIdentifier possibleMatch = entry.left;
                    return depSelector.getGroup().equals(possibleMatch.getGroup()) && depSelector.getModule().equals(possibleMatch.getName());
                }
            });
            if (entry == null) {
                result.failed(new ModuleVersionResolveException(depSelector, spec.getDisplayName() + " did not contain a project publishing the specified dependency."));
            } else {
                LocalComponentMetadata componentMetaData = localComponentRegistry.getComponent(entry.right);
                if (componentMetaData == null) {
                    result.failed(new ModuleVersionResolveException(DefaultProjectComponentSelector.newSelector(includedBuild.getModel(), entry.right.getProjectPath()), spec.getDisplayName() + " could not be resolved into a usable project."));
                } else {
                    result.resolved(componentMetaData);
                }
                return;
            }
        }
    }
    projectDependencyResolver.resolve(dependency, versionConstraint, result);
}
Also used : VersionRef(org.gradle.vcs.internal.VersionRef) ModuleVersionNotFoundException(org.gradle.internal.resolve.ModuleVersionNotFoundException) VersionControlSystem(org.gradle.vcs.internal.VersionControlSystem) NestedBuildFactory(org.gradle.initialization.NestedBuildFactory) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) IncludedBuildInternal(org.gradle.composite.internal.IncludedBuildInternal) IncludedBuildRegistry(org.gradle.composite.internal.IncludedBuildRegistry) VersionControlSpec(org.gradle.vcs.VersionControlSpec) AbstractVersionControlSpec(org.gradle.vcs.internal.spec.AbstractVersionControlSpec) ModuleComponentSelector(org.gradle.api.artifacts.component.ModuleComponentSelector) LocalComponentMetadata(org.gradle.internal.component.local.model.LocalComponentMetadata) ModuleVersionResolveException(org.gradle.internal.resolve.ModuleVersionResolveException) File(java.io.File) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) Pair(org.gradle.internal.Pair)

Example 28 with ModuleComponentSelector

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

the class ExternalModuleDependencyDescriptorFactoryTest method testCreateFromModuleDependency.

@Test
public void testCreateFromModuleDependency() {
    DefaultExternalModuleDependency moduleDependency = new DefaultExternalModuleDependency("org.gradle", "gradle-core", "1.0", TEST_DEP_CONF);
    setUpDependency(moduleDependency);
    LocalOriginDependencyMetadata dependencyMetaData = externalModuleDependencyDescriptorFactory.createDependencyDescriptor(componentId, TEST_CONF, null, moduleDependency);
    ModuleComponentSelector selector = (ModuleComponentSelector) dependencyMetaData.getSelector();
    assertEquals(moduleDependency.isChanging(), dependencyMetaData.isChanging());
    assertEquals(moduleDependency.isForce(), dependencyMetaData.isForce());
    assertEquals(moduleDependency.getGroup(), moduleDependency.getGroup());
    assertEquals(moduleDependency.getName(), moduleDependency.getName());
    assertEquals(moduleDependency.getVersion(), moduleDependency.getVersion());
    assertEquals(moduleDependency.getGroup(), selector.getGroup());
    assertEquals(moduleDependency.getName(), selector.getModule());
    assertEquals(moduleDependency.getVersion(), selector.getVersion());
    assertEquals(((VersionConstraintInternal) moduleDependency.getVersionConstraint()).asImmutable(), selector.getVersionConstraint());
    assertDependencyDescriptorHasCommonFixtureValues(dependencyMetaData);
}
Also used : DefaultExternalModuleDependency(org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency) LocalOriginDependencyMetadata(org.gradle.internal.component.model.LocalOriginDependencyMetadata) ModuleComponentSelector(org.gradle.api.artifacts.component.ModuleComponentSelector) Test(org.junit.Test)

Example 29 with ModuleComponentSelector

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

the class DefaultDependencySubstitutions method substitute.

@Override
public Substitution substitute(final ComponentSelector substituted) {
    return new Substitution() {

        Action<? super ArtifactSelectionDetails> artifactAction = Actions.doNothing();

        ComponentSelectionDescriptorInternal substitutionReason = (ComponentSelectionDescriptorInternal) reason;

        @Override
        public Substitution because(String description) {
            substitutionReason = substitutionReason.withDescription(Describables.of(description));
            return this;
        }

        @Override
        public Substitution withClassifier(String classifier) {
            artifactAction = Actions.composite(artifactAction, new SetClassifier(classifier));
            return this;
        }

        @Override
        public Substitution withoutClassifier() {
            artifactAction = Actions.composite(artifactAction, NoClassifier.INSTANCE);
            return this;
        }

        @Override
        public Substitution withoutArtifactSelectors() {
            artifactAction = Actions.composite(artifactAction, NoArtifactSelector.INSTANCE);
            return this;
        }

        @Override
        public Substitution using(ComponentSelector notation) {
            DefaultDependencySubstitution.validateTarget(notation);
            boolean projectInvolved = false;
            if (substituted instanceof ProjectComponentSelector || notation instanceof ProjectComponentSelector) {
                // A project is involved, need to be aware of it
                projectInvolved = true;
            }
            if (substituted instanceof UnversionedModuleComponentSelector) {
                final ModuleIdentifier moduleId = ((UnversionedModuleComponentSelector) substituted).getModuleIdentifier();
                if (notation instanceof ModuleComponentSelector) {
                    if (((ModuleComponentSelector) notation).getModuleIdentifier().equals(moduleId)) {
                        // This substitution is effectively a force
                        substitutionReason = substitutionReason.markAsEquivalentToForce();
                    }
                }
                addSubstitution(new ModuleMatchDependencySubstitutionAction(substitutionReason, moduleId, notation, () -> artifactAction), projectInvolved);
            } else {
                addSubstitution(new ExactMatchDependencySubstitutionAction(substitutionReason, substituted, notation, () -> artifactAction), projectInvolved);
            }
            return this;
        }

        @Override
        @Deprecated
        public void with(ComponentSelector substitute) {
            // Do not nag for 7.1 as this introduces a performance regression with Android plugin
            // Revisit when upgrading Android plugin
            /*DeprecationLogger.deprecateMethod(Substitution.class, "with(ComponentSelector)")
                    .replaceWith("using(ComponentSelector)")
                    .willBeRemovedInGradle8()
                    .withUpgradeGuideSection(7, "dependency_substitutions_with")
                    .nagUser();*/
            using(substitute);
        }
    };
}
Also used : Action(org.gradle.api.Action) ComponentSelector(org.gradle.api.artifacts.component.ComponentSelector) ModuleComponentSelector(org.gradle.api.artifacts.component.ModuleComponentSelector) DefaultModuleComponentSelector(org.gradle.internal.component.external.model.DefaultModuleComponentSelector) ProjectComponentSelector(org.gradle.api.artifacts.component.ProjectComponentSelector) DefaultProjectComponentSelector(org.gradle.internal.component.local.model.DefaultProjectComponentSelector) ArtifactSelectionDetails(org.gradle.api.artifacts.ArtifactSelectionDetails) DependencySubstitution(org.gradle.api.artifacts.DependencySubstitution) ProjectComponentSelector(org.gradle.api.artifacts.component.ProjectComponentSelector) DefaultProjectComponentSelector(org.gradle.internal.component.local.model.DefaultProjectComponentSelector) ModuleComponentSelector(org.gradle.api.artifacts.component.ModuleComponentSelector) DefaultModuleComponentSelector(org.gradle.internal.component.external.model.DefaultModuleComponentSelector) ComponentSelectionDescriptorInternal(org.gradle.api.internal.artifacts.ivyservice.resolveengine.result.ComponentSelectionDescriptorInternal) ModuleIdentifier(org.gradle.api.artifacts.ModuleIdentifier)

Example 30 with ModuleComponentSelector

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

the class GradlePomModuleDescriptorParser method doParsePom.

private void doParsePom(DescriptorParseContext parserSettings, GradlePomModuleDescriptorBuilder mdBuilder, PomReader pomReader) throws IOException, SAXException {
    pomReader.resolveGAV();
    String groupId = pomReader.getGroupId();
    String artifactId = pomReader.getArtifactId();
    String version = pomReader.getVersion();
    if (pomReader.hasParent()) {
        // Is there any other parent properties?
        String parentGroupId = pomReader.getParentGroupId();
        String parentArtifactId = pomReader.getParentArtifactId();
        String parentVersion = pomReader.getParentVersion();
        if (!(Objects.equals(parentGroupId, groupId) && Objects.equals(parentArtifactId, artifactId) && Objects.equals(parentVersion, version))) {
            // Only attempt loading the parent if it has different coordinates
            ModuleComponentSelector parentId = DefaultModuleComponentSelector.newSelector(DefaultModuleIdentifier.newId(parentGroupId, parentArtifactId), new DefaultImmutableVersionConstraint(parentVersion));
            PomReader parentPomReader = parsePomForSelector(parserSettings, parentId, pomReader.getAllPomProperties());
            pomReader.setPomParent(parentPomReader);
            // Current POM can derive version/artifactId from parent. Resolve GAV and substitute values
            pomReader.resolveGAV();
            groupId = pomReader.getGroupId();
            artifactId = pomReader.getArtifactId();
            version = pomReader.getVersion();
        }
    }
    mdBuilder.setModuleRevId(groupId, artifactId, version);
    ModuleVersionIdentifier relocation = pomReader.getRelocation();
    if (relocation != null) {
        if (groupId != null && artifactId != null && artifactId.equals(relocation.getName()) && groupId.equals(relocation.getGroup())) {
            LOGGER.error("POM relocation to an other version number is not fully supported in Gradle : {} relocated to {}.", mdBuilder.getComponentIdentifier(), relocation);
            LOGGER.warn("Please update your dependency to directly use the correct version '{}'.", relocation);
            LOGGER.warn("Resolution will only pick dependencies of the relocated element.  Artifacts and other metadata will be ignored.");
            PomReader relocatedModule = parsePomForId(parserSettings, DefaultModuleComponentIdentifier.newId(relocation), Maps.newHashMap());
            addDependencies(mdBuilder, relocatedModule);
        } else {
            LOGGER.info(mdBuilder.getComponentIdentifier() + " is relocated to " + relocation + ". Please update your dependencies.");
            LOGGER.debug("Relocated module will be considered as a dependency");
            ModuleComponentSelector selector = DefaultModuleComponentSelector.newSelector(DefaultModuleIdentifier.newId(relocation.getGroup(), relocation.getName()), new DefaultMutableVersionConstraint(relocation.getVersion()));
            mdBuilder.addDependencyForRelocation(selector);
        }
    } else {
        overrideDependencyMgtsWithImported(parserSettings, pomReader);
        addDependencies(mdBuilder, pomReader);
    }
}
Also used : ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) DefaultImmutableVersionConstraint(org.gradle.api.internal.artifacts.dependencies.DefaultImmutableVersionConstraint) ModuleComponentSelector(org.gradle.api.artifacts.component.ModuleComponentSelector) DefaultModuleComponentSelector(org.gradle.internal.component.external.model.DefaultModuleComponentSelector) DefaultMutableVersionConstraint(org.gradle.api.internal.artifacts.dependencies.DefaultMutableVersionConstraint)

Aggregations

ModuleComponentSelector (org.gradle.api.artifacts.component.ModuleComponentSelector)46 DefaultModuleComponentSelector (org.gradle.internal.component.external.model.DefaultModuleComponentSelector)21 ComponentSelector (org.gradle.api.artifacts.component.ComponentSelector)10 ProjectComponentSelector (org.gradle.api.artifacts.component.ProjectComponentSelector)9 DefaultImmutableVersionConstraint (org.gradle.api.internal.artifacts.dependencies.DefaultImmutableVersionConstraint)8 ModuleVersionIdentifier (org.gradle.api.artifacts.ModuleVersionIdentifier)7 ExcludeMetadata (org.gradle.internal.component.model.ExcludeMetadata)6 ModuleIdentifier (org.gradle.api.artifacts.ModuleIdentifier)5 ModuleVersionSelector (org.gradle.api.artifacts.ModuleVersionSelector)5 ProjectComponentIdentifier (org.gradle.api.artifacts.component.ProjectComponentIdentifier)5 IvyArtifactName (org.gradle.internal.component.model.IvyArtifactName)5 ModuleVersionResolveException (org.gradle.internal.resolve.ModuleVersionResolveException)5 ModuleComponentIdentifier (org.gradle.api.artifacts.component.ModuleComponentIdentifier)4 DefaultModuleVersionSelector (org.gradle.api.internal.artifacts.DefaultModuleVersionSelector)4 DefaultExclude (org.gradle.internal.component.external.descriptor.DefaultExclude)4 DefaultProjectComponentSelector (org.gradle.internal.component.local.model.DefaultProjectComponentSelector)4 ArrayList (java.util.ArrayList)3 ExternalModuleDependency (org.gradle.api.artifacts.ExternalModuleDependency)3 VersionConstraint (org.gradle.api.artifacts.VersionConstraint)3 MavenScope (org.gradle.internal.component.external.descriptor.MavenScope)3