Search in sources :

Example 26 with ModuleId

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

the class IvyConfigurationProvider method addArtifact.

public void addArtifact(String groupId, String artifactId, String version) throws Exception {
    artifacts.add(groupId + ":" + artifactId + ":" + version);
    String[] dep = null;
    dep = new String[] { groupId, artifactId, version };
    if (md == null) {
        md = DefaultModuleDescriptor.newDefaultInstance(ModuleRevisionId.newInstance(dep[0], dep[1] + "-caller", "working"));
    }
    DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, ModuleRevisionId.newInstance(dep[0], dep[1], dep[2]), false, false, true);
    md.addDependency(dd);
    ExcludeRule er = new DefaultExcludeRule(new ArtifactId(new ModuleId("org.walkmod", "walkmod-core"), PatternMatcher.ANY_EXPRESSION, PatternMatcher.ANY_EXPRESSION, PatternMatcher.ANY_EXPRESSION), ExactPatternMatcher.INSTANCE, null);
    dd.addExcludeRule(null, er);
}
Also used : DefaultExcludeRule(org.apache.ivy.core.module.descriptor.DefaultExcludeRule) ModuleId(org.apache.ivy.core.module.id.ModuleId) ArtifactId(org.apache.ivy.core.module.id.ArtifactId) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) ExcludeRule(org.apache.ivy.core.module.descriptor.ExcludeRule) DefaultExcludeRule(org.apache.ivy.core.module.descriptor.DefaultExcludeRule)

Example 27 with ModuleId

use of org.apache.ivy.core.module.id.ModuleId 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)

Example 28 with ModuleId

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

the class ResolveReport method getModuleIds.

/**
 * gives all the modules ids concerned by this report, from the most dependent to the least one
 *
 * @return a list of ModuleId
 */
public List<ModuleId> getModuleIds() {
    List<ModuleId> ret = new ArrayList<>();
    List<IvyNode> sortedDependencies = new ArrayList<>(dependencies);
    for (IvyNode dependency : sortedDependencies) {
        ModuleId mid = dependency.getResolvedId().getModuleId();
        if (!ret.contains(mid)) {
            ret.add(mid);
        }
    }
    return ret;
}
Also used : ModuleId(org.apache.ivy.core.module.id.ModuleId) ArrayList(java.util.ArrayList) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Example 29 with ModuleId

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

the class CollectionOfModulesToSort method addToModulesByModuleId.

private void addToModulesByModuleId(ModuleDescriptor md, ModuleInSort mdInSort) {
    ModuleId mdId = md.getModuleRevisionId().getModuleId();
    List<ModuleInSort> mdInSortAsList = new LinkedList<>();
    mdInSortAsList.add(mdInSort);
    Collection<ModuleInSort> previousList = modulesByModuleId.put(mdId, mdInSortAsList);
    if (previousList != null) {
        mdInSortAsList.addAll(previousList);
    }
}
Also used : ModuleId(org.apache.ivy.core.module.id.ModuleId) LinkedList(java.util.LinkedList)

Example 30 with ModuleId

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

the class RetrieveEngine method retrieve.

public RetrieveReport retrieve(ModuleRevisionId mrid, RetrieveOptions options) throws IOException {
    RetrieveReport report = new RetrieveReport();
    ModuleId moduleId = mrid.getModuleId();
    if (LogOptions.LOG_DEFAULT.equals(options.getLog())) {
        Message.info(":: retrieving :: " + moduleId + (options.isSync() ? " [sync]" : ""));
    } else {
        Message.verbose(":: retrieving :: " + moduleId + (options.isSync() ? " [sync]" : ""));
    }
    Message.verbose("\tcheckUpToDate=" + settings.isCheckUpToDate());
    long start = System.currentTimeMillis();
    String destFilePattern = IvyPatternHelper.substituteVariables(options.getDestArtifactPattern(), settings.getVariables());
    String destIvyPattern = IvyPatternHelper.substituteVariables(options.getDestIvyPattern(), settings.getVariables());
    String[] confs = getConfs(mrid, options);
    if (LogOptions.LOG_DEFAULT.equals(options.getLog())) {
        Message.info("\tconfs: " + Arrays.asList(confs));
    } else {
        Message.verbose("\tconfs: " + Arrays.asList(confs));
    }
    if (this.eventManager != null) {
        this.eventManager.fireIvyEvent(new StartRetrieveEvent(mrid, confs, options));
    }
    try {
        Map<ArtifactDownloadReport, Set<String>> artifactsToCopy = determineArtifactsToCopy(mrid, destFilePattern, options);
        File fileRetrieveRoot = settings.resolveFile(IvyPatternHelper.getTokenRoot(destFilePattern));
        report.setRetrieveRoot(fileRetrieveRoot);
        File ivyRetrieveRoot = destIvyPattern == null ? null : settings.resolveFile(IvyPatternHelper.getTokenRoot(destIvyPattern));
        Collection<File> targetArtifactsStructure = new HashSet<>();
        // Set(File) set of all paths which should be present at then end of retrieve (useful
        // for sync)
        // same for ivy files
        Collection<File> targetIvysStructure = new HashSet<>();
        // do retrieve
        long totalCopiedSize = 0;
        for (Map.Entry<ArtifactDownloadReport, Set<String>> artifactAndPaths : artifactsToCopy.entrySet()) {
            ArtifactDownloadReport artifact = artifactAndPaths.getKey();
            File archive = artifact.getLocalFile();
            if (artifact.getUnpackedLocalFile() != null) {
                archive = artifact.getUnpackedLocalFile();
            }
            if (archive == null) {
                Message.verbose("\tno local file available for " + artifact + ": skipping");
                continue;
            }
            Message.verbose("\tretrieving " + archive);
            for (String path : artifactAndPaths.getValue()) {
                IvyContext.getContext().checkInterrupted();
                File destFile = settings.resolveFile(path);
                if (!settings.isCheckUpToDate() || !upToDate(archive, destFile, options)) {
                    Message.verbose("\t\tto " + destFile);
                    if (this.eventManager != null) {
                        this.eventManager.fireIvyEvent(new StartRetrieveArtifactEvent(artifact, destFile));
                    }
                    if (options.isMakeSymlinks()) {
                        boolean symlinkCreated;
                        try {
                            symlinkCreated = FileUtil.symlink(archive, destFile, true);
                        } catch (IOException ioe) {
                            symlinkCreated = false;
                            // warn about the inability to create a symlink
                            Message.warn("symlink creation failed at path " + destFile, ioe);
                        }
                        if (!symlinkCreated) {
                            // since symlink creation failed, let's attempt to an actual copy instead
                            Message.info("Attempting a copy operation (since symlink creation failed) at path " + destFile);
                            FileUtil.copy(archive, destFile, null, true);
                        }
                    } else {
                        FileUtil.copy(archive, destFile, null, true);
                    }
                    if (this.eventManager != null) {
                        this.eventManager.fireIvyEvent(new EndRetrieveArtifactEvent(artifact, destFile));
                    }
                    totalCopiedSize += FileUtil.getFileLength(destFile);
                    report.addCopiedFile(destFile, artifact);
                } else {
                    Message.verbose("\t\tto " + destFile + " [NOT REQUIRED]");
                    report.addUpToDateFile(destFile, artifact);
                }
                if ("ivy".equals(artifact.getType())) {
                    targetIvysStructure.addAll(FileUtil.getPathFiles(ivyRetrieveRoot, destFile));
                } else {
                    Collection<File> files = FileUtil.listAll(destFile, Collections.<String>emptyList());
                    for (File file : files) {
                        targetArtifactsStructure.addAll(FileUtil.getPathFiles(fileRetrieveRoot, file));
                    }
                }
            }
        }
        if (options.isSync()) {
            Message.verbose("\tsyncing...");
            String[] ignorableFilenames = settings.getIgnorableFilenames();
            Collection<String> ignoreList = Arrays.asList(ignorableFilenames);
            Collection<File> existingArtifacts = FileUtil.listAll(fileRetrieveRoot, ignoreList);
            Collection<File> existingIvys = (ivyRetrieveRoot == null) ? null : FileUtil.listAll(ivyRetrieveRoot, ignoreList);
            if (fileRetrieveRoot.equals(ivyRetrieveRoot)) {
                targetArtifactsStructure.addAll(targetIvysStructure);
                existingArtifacts.addAll(existingIvys);
                sync(targetArtifactsStructure, existingArtifacts);
            } else {
                sync(targetArtifactsStructure, existingArtifacts);
                if (existingIvys != null) {
                    sync(targetIvysStructure, existingIvys);
                }
            }
        }
        long elapsedTime = System.currentTimeMillis() - start;
        String msg = "\t" + report.getNbrArtifactsCopied() + " artifacts copied" + (settings.isCheckUpToDate() ? (", " + report.getNbrArtifactsUpToDate() + " already retrieved") : "") + " (" + (totalCopiedSize / KILO) + "kB/" + elapsedTime + "ms)";
        if (LogOptions.LOG_DEFAULT.equals(options.getLog())) {
            Message.info(msg);
        } else {
            Message.verbose(msg);
        }
        Message.verbose("\tretrieve done (" + (elapsedTime) + "ms)");
        if (this.eventManager != null) {
            this.eventManager.fireIvyEvent(new EndRetrieveEvent(mrid, confs, elapsedTime, report.getNbrArtifactsCopied(), report.getNbrArtifactsUpToDate(), totalCopiedSize, options));
        }
        return report;
    } catch (Exception ex) {
        throw new RuntimeException("problem during retrieve of " + moduleId + ": " + ex, ex);
    }
}
Also used : EndRetrieveEvent(org.apache.ivy.core.event.retrieve.EndRetrieveEvent) HashSet(java.util.HashSet) Set(java.util.Set) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) IOException(java.io.IOException) ParseException(java.text.ParseException) IOException(java.io.IOException) ModuleId(org.apache.ivy.core.module.id.ModuleId) StartRetrieveArtifactEvent(org.apache.ivy.core.event.retrieve.StartRetrieveArtifactEvent) StartRetrieveEvent(org.apache.ivy.core.event.retrieve.StartRetrieveEvent) EndRetrieveArtifactEvent(org.apache.ivy.core.event.retrieve.EndRetrieveArtifactEvent) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

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