Search in sources :

Example 11 with ModuleId

use of org.apache.ivy.core.module.id.ModuleId in project ant-ivy by apache.

the class PomModuleDescriptorBuilder method addDependency.

public void addDependency(Resource res, PomDependencyData dep) {
    String scope = dep.getScope();
    if (!isNullOrEmpty(scope) && !MAVEN2_CONF_MAPPING.containsKey(scope)) {
        // unknown scope, defaulting to 'compile'
        scope = "compile";
    }
    String version = dep.getVersion();
    if (isNullOrEmpty(version)) {
        version = getDefaultVersion(dep);
    }
    ModuleRevisionId moduleRevId = ModuleRevisionId.newInstance(dep.getGroupId(), dep.getArtifactId(), version);
    // Some POMs depend on themselves; Ivy doesn't allow this. Don't add this dependency!
    // Example: https://repo1.maven.org/maven2/net/jini/jsk-platform/2.1/jsk-platform-2.1.pom
    ModuleRevisionId mRevId = ivyModuleDescriptor.getModuleRevisionId();
    if (mRevId != null && mRevId.getModuleId().equals(moduleRevId.getModuleId())) {
        return;
    }
    // experimentation shows the following, excluded modules are
    // inherited from parent POMs if either of the following is true:
    // the <exclusions> element is missing or the <exclusions> element
    // is present, but empty.
    List<ModuleId> excluded = dep.getExcludedModules();
    if (excluded.isEmpty()) {
        excluded = getDependencyMgtExclusions(ivyModuleDescriptor, dep.getGroupId(), dep.getArtifactId());
    }
    final boolean excludeAllTransitiveDeps = shouldExcludeAllTransitiveDeps(excluded);
    // the same dependency mrid could appear twice in the module descriptor,
    // so we check if we already have created a dependency descriptor for the dependency mrid
    final DependencyDescriptor existing = this.ivyModuleDescriptor.depDescriptors.get(moduleRevId);
    final DefaultDependencyDescriptor dd = (existing != null && existing instanceof DefaultDependencyDescriptor) ? (DefaultDependencyDescriptor) existing : new PomDependencyDescriptor(dep, ivyModuleDescriptor, moduleRevId, !excludeAllTransitiveDeps);
    if (isNullOrEmpty(scope)) {
        scope = getDefaultScope(dep);
    }
    ConfMapper mapping = MAVEN2_CONF_MAPPING.get(scope);
    mapping.addMappingConfs(dd, dep.isOptional());
    Map<String, String> extraAtt = new HashMap<>();
    if (dep.getClassifier() != null || dep.getType() != null && !"jar".equals(dep.getType())) {
        String type = "jar";
        if (dep.getType() != null) {
            type = dep.getType();
        }
        String ext = type;
        // Cfr. http://maven.apache.org/guides/mini/guide-attached-tests.html
        if ("test-jar".equals(type)) {
            ext = "jar";
            extraAtt.put("m:classifier", "tests");
        } else if (JAR_PACKAGINGS.contains(type)) {
            ext = "jar";
        }
        // dependency to assume such an artifact is published
        if (dep.getClassifier() != null) {
            extraAtt.put("m:classifier", dep.getClassifier());
        }
        DefaultDependencyArtifactDescriptor depArtifact = new DefaultDependencyArtifactDescriptor(dd, dd.getDependencyId().getName(), type, ext, null, extraAtt);
        // here we have to assume a type and ext for the artifact, so this is a limitation
        // compared to how m2 behave with classifiers
        String optionalizedScope = dep.isOptional() ? "optional" : scope;
        dd.addDependencyArtifact(optionalizedScope, depArtifact);
    }
    for (ModuleId excludedModule : excluded) {
        // in account while defining the DefaultDependencyDescriptor itself
        if ("*".equals(excludedModule.getOrganisation()) && "*".equals(excludedModule.getName())) {
            continue;
        }
        for (String conf : dd.getModuleConfigurations()) {
            dd.addExcludeRule(conf, new DefaultExcludeRule(new ArtifactId(excludedModule, PatternMatcher.ANY_EXPRESSION, PatternMatcher.ANY_EXPRESSION, PatternMatcher.ANY_EXPRESSION), ExactPatternMatcher.INSTANCE, null));
        }
    }
    // intentional identity check to make sure we don't re-add the same dependency
    if (existing != dd) {
        ivyModuleDescriptor.addDependency(dd);
    }
}
Also used : ArtifactId(org.apache.ivy.core.module.id.ArtifactId) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) DefaultDependencyArtifactDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyArtifactDescriptor) ModuleId(org.apache.ivy.core.module.id.ModuleId) DefaultExcludeRule(org.apache.ivy.core.module.descriptor.DefaultExcludeRule) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)

Example 12 with ModuleId

use of org.apache.ivy.core.module.id.ModuleId in project ant-ivy by apache.

the class PomModuleDescriptorBuilder method getDefaultVersion.

private String getDefaultVersion(PomDependencyData dep) {
    ModuleId moduleId = ModuleId.newInstance(dep.getGroupId(), dep.getArtifactId());
    if (ivyModuleDescriptor.getDependencyManagementMap().containsKey(moduleId)) {
        return ivyModuleDescriptor.getDependencyManagementMap().get(moduleId).getVersion();
    }
    String key = getDependencyMgtExtraInfoKeyForVersion(dep.getGroupId(), dep.getArtifactId());
    return ivyModuleDescriptor.getExtraInfoContentByTagName(key);
}
Also used : ModuleId(org.apache.ivy.core.module.id.ModuleId)

Example 13 with ModuleId

use of org.apache.ivy.core.module.id.ModuleId in project ant-ivy by apache.

the class PomModuleDescriptorBuilder method addDependencyMgt.

public void addDependencyMgt(PomDependencyMgt dep) {
    ivyModuleDescriptor.addDependencyManagement(dep);
    String key = getDependencyMgtExtraInfoKeyForVersion(dep.getGroupId(), dep.getArtifactId());
    overwriteExtraInfoIfExists(key, dep.getVersion());
    if (dep.getScope() != null) {
        String scopeKey = getDependencyMgtExtraInfoKeyForScope(dep.getGroupId(), dep.getArtifactId());
        overwriteExtraInfoIfExists(scopeKey, dep.getScope());
    }
    if (!dep.getExcludedModules().isEmpty()) {
        String exclusionPrefix = getDependencyMgtExtraInfoPrefixForExclusion(dep.getGroupId(), dep.getArtifactId());
        int index = 0;
        for (ModuleId excludedModule : dep.getExcludedModules()) {
            overwriteExtraInfoIfExists(exclusionPrefix + index, excludedModule.getOrganisation() + EXTRA_INFO_DELIMITER + excludedModule.getName());
            index++;
        }
    }
    // dependency management info is also used for version mediation of transitive dependencies
    ivyModuleDescriptor.addDependencyDescriptorMediator(ModuleId.newInstance(dep.getGroupId(), dep.getArtifactId()), ExactPatternMatcher.INSTANCE, new OverrideDependencyDescriptorMediator(null, dep.getVersion()));
}
Also used : OverrideDependencyDescriptorMediator(org.apache.ivy.core.module.descriptor.OverrideDependencyDescriptorMediator) ModuleId(org.apache.ivy.core.module.id.ModuleId)

Example 14 with ModuleId

use of org.apache.ivy.core.module.id.ModuleId in project ant-ivy by apache.

the class SearchEngine method findModuleRevisionIds.

public Collection<ModuleRevisionId> findModuleRevisionIds(DependencyResolver resolver, ModuleRevisionId pattern, PatternMatcher matcher) {
    Collection<ModuleRevisionId> mrids = new ArrayList<>();
    String resolverName = resolver.getName();
    Message.verbose("looking for modules matching " + pattern + " using " + matcher.getName());
    Namespace fromNamespace = null;
    if (resolver instanceof AbstractResolver) {
        fromNamespace = resolver.getNamespace();
    }
    Collection<ModuleEntry> modules = new ArrayList<>();
    OrganisationEntry[] orgs = resolver.listOrganisations();
    if (orgs == null || orgs.length == 0) {
        // hack for resolvers which are not able to list organisation, we try to see if the
        // asked organisation is not an exact one:
        String org = pattern.getOrganisation();
        if (fromNamespace != null) {
            org = NameSpaceHelper.transform(pattern.getModuleId(), fromNamespace.getFromSystemTransformer()).getOrganisation();
        }
        modules.addAll(Arrays.asList(resolver.listModules(new OrganisationEntry(resolver, org))));
    } else {
        Matcher orgMatcher = matcher.getMatcher(pattern.getOrganisation());
        for (OrganisationEntry oe : orgs) {
            String org = oe.getOrganisation();
            String systemOrg = (fromNamespace == null) ? org : NameSpaceHelper.transformOrganisation(org, fromNamespace.getToSystemTransformer());
            if (orgMatcher.matches(systemOrg)) {
                modules.addAll(Arrays.asList(resolver.listModules(new OrganisationEntry(resolver, org))));
            }
        }
    }
    Message.debug("found " + modules.size() + " modules for " + pattern.getOrganisation() + " on " + resolverName);
    boolean foundModule = false;
    for (ModuleEntry mEntry : modules) {
        ModuleId foundMid = new ModuleId(mEntry.getOrganisation(), mEntry.getModule());
        ModuleId systemMid = foundMid;
        if (fromNamespace != null) {
            systemMid = NameSpaceHelper.transform(foundMid, fromNamespace.getToSystemTransformer());
        }
        if (MatcherHelper.matches(matcher, pattern.getModuleId(), systemMid)) {
            // The module corresponds to the searched module pattern
            foundModule = true;
            RevisionEntry[] rEntries = resolver.listRevisions(mEntry);
            Message.debug("found " + rEntries.length + " revisions for [" + mEntry.getOrganisation() + ", " + mEntry.getModule() + "] on " + resolverName);
            boolean foundRevision = false;
            for (RevisionEntry rEntry : rEntries) {
                ModuleRevisionId foundMrid = ModuleRevisionId.newInstance(mEntry.getOrganisation(), mEntry.getModule(), rEntry.getRevision());
                ModuleRevisionId systemMrid = foundMrid;
                if (fromNamespace != null) {
                    systemMrid = fromNamespace.getToSystemTransformer().transform(foundMrid);
                }
                if (MatcherHelper.matches(matcher, pattern, systemMrid)) {
                    // We have a matching module revision
                    foundRevision = true;
                    mrids.add(systemMrid);
                }
            }
            if (!foundRevision) {
                Message.debug("no revision found matching " + pattern + " in [" + mEntry.getOrganisation() + "," + mEntry.getModule() + "] using " + resolverName);
            }
        }
    }
    if (!foundModule) {
        Message.debug("no module found matching " + pattern + " using " + resolverName);
    }
    return mrids;
}
Also used : Matcher(org.apache.ivy.plugins.matcher.Matcher) PatternMatcher(org.apache.ivy.plugins.matcher.PatternMatcher) ArrayList(java.util.ArrayList) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Namespace(org.apache.ivy.plugins.namespace.Namespace) ModuleId(org.apache.ivy.core.module.id.ModuleId) AbstractResolver(org.apache.ivy.plugins.resolver.AbstractResolver)

Example 15 with ModuleId

use of org.apache.ivy.core.module.id.ModuleId in project ant-ivy by apache.

the class SearchEngine method listModules.

/**
 * List module ids of the module accessible through the current resolvers matching the given mid
 * criteria according to the given matcher.
 * <p>
 * ModuleId are returned in the system namespace.
 * </p>
 *
 * @param moduleCrit ModuleId
 * @param matcher PatternMatcher
 * @return ModuleId[]
 */
public ModuleId[] listModules(ModuleId moduleCrit, PatternMatcher matcher) {
    List<ModuleId> ret = new ArrayList<>();
    Map<String, Object> criteria = new HashMap<>();
    addMatcher(matcher, moduleCrit.getOrganisation(), criteria, IvyPatternHelper.ORGANISATION_KEY);
    addMatcher(matcher, moduleCrit.getName(), criteria, IvyPatternHelper.MODULE_KEY);
    String[] tokensToList = new String[] { IvyPatternHelper.ORGANISATION_KEY, IvyPatternHelper.MODULE_KEY };
    for (DependencyResolver resolver : settings.getResolvers()) {
        Map<String, String>[] moduleIdAsMap = resolver.listTokenValues(tokensToList, criteria);
        for (Map<String, String> moduleId : moduleIdAsMap) {
            String org = moduleId.get(IvyPatternHelper.ORGANISATION_KEY);
            String name = moduleId.get(IvyPatternHelper.MODULE_KEY);
            ModuleId modId = ModuleId.newInstance(org, name);
            ret.add(NameSpaceHelper.transform(modId, resolver.getNamespace().getToSystemTransformer()));
        }
    }
    return ret.toArray(new ModuleId[ret.size()]);
}
Also used : ModuleId(org.apache.ivy.core.module.id.ModuleId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver)

Aggregations

ModuleId (org.apache.ivy.core.module.id.ModuleId)49 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)19 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)13 File (java.io.File)9 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 Date (java.util.Date)6 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)6 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)6 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)6 BuildException (org.apache.tools.ant.BuildException)6 Ivy (org.apache.ivy.Ivy)5 ArtifactId (org.apache.ivy.core.module.id.ArtifactId)5 ResolveReport (org.apache.ivy.core.report.ResolveReport)5 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)5 IvySettings (org.apache.ivy.core.settings.IvySettings)5 IOException (java.io.IOException)4 Map (java.util.Map)4 Configuration (org.apache.ivy.core.module.descriptor.Configuration)4