Search in sources :

Example 6 with DependencyDescriptor

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

the class IvyDeliverTest method testSimple.

@Test
public void testSimple() throws Exception {
    project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-latest.xml");
    IvyResolve res = new IvyResolve();
    res.setProject(project);
    res.execute();
    deliver.setPubrevision("1.2");
    deliver.setDeliverpattern("build/test/deliver/ivy-[revision].xml");
    deliver.execute();
    // should have done the ivy delivering
    File deliveredIvyFile = new File("build/test/deliver/ivy-1.2.xml");
    assertTrue(deliveredIvyFile.exists());
    ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), deliveredIvyFile.toURI().toURL(), true);
    assertEquals(ModuleRevisionId.newInstance("apache", "resolve-latest", "1.2"), md.getModuleRevisionId());
    DependencyDescriptor[] dds = md.getDependencies();
    assertEquals(1, dds.length);
    assertEquals(ModuleRevisionId.newInstance("org1", "mod1.2", "2.2"), dds[0].getDependencyRevisionId());
    assertEquals(ModuleRevisionId.newInstance("org1", "mod1.2", "latest.integration"), dds[0].getDynamicConstraintDependencyRevisionId());
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) IvySettings(org.apache.ivy.core.settings.IvySettings) File(java.io.File) Test(org.junit.Test)

Example 7 with DependencyDescriptor

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

the class IvyDeliverTest method testWithResolveIdInAnotherBuild.

@Test
public void testWithResolveIdInAnotherBuild() throws Exception {
    // create a new build
    Project other = TestHelper.newProject();
    other.setProperty("ivy.settings.file", "test/repositories/ivysettings.xml");
    other.setProperty("build", "build/test/deliver");
    // do a resolve in the new build
    IvyResolve resolve = new IvyResolve();
    resolve.setProject(other);
    resolve.setFile(new File("test/java/org/apache/ivy/ant/ivy-simple.xml"));
    resolve.setResolveId("withResolveId");
    resolve.execute();
    // resolve another ivy file
    resolve = new IvyResolve();
    resolve.setProject(project);
    resolve.setFile(new File("test/java/org/apache/ivy/ant/ivy-latest.xml"));
    resolve.execute();
    deliver.setResolveId("withResolveId");
    deliver.setPubrevision("1.2");
    deliver.setDeliverpattern("build/test/deliver/ivy-[revision].xml");
    deliver.execute();
    // should have done the ivy delivering
    File deliveredIvyFile = new File("build/test/deliver/ivy-1.2.xml");
    assertTrue(deliveredIvyFile.exists());
    ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), deliveredIvyFile.toURI().toURL(), true);
    assertEquals(ModuleRevisionId.newInstance("apache", "resolve-simple", "1.2"), md.getModuleRevisionId());
    DependencyDescriptor[] dds = md.getDependencies();
    assertEquals(1, dds.length);
    assertEquals(ModuleRevisionId.newInstance("org1", "mod1.2", "2.0"), dds[0].getDependencyRevisionId());
}
Also used : Project(org.apache.tools.ant.Project) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) IvySettings(org.apache.ivy.core.settings.IvySettings) File(java.io.File) Test(org.junit.Test)

Example 8 with DependencyDescriptor

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

the class IvyDeliverTest method testWithExtraAttributes.

/**
 * Test case for IVY-415.
 *
 * @throws Exception if something goes wrong
 * @see <a href="https://issues.apache.org/jira/browse/IVY-415">IVY-415</a>
 */
@Test
public void testWithExtraAttributes() throws Exception {
    project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-latest-extra.xml");
    IvyResolve res = new IvyResolve();
    res.setValidate(false);
    res.setProject(project);
    res.execute();
    deliver.setPubrevision("1.2");
    deliver.setDeliverpattern("build/test/deliver/ivy-[revision].xml");
    deliver.setValidate(false);
    deliver.execute();
    // should have done the ivy delivering
    File deliveredIvyFile = new File("build/test/deliver/ivy-1.2.xml");
    assertTrue(deliveredIvyFile.exists());
    ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), deliveredIvyFile.toURI().toURL(), false);
    assertEquals(ModuleRevisionId.newInstance("apache", "resolve-latest", "1.2"), md.getModuleRevisionId());
    DependencyDescriptor[] dds = md.getDependencies();
    assertEquals(1, dds.length);
    Map<String, String> extraAtt = new HashMap<>();
    extraAtt.put("myExtraAtt", "myValue");
    assertEquals(ModuleRevisionId.newInstance("org1", "mod1.2", "2.2", extraAtt), dds[0].getDependencyRevisionId());
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) HashMap(java.util.HashMap) IvySettings(org.apache.ivy.core.settings.IvySettings) File(java.io.File) Test(org.junit.Test)

Example 9 with DependencyDescriptor

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

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

the class PomModuleDescriptorParser method parseDescriptor.

public ModuleDescriptor parseDescriptor(ParserSettings ivySettings, URL descriptorURL, Resource res, boolean validate) throws ParseException, IOException {
    PomModuleDescriptorBuilder mdBuilder = new PomModuleDescriptorBuilder(this, res, ivySettings);
    try {
        final IvyContext ivyContext = IvyContext.pushNewCopyContext();
        HashSet<ModuleRevisionId> parents = ivyContext.get(PARENT_MAP_KEY);
        if (parents == null) {
            parents = new LinkedHashSet<>();
            ivyContext.set(PARENT_MAP_KEY, parents);
        }
        PomReader domReader = new PomReader(descriptorURL, res);
        domReader.setProperty("parent.version", domReader.getParentVersion());
        domReader.setProperty("parent.groupId", domReader.getParentGroupId());
        domReader.setProperty("project.parent.version", domReader.getParentVersion());
        domReader.setProperty("project.parent.groupId", domReader.getParentGroupId());
        Message.debug("parent.groupId: " + domReader.getParentGroupId());
        Message.debug("parent.artifactId: " + domReader.getParentArtifactId());
        Message.debug("parent.version: " + domReader.getParentVersion());
        for (final Map.Entry<String, String> prop : domReader.getPomProperties().entrySet()) {
            domReader.setProperty(prop.getKey(), prop.getValue());
            mdBuilder.addProperty(prop.getKey(), prop.getValue());
        }
        final List<PomProfileElement> activeProfiles = new ArrayList<>();
        // add profile specific properties
        for (final PomProfileElement profile : domReader.getProfiles()) {
            if (!profile.isActive()) {
                continue;
            }
            // keep track of this active profile for later use
            activeProfiles.add(profile);
            final Map<String, String> profileProps = profile.getProfileProperties();
            if (profileProps.isEmpty()) {
                continue;
            }
            for (final Map.Entry<String, String> entry : profileProps.entrySet()) {
                domReader.setProperty(entry.getKey(), entry.getValue());
                mdBuilder.addProperty(entry.getKey(), entry.getValue());
            }
        }
        ModuleDescriptor parentDescr = null;
        if (domReader.hasParent()) {
            // Is there any other parent properties?
            ModuleRevisionId parentModRevID = ModuleRevisionId.newInstance(domReader.getParentGroupId(), domReader.getParentArtifactId(), domReader.getParentVersion());
            // check for cycles
            if (parents.contains(parentModRevID)) {
                throw new CircularDependencyException(parents);
            } else {
                parents.add(parentModRevID);
            }
            ResolvedModuleRevision parentModule = parseOtherPom(ivySettings, parentModRevID);
            if (parentModule == null) {
                throw new IOException("Impossible to load parent for " + res.getName() + ". Parent=" + parentModRevID);
            }
            parentDescr = parentModule.getDescriptor();
            if (parentDescr != null) {
                for (Map.Entry<String, String> prop : extractPomProperties(parentDescr.getExtraInfos()).entrySet()) {
                    domReader.setProperty(prop.getKey(), prop.getValue());
                }
            }
        }
        String groupId = domReader.getGroupId();
        String artifactId = domReader.getArtifactId();
        String version = domReader.getVersion();
        mdBuilder.setModuleRevId(groupId, artifactId, version);
        mdBuilder.setHomePage(domReader.getHomePage());
        mdBuilder.setDescription(domReader.getDescription());
        // if this module doesn't have an explicit license, use the parent's license (if any)
        final License[] licenses = domReader.getLicenses();
        if (licenses != null && licenses.length > 0) {
            mdBuilder.setLicenses(licenses);
        } else if (parentDescr != null) {
            mdBuilder.setLicenses(parentDescr.getLicenses());
        }
        ModuleRevisionId relocation = domReader.getRelocation();
        if (relocation != null) {
            if (groupId != null && artifactId != null && artifactId.equals(relocation.getName()) && groupId.equals(relocation.getOrganisation())) {
                Message.error("Relocation to an other version number not supported in ivy : " + mdBuilder.getModuleDescriptor().getModuleRevisionId() + " relocated to " + relocation + ". Please update your dependency to directly use the right version.");
                Message.warn("Resolution will only pick dependencies of the relocated element." + "  Artifact and other metadata will be ignored.");
                ResolvedModuleRevision relocatedModule = parseOtherPom(ivySettings, relocation);
                if (relocatedModule == null) {
                    throw new ParseException("impossible to load module " + relocation + " to which " + mdBuilder.getModuleDescriptor().getModuleRevisionId() + " has been relocated", 0);
                }
                for (DependencyDescriptor dd : relocatedModule.getDescriptor().getDependencies()) {
                    mdBuilder.addDependency(dd);
                }
            } else {
                Message.info(mdBuilder.getModuleDescriptor().getModuleRevisionId() + " is relocated to " + relocation + ". Please update your dependencies.");
                Message.verbose("Relocated module will be considered as a dependency");
                DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(mdBuilder.getModuleDescriptor(), relocation, true, false, true);
                /* Map all public dependencies */
                for (Configuration m2Conf : MAVEN2_CONFIGURATIONS) {
                    if (PUBLIC.equals(m2Conf.getVisibility())) {
                        dd.addDependencyConfiguration(m2Conf.getName(), m2Conf.getName());
                    }
                }
                mdBuilder.addDependency(dd);
            }
        } else {
            domReader.setProperty("project.groupId", groupId);
            domReader.setProperty("pom.groupId", groupId);
            domReader.setProperty("groupId", groupId);
            domReader.setProperty("project.artifactId", artifactId);
            domReader.setProperty("pom.artifactId", artifactId);
            domReader.setProperty("artifactId", artifactId);
            domReader.setProperty("project.version", version);
            domReader.setProperty("pom.version", version);
            domReader.setProperty("version", version);
            if (parentDescr != null) {
                mdBuilder.addExtraInfos(parentDescr.getExtraInfos());
                // add dependency management info from parent
                for (PomDependencyMgt dep : getDependencyManagements(parentDescr)) {
                    if (dep instanceof PomDependencyMgtElement) {
                        dep = domReader.new PomDependencyMgtElement((PomDependencyMgtElement) dep);
                    }
                    mdBuilder.addDependencyMgt(dep);
                }
                // add plugins from parent
                for (PomDependencyMgt pomDependencyMgt : getPlugins(parentDescr)) {
                    mdBuilder.addPlugin(pomDependencyMgt);
                }
            }
            for (PomDependencyMgt dep : domReader.getDependencyMgt()) {
                addTo(mdBuilder, dep, ivySettings);
            }
            for (PomDependencyData dep : domReader.getDependencies()) {
                mdBuilder.addDependency(res, dep);
            }
            for (PomPluginElement plugin : domReader.getPlugins()) {
                mdBuilder.addPlugin(plugin);
            }
            // consult active profiles:
            for (final PomProfileElement activeProfile : activeProfiles) {
                for (PomDependencyMgt dep : activeProfile.getDependencyMgt()) {
                    addTo(mdBuilder, dep, ivySettings);
                }
                for (PomDependencyData dep : activeProfile.getDependencies()) {
                    mdBuilder.addDependency(res, dep);
                }
                for (PomPluginElement plugin : activeProfile.getPlugins()) {
                    mdBuilder.addPlugin(plugin);
                }
            }
            if (parentDescr != null) {
                for (DependencyDescriptor descriptor : parentDescr.getDependencies()) {
                    if (descriptor instanceof PomDependencyDescriptor) {
                        PomDependencyData parentDep = ((PomDependencyDescriptor) descriptor).getPomDependencyData();
                        PomDependencyData dep = domReader.new PomDependencyData(parentDep);
                        mdBuilder.addDependency(res, dep);
                    } else {
                        mdBuilder.addDependency(descriptor);
                    }
                }
            }
            mdBuilder.addMainArtifact(artifactId, domReader.getPackaging());
            addSourcesAndJavadocArtifactsIfPresent(mdBuilder, ivySettings);
        }
    } catch (SAXException e) {
        throw newParserException(e);
    } finally {
        IvyContext.popContext();
    }
    return mdBuilder.getModuleDescriptor();
}
Also used : Configuration(org.apache.ivy.core.module.descriptor.Configuration) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) PomDependencyDescriptor(org.apache.ivy.plugins.parser.m2.PomModuleDescriptorBuilder.PomDependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) ArrayList(java.util.ArrayList) License(org.apache.ivy.core.module.descriptor.License) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) SAXException(org.xml.sax.SAXException) PomDependencyMgtElement(org.apache.ivy.plugins.parser.m2.PomReader.PomDependencyMgtElement) IvyContext(org.apache.ivy.core.IvyContext) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) PomProfileElement(org.apache.ivy.plugins.parser.m2.PomReader.PomProfileElement) CircularDependencyException(org.apache.ivy.plugins.circular.CircularDependencyException) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) IOException(java.io.IOException) PomPluginElement(org.apache.ivy.plugins.parser.m2.PomReader.PomPluginElement) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) PomDependencyDescriptor(org.apache.ivy.plugins.parser.m2.PomModuleDescriptorBuilder.PomDependencyDescriptor) PomDependencyData(org.apache.ivy.plugins.parser.m2.PomReader.PomDependencyData) ParseException(java.text.ParseException) Map(java.util.Map)

Aggregations

DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)101 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)80 Test (org.junit.Test)75 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)35 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)33 File (java.io.File)26 XmlModuleDescriptorParserTest (org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest)26 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)25 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)18 IvySettings (org.apache.ivy.core.settings.IvySettings)17 Configuration (org.apache.ivy.core.module.descriptor.Configuration)15 Artifact (org.apache.ivy.core.module.descriptor.Artifact)13 ParseException (java.text.ParseException)12 IOException (java.io.IOException)11 ResolveData (org.apache.ivy.core.resolve.ResolveData)11 MockResolver (org.apache.ivy.plugins.resolver.MockResolver)9 JarFile (java.util.jar.JarFile)7 GregorianCalendar (java.util.GregorianCalendar)6 ModuleId (org.apache.ivy.core.module.id.ModuleId)6 ResolveReport (org.apache.ivy.core.report.ResolveReport)6