Search in sources :

Example 36 with DependencyDescriptor

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

the class XmlModuleDescriptorParserTest method testExtendsCached.

@Test
public void testExtendsCached() throws Exception {
    // configure a resolver to serve the parent descriptor, so that parse succeeds.
    File resolveRoot = new File("build/tmp/xmlModuleDescriptorTest");
    assertTrue(resolveRoot.exists() || resolveRoot.mkdirs());
    FileUtil.copy(getClass().getResource("test-extends-parent.xml"), new File(resolveRoot, "myorg/myparent/ivy.xml"), null, null);
    FileSystemResolver resolver = new FileSystemResolver();
    resolver.setSettings(settings);
    resolver.setName("testExtendsCached");
    resolver.addIvyPattern(resolveRoot.getAbsolutePath() + "/[organisation]/[module]/[artifact].[ext]");
    settings.addResolver(resolver);
    settings.setDefaultResolver("testExtendsCached");
    // descriptor extends a module without a location="..." attribute, so resolver lookup
    // must be performed.
    ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-extends-cached.xml"), true);
    assertNotNull(md);
    assertEquals("myorg", md.getModuleRevisionId().getOrganisation());
    assertEquals("mymodule", md.getModuleRevisionId().getName());
    assertEquals("myrev", md.getModuleRevisionId().getRevision());
    assertEquals("integration", md.getStatus());
    // verify that the parent description was merged.
    assertEquals("Parent module description.", md.getDescription());
    // verify that the parent and child configurations were merged together.
    final Configuration[] expectedConfs = { new Configuration("default"), new Configuration("conf1"), new Configuration("conf2") };
    assertNotNull(md.getConfigurations());
    assertEquals(Arrays.asList(expectedConfs), Arrays.asList(md.getConfigurations()));
    // verify parent and child dependencies were merged together.
    DependencyDescriptor[] deps = md.getDependencies();
    assertNotNull(deps);
    assertEquals(2, deps.length);
    assertEquals(Collections.singletonList("default"), Arrays.asList(deps[0].getModuleConfigurations()));
    ModuleRevisionId dep = deps[0].getDependencyRevisionId();
    assertEquals("myorg", dep.getModuleId().getOrganisation());
    assertEquals("mymodule1", dep.getModuleId().getName());
    assertEquals("1.0", dep.getRevision());
    assertEquals(Arrays.asList("conf1", "conf2"), Arrays.asList(deps[1].getModuleConfigurations()));
    dep = deps[1].getDependencyRevisionId();
    assertEquals("myorg", dep.getModuleId().getOrganisation());
    assertEquals("mymodule2", dep.getModuleId().getName());
    assertEquals("2.0", dep.getRevision());
    // verify only child publications are present
    Artifact[] artifacts = md.getAllArtifacts();
    assertNotNull(artifacts);
    assertEquals(1, artifacts.length);
    assertEquals("mymodule", artifacts[0].getName());
    assertEquals("jar", artifacts[0].getType());
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) Configuration(org.apache.ivy.core.module.descriptor.Configuration) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) File(java.io.File) Artifact(org.apache.ivy.core.module.descriptor.Artifact) FileSystemResolver(org.apache.ivy.plugins.resolver.FileSystemResolver) Test(org.junit.Test)

Example 37 with DependencyDescriptor

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

the class SortTest method testDifferentVersionWarning.

/**
 * In case of Different version a warning is generated.
 */
@Test
public void testDifferentVersionWarning() {
    final DependencyDescriptor md4OtherDep = addDependency(md1, "md4", "rev4-other");
    addDependency(md2, "md1", "rev1");
    addDependency(md3, "md2", "rev2");
    addDependency(md4, "md3", "rev3");
    // Would be much easier with a tool like jmock
    class NonMatchingVersionReporterMock implements NonMatchingVersionReporter {

        private int nbOfCall = 0;

        public void reportNonMatchingVersion(DependencyDescriptor descriptor, ModuleDescriptor md) {
            assertEquals("reportNonMatchingVersion should be invoked only once", 0, nbOfCall);
            assertEquals(md4OtherDep, descriptor);
            assertEquals(md4, md);
            nbOfCall++;
        }

        public void validate() {
            assertEquals("reportNonMatchingVersion has not been called", 1, nbOfCall);
        }
    }
    NonMatchingVersionReporterMock nonMatchingVersionReporterMock = new NonMatchingVersionReporterMock();
    List<ModuleDescriptor> toSort = Arrays.asList(new ModuleDescriptor[] { md4, md3, md2, md1 });
    sortModuleDescriptors(toSort, nonMatchingVersionReporterMock);
    nonMatchingVersionReporterMock.validate();
}
Also used : DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) Test(org.junit.Test)

Example 38 with DependencyDescriptor

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

the class DefaultRepositoryCacheManagerTest method testLatestIntegrationIsCachedPerResolver.

@Test
@Ignore
public void testLatestIntegrationIsCachedPerResolver() throws Exception {
    // given a module org#module
    ModuleId mi = new ModuleId("org", "module");
    // and a latest.integration mrid/dd
    ModuleRevisionId mridLatest = new ModuleRevisionId(mi, "trunk", "latest.integration");
    DependencyDescriptor ddLatest = new DefaultDependencyDescriptor(mridLatest, false);
    // and some random options
    CacheMetadataOptions options = new CacheMetadataOptions().setCheckTTL(false);
    // setup resolver1 to download the static content so we can call cacheModuleDescriptor
    MockResolver resolver = new MockResolver();
    resolver.setName("resolver1");
    resolver.setSettings(ivy.getSettings());
    ivy.getSettings().addResolver(resolver);
    ResourceDownloader downloader = new ResourceDownloader() {

        public void download(Artifact artifact, Resource resource, File dest) throws IOException {
            String content = "<ivy-module version=\"2.0\"><info organisation=\"org\" module=\"module\" status=\"integration\" revision=\"1.1\" branch=\"trunk\"/></ivy-module>";
            dest.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(dest);
            PrintWriter pw = new PrintWriter(out);
            pw.write(content);
            pw.flush();
            out.close();
        }
    };
    ModuleDescriptorWriter writer = new ModuleDescriptorWriter() {

        public void write(ResolvedResource originalMdResource, ModuleDescriptor md, File src, File dest) throws IOException {
            XmlModuleDescriptorWriter.write(md, dest);
        }
    };
    // latest.integration will resolve to 1.1 in resolver1
    ModuleRevisionId mrid11 = new ModuleRevisionId(mi, "trunk", "1.1");
    DefaultArtifact artifact11 = new DefaultArtifact(mrid11, new Date(), "module-1.1.ivy", "ivy", "ivy", true);
    DependencyDescriptor dd11 = new DefaultDependencyDescriptor(mrid11, false);
    BasicResource resource11 = new BasicResource("/module-1-1.ivy", true, 1, 0, true);
    ResolvedResource mdRef11 = new ResolvedResource(resource11, "1.1");
    // tell the cache about 1.1
    ResolvedModuleRevision rmr11 = cacheManager.cacheModuleDescriptor(resolver, mdRef11, dd11, artifact11, downloader, options);
    cacheManager.originalToCachedModuleDescriptor(resolver, mdRef11, artifact11, rmr11, writer);
    // and use the new overload that passes in resolver name
    cacheManager.saveResolvedRevision("resolver1", mridLatest, "1.1");
    ResolvedModuleRevision rmrFromCache = cacheManager.findModuleInCache(ddLatest, mridLatest, options, "resolver1");
    assertEquals(rmr11, rmrFromCache);
}
Also used : DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) BasicResource(org.apache.ivy.plugins.repository.BasicResource) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) MockResolver(org.apache.ivy.plugins.resolver.MockResolver) ResourceDownloader(org.apache.ivy.plugins.repository.ResourceDownloader) BasicResource(org.apache.ivy.plugins.repository.BasicResource) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Date(java.util.Date) ModuleId(org.apache.ivy.core.module.id.ModuleId) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) FileOutputStream(java.io.FileOutputStream) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) XmlModuleDescriptorWriter(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter) File(java.io.File) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) PrintWriter(java.io.PrintWriter) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 39 with DependencyDescriptor

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

the class ResolveData method mediate.

public DependencyDescriptor mediate(DependencyDescriptor dd) {
    DependencyDescriptor originalDD = dd;
    dd = getEngine().mediate(dd, getOptions());
    VisitNode current = getCurrentVisitNode();
    if (current != null) {
        // mediating dd through dependers stack
        List<VisitNode> dependers = new ArrayList<>(current.getPath());
        // the returned path contains the currently visited node, we are only interested in
        // the dependers, so we remove the currently visited node from the end
        dependers.remove(dependers.size() - 1);
        // we want to apply mediation going up in the dependers stack, not the opposite
        Collections.reverse(dependers);
        for (VisitNode n : dependers) {
            ModuleDescriptor md = n.getDescriptor();
            if (md != null) {
                dd = md.mediate(dd);
            }
        }
    }
    if (originalDD != dd) {
        Message.verbose("dependency descriptor has been mediated: " + originalDD + " => " + dd);
    }
    return dd;
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) ArrayList(java.util.ArrayList)

Example 40 with DependencyDescriptor

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

the class ResolveEngine method resolve.

/**
 * Resolve dependencies of a module described by a module descriptor.
 * @param md ModuleDescriptor
 * @param options ResolveOptions
 * @return ResolveReport
 * @throws ParseException if something goes wrong
 * @throws IOException if something goes wrong
 */
public ResolveReport resolve(ModuleDescriptor md, ResolveOptions options) throws ParseException, IOException {
    DependencyResolver oldDictator = getDictatorResolver();
    IvyContext context = IvyContext.getContext();
    try {
        String[] confs = options.getConfs(md);
        options.setConfs(confs);
        if (options.getResolveId() == null) {
            options.setResolveId(ResolveOptions.getDefaultResolveId(md));
        }
        eventManager.fireIvyEvent(new StartResolveEvent(md, confs));
        long start = System.currentTimeMillis();
        if (ResolveOptions.LOG_DEFAULT.equals(options.getLog())) {
            Message.info(":: resolving dependencies :: " + md.getResolvedModuleRevisionId() + (options.isTransitive() ? "" : " [not transitive]"));
            Message.info("\tconfs: " + Arrays.asList(confs));
        } else {
            Message.verbose(":: resolving dependencies :: " + md.getResolvedModuleRevisionId() + (options.isTransitive() ? "" : " [not transitive]"));
            Message.verbose("\tconfs: " + Arrays.asList(confs));
        }
        Message.verbose("\tvalidate = " + options.isValidate());
        Message.verbose("\trefresh = " + options.isRefresh());
        ResolveReport report = new ResolveReport(md, options.getResolveId());
        ResolveData data = new ResolveData(this, options);
        context.setResolveData(data);
        // resolve dependencies
        IvyNode[] dependencies = getDependencies(md, options, report);
        report.setDependencies(Arrays.asList(dependencies), options.getArtifactFilter());
        if (options.getCheckIfChanged()) {
            report.checkIfChanged();
        }
        // produce resolved ivy file and ivy properties in cache
        ResolutionCacheManager cacheManager = settings.getResolutionCacheManager();
        cacheManager.saveResolvedModuleDescriptor(md);
        // we store the resolved dependencies revisions and statuses per asked dependency
        // revision id, for direct dependencies only.
        // this is used by the deliver task to resolve dynamic revisions to static ones
        File ivyPropertiesInCache = cacheManager.getResolvedIvyPropertiesInCache(md.getResolvedModuleRevisionId());
        Properties props = new Properties();
        if (dependencies.length > 0) {
            Map<ModuleId, ModuleRevisionId> forcedRevisions = new HashMap<>();
            for (IvyNode dependency : dependencies) {
                if (dependency.getModuleRevision() != null && dependency.getModuleRevision().isForce()) {
                    forcedRevisions.put(dependency.getModuleId(), dependency.getResolvedId());
                }
            }
            IvyNode root = dependencies[0].getRoot();
            Map<ModuleId, IvyNode> topLevelDeps = new HashMap<>();
            for (IvyNode dependency : dependencies) {
                if (!dependency.hasProblem()) {
                    DependencyDescriptor dd = dependency.getDependencyDescriptor(root);
                    if (dd != null) {
                        ModuleId orgMod = dependency.getModuleId();
                        topLevelDeps.put(orgMod, dependency);
                    }
                }
            }
            for (IvyNode dependency : dependencies) {
                if (!dependency.hasProblem() && !dependency.isCompletelyEvicted()) {
                    DependencyDescriptor dd = dependency.getDependencyDescriptor(root);
                    if (dd == null) {
                        ModuleId mid = dependency.getModuleId();
                        IvyNode tlDep = topLevelDeps.get(mid);
                        if (tlDep != null) {
                            dd = tlDep.getDependencyDescriptor(root);
                        }
                    }
                    if (dd != null) {
                        ModuleRevisionId depResolvedId = dependency.getResolvedId();
                        ModuleDescriptor depDescriptor = dependency.getDescriptor();
                        ModuleRevisionId depRevisionId = dd.getDependencyRevisionId();
                        ModuleRevisionId forcedRevisionId = forcedRevisions.get(dependency.getModuleId());
                        if (dependency.getModuleRevision() != null && dependency.getModuleRevision().isForce() && !depResolvedId.equals(depRevisionId) && !settings.getVersionMatcher().isDynamic(depRevisionId)) {
                            // if we were forced to this revision and we
                            // are not a dynamic revision, reset to the
                            // asked revision
                            depResolvedId = depRevisionId;
                            depDescriptor = null;
                        }
                        if (depResolvedId == null) {
                            throw new NullPointerException("getResolvedId() is null for " + dependency.toString());
                        }
                        if (depRevisionId == null) {
                            throw new NullPointerException("getDependencyRevisionId() " + "is null for " + dd.toString());
                        }
                        String rev = depResolvedId.getRevision();
                        String forcedRev = forcedRevisionId == null ? rev : forcedRevisionId.getRevision();
                        // The evicted modules have no description, so we can't put the status
                        String status = depDescriptor == null ? "?" : depDescriptor.getStatus();
                        Message.debug("storing dependency " + depResolvedId + " in props");
                        props.put(depRevisionId.encodeToString(), rev + " " + status + " " + forcedRev + " " + depResolvedId.getBranch());
                    }
                }
            }
        }
        FileOutputStream out = new FileOutputStream(ivyPropertiesInCache);
        props.store(out, md.getResolvedModuleRevisionId() + " resolved revisions");
        out.close();
        Message.verbose("\tresolved ivy file produced in cache");
        report.setResolveTime(System.currentTimeMillis() - start);
        if (options.isDownload()) {
            Message.verbose(":: downloading artifacts ::");
            DownloadOptions downloadOptions = new DownloadOptions();
            downloadOptions.setLog(options.getLog());
            downloadArtifacts(report, options.getArtifactFilter(), downloadOptions);
        }
        if (options.isOutputReport()) {
            outputReport(report, cacheManager, options);
        }
        Message.verbose("\tresolve done (" + report.getResolveTime() + "ms resolve - " + report.getDownloadTime() + "ms download)");
        Message.sumupProblems();
        eventManager.fireIvyEvent(new EndResolveEvent(md, confs, report));
        return report;
    } catch (RuntimeException ex) {
        Message.debug(ex);
        Message.error(ex.getMessage());
        Message.sumupProblems();
        throw ex;
    } finally {
        context.setResolveData(null);
        setDictatorResolver(oldDictator);
    }
}
Also used : HashMap(java.util.HashMap) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) ResolutionCacheManager(org.apache.ivy.core.cache.ResolutionCacheManager) StartResolveEvent(org.apache.ivy.core.event.resolve.StartResolveEvent) Properties(java.util.Properties) EndResolveEvent(org.apache.ivy.core.event.resolve.EndResolveEvent) ModuleId(org.apache.ivy.core.module.id.ModuleId) IvyContext(org.apache.ivy.core.IvyContext) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

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