Search in sources :

Example 1 with EndResolveEvent

use of org.apache.ivy.core.event.resolve.EndResolveEvent in project ant-ivy by apache.

the class IvyEventFilterTest method testSimple.

@Test
public void testSimple() {
    IvyEventFilter f = new IvyEventFilter("pre-resolve", null, null);
    assertTrue(f.accept(new StartResolveEvent(md, new String[] { "default" })));
    assertFalse(f.accept(new EndResolveEvent(md, new String[] { "default" }, new ResolveReport(md))));
}
Also used : ResolveReport(org.apache.ivy.core.report.ResolveReport) StartResolveEvent(org.apache.ivy.core.event.resolve.StartResolveEvent) EndResolveEvent(org.apache.ivy.core.event.resolve.EndResolveEvent) Test(org.junit.Test)

Example 2 with EndResolveEvent

use of org.apache.ivy.core.event.resolve.EndResolveEvent in project build-info by JFrogDev.

the class ArtifactoryBuildInfoTrigger method collectDependencyInformation.

/**
 * Collect dependency information during the build.
 *
 * @param event The end of resolution Ivy event
 */
private void collectDependencyInformation(IvyEvent event) {
    Project project = (Project) IvyContext.peekInContextStack(IvyTask.ANT_PROJECT_CONTEXT_KEY);
    ResolveReport report = ((EndResolveEvent) event).getReport();
    @SuppressWarnings("unchecked") Map<String, String> attributes = event.getAttributes();
    Module module = getOrCreateModule(attributes);
    project.log("[buildinfo:collect] Collecting dependencies for " + module.getId(), Project.MSG_INFO);
    if (module.getDependencies() == null || module.getDependencies().isEmpty()) {
        String[] configurations = report.getConfigurations();
        List<Dependency> moduleDependencies = Lists.newArrayList();
        for (String configuration : configurations) {
            project.log("[buildinfo:collect] Configuration: " + configuration + " Dependencies", Project.MSG_DEBUG);
            ConfigurationResolveReport configurationReport = report.getConfigurationReport(configuration);
            ArtifactDownloadReport[] allArtifactsReports = configurationReport.getAllArtifactsReports();
            for (final ArtifactDownloadReport artifactsReport : allArtifactsReports) {
                project.log("[buildinfo:collect] Artifact Download Report for configuration: " + configuration + " : " + artifactsReport, Project.MSG_DEBUG);
                ModuleRevisionId id = artifactsReport.getArtifact().getModuleRevisionId();
                String type = getType(artifactsReport.getArtifact());
                Dependency dependency = findDependencyInList(id, type, moduleDependencies);
                if (dependency == null) {
                    DependencyBuilder dependencyBuilder = new DependencyBuilder();
                    dependencyBuilder.type(type).scopes(Sets.newHashSet(configuration));
                    String idString = getModuleIdString(id.getOrganisation(), id.getName(), id.getRevision());
                    dependencyBuilder.id(idString);
                    File file = artifactsReport.getLocalFile();
                    Map<String, String> checksums;
                    try {
                        checksums = FileChecksumCalculator.calculateChecksums(file, MD5, SHA1);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    String md5 = checksums.get(MD5);
                    String sha1 = checksums.get(SHA1);
                    dependencyBuilder.md5(md5).sha1(sha1);
                    dependency = dependencyBuilder.build();
                    moduleDependencies.add(dependency);
                    project.log("[buildinfo:collect] Added dependency '" + dependency.getId() + "'", Project.MSG_DEBUG);
                } else {
                    if (!dependency.getScopes().contains(configuration)) {
                        dependency.getScopes().add(configuration);
                        project.log("[buildinfo:collect] Added scope " + configuration + " to dependency '" + dependency.getId() + "'", Project.MSG_DEBUG);
                    } else {
                        project.log("[buildinfo:collect] Find same dependency twice in configuration '" + configuration + "' for dependency '" + artifactsReport + "'", Project.MSG_WARN);
                    }
                }
            }
        }
        module.setDependencies(moduleDependencies);
    }
}
Also used : ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) BuildInfoExtractorUtils.getModuleIdString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getModuleIdString) BuildInfoExtractorUtils.getTypeString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getTypeString) Dependency(org.jfrog.build.api.Dependency) EndResolveEvent(org.apache.ivy.core.event.resolve.EndResolveEvent) Project(org.apache.tools.ant.Project) ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) DependencyBuilder(org.jfrog.build.api.builder.DependencyBuilder) ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) Module(org.jfrog.build.api.Module) File(java.io.File)

Example 3 with EndResolveEvent

use of org.apache.ivy.core.event.resolve.EndResolveEvent 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

EndResolveEvent (org.apache.ivy.core.event.resolve.EndResolveEvent)3 ResolveReport (org.apache.ivy.core.report.ResolveReport)3 File (java.io.File)2 StartResolveEvent (org.apache.ivy.core.event.resolve.StartResolveEvent)2 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)2 ConfigurationResolveReport (org.apache.ivy.core.report.ConfigurationResolveReport)2 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 IvyContext (org.apache.ivy.core.IvyContext)1 ResolutionCacheManager (org.apache.ivy.core.cache.ResolutionCacheManager)1 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)1 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)1 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)1 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)1 ModuleId (org.apache.ivy.core.module.id.ModuleId)1 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)1 DependencyResolver (org.apache.ivy.plugins.resolver.DependencyResolver)1 Project (org.apache.tools.ant.Project)1 Dependency (org.jfrog.build.api.Dependency)1