Search in sources :

Example 1 with DefaultDependencyArtifactDescriptor

use of org.apache.ivy.core.module.descriptor.DefaultDependencyArtifactDescriptor 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 2 with DefaultDependencyArtifactDescriptor

use of org.apache.ivy.core.module.descriptor.DefaultDependencyArtifactDescriptor in project Saturn by vipshop.

the class IvyGetArtifact method getIvyfile.

private File getIvyfile(String org, String name, String rev, String[] confs, Set<Map<String, Object>> artifacts) throws IOException {
    File ivyfile;
    ivyfile = File.createTempFile("ivy", ".xml");
    ivyfile.deleteOnExit();
    DefaultModuleDescriptor md = DefaultModuleDescriptor.newDefaultInstance(ModuleRevisionId.newInstance(org, name + "-caller", "working"));
    DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, ModuleRevisionId.newInstance(org, name, rev), false, false, true);
    if (artifacts != null && !artifacts.isEmpty()) {
        for (Map<String, Object> artifact : artifacts) {
            String artifactName = (String) artifact.get("name");
            String artifactType = (String) artifact.get("type");
            String artifactExt = (String) artifact.get("ext");
            URL artifactUrl = (URL) artifact.get("url");
            Map<?, ?> extraAttributes = (Map<?, ?>) artifact.get("extraAttributes");
            DefaultDependencyArtifactDescriptor dad = new DefaultDependencyArtifactDescriptor(dd, artifactName, artifactType, artifactExt, artifactUrl, extraAttributes);
            dd.addDependencyArtifact("default", dad);
        }
    }
    for (int i = 0; i < confs.length; i++) {
        dd.addDependencyConfiguration("default", confs[i]);
    }
    md.addDependency(dd);
    md.addExtraAttributeNamespace("m", "http://ant.apache.org/ivy/maven");
    XmlModuleDescriptorWriter.write(md, ivyfile);
    return ivyfile;
}
Also used : DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) DefaultDependencyArtifactDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyArtifactDescriptor) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) File(java.io.File) Map(java.util.Map) URL(java.net.URL)

Example 3 with DefaultDependencyArtifactDescriptor

use of org.apache.ivy.core.module.descriptor.DefaultDependencyArtifactDescriptor in project ant-ivy by apache.

the class IvyDependencyArtifact method addArtifact.

void addArtifact(DefaultDependencyDescriptor dd, String masterConf) {
    String typePattern = type == null ? PatternMatcher.ANY_EXPRESSION : type;
    String extPattern = ext == null ? typePattern : ext;
    URL u;
    try {
        u = url == null ? null : new URL(url);
    } catch (MalformedURLException e) {
        throw new BuildException("Malformed url in the artifact: " + e.getMessage(), e);
    }
    DefaultDependencyArtifactDescriptor dad = new DefaultDependencyArtifactDescriptor(dd, name, typePattern, extPattern, u, null);
    dd.addDependencyArtifact(masterConf, dad);
}
Also used : MalformedURLException(java.net.MalformedURLException) BuildException(org.apache.tools.ant.BuildException) DefaultDependencyArtifactDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyArtifactDescriptor) URL(java.net.URL)

Aggregations

DefaultDependencyArtifactDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyArtifactDescriptor)3 URL (java.net.URL)2 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)2 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 DefaultExcludeRule (org.apache.ivy.core.module.descriptor.DefaultExcludeRule)1 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)1 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)1 ArtifactId (org.apache.ivy.core.module.id.ArtifactId)1 ModuleId (org.apache.ivy.core.module.id.ModuleId)1 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)1 BuildException (org.apache.tools.ant.BuildException)1