Search in sources :

Example 1 with RetrieveOptions

use of org.apache.ivy.core.retrieve.RetrieveOptions in project ant-ivy by apache.

the class IvyRetrieve method doExecute.

@SuppressWarnings("deprecation")
@Override
public void doExecute() throws BuildException {
    prepareAndCheck();
    if (!getAllowedLogOptions().contains(getLog())) {
        throw new BuildException("invalid option for 'log': " + getLog() + ". Available options are " + getAllowedLogOptions());
    }
    pattern = getProperty(pattern, getSettings(), "ivy.retrieve.pattern");
    try {
        final Filter<Artifact> artifactFilter = getArtifactFilter();
        final RetrieveOptions retrieveOptions = (RetrieveOptions) new RetrieveOptions().setLog(getLog());
        retrieveOptions.setConfs(splitToArray(getConf())).setDestArtifactPattern(pattern).setDestIvyPattern(ivypattern).setArtifactFilter(artifactFilter).setSync(sync).setOverwriteMode(getOverwriteMode()).setUseOrigin(isUseOrigin()).setMakeSymlinks(symlink).setResolveId(getResolveId()).setMapper(mapper == null ? null : new MapperAdapter(mapper));
        // only set this if the user has explicitly enabled this deprecated option
        if (symlinkmass) {
            retrieveOptions.setMakeSymlinksInMass(symlinkmass);
        }
        final RetrieveReport report = getIvyInstance().retrieve(getResolvedMrid(), retrieveOptions);
        int targetsCopied = report.getNbrArtifactsCopied();
        boolean haveTargetsBeenCopied = targetsCopied > 0;
        getProject().setProperty("ivy.nb.targets.copied", String.valueOf(targetsCopied));
        getProject().setProperty("ivy.targets.copied", String.valueOf(haveTargetsBeenCopied));
        if (getPathId() != null) {
            Path path = new Path(getProject());
            getProject().addReference(getPathId(), path);
            for (File file : report.getRetrievedFiles()) {
                path.createPathElement().setLocation(file);
            }
        }
        if (getSetId() != null) {
            FileSet fileset = new FileSet();
            fileset.setProject(getProject());
            getProject().addReference(getSetId(), fileset);
            fileset.setDir(report.getRetrieveRoot());
            for (File file : report.getRetrievedFiles()) {
                PatternSet.NameEntry ne = fileset.createInclude();
                ne.setName(getPath(report.getRetrieveRoot(), file));
            }
        }
    } catch (Exception ex) {
        throw new BuildException("impossible to ivy retrieve: " + ex, ex);
    }
}
Also used : Path(org.apache.tools.ant.types.Path) FileSet(org.apache.tools.ant.types.FileSet) RetrieveOptions(org.apache.ivy.core.retrieve.RetrieveOptions) Artifact(org.apache.ivy.core.module.descriptor.Artifact) BuildException(org.apache.tools.ant.BuildException) RetrieveReport(org.apache.ivy.core.retrieve.RetrieveReport) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) PatternSet(org.apache.tools.ant.types.PatternSet)

Example 2 with RetrieveOptions

use of org.apache.ivy.core.retrieve.RetrieveOptions in project ant-ivy by apache.

the class Main method run.

@SuppressWarnings("deprecation")
private static ResolveReport run(CommandLine line, boolean isCli) throws Exception {
    if (line.hasOption("version")) {
        System.out.println("Apache Ivy " + Ivy.getIvyVersion() + " - " + Ivy.getIvyDate() + " :: " + Ivy.getIvyHomeURL());
        return null;
    }
    boolean validate = !line.hasOption("novalidate");
    Ivy ivy = Ivy.newInstance();
    initMessage(line, ivy);
    IvySettings settings = initSettings(line, ivy);
    ivy.pushContext();
    File cache = new File(settings.substitute(line.getOptionValue("cache", settings.getDefaultCache().getAbsolutePath())));
    if (line.hasOption("cache")) {
        // override default cache path with user supplied cache path
        settings.setDefaultCache(cache);
    }
    if (!cache.exists()) {
        cache.mkdirs();
    } else if (!cache.isDirectory()) {
        error(cache + " is not a directory");
    }
    String[] confs;
    if (line.hasOption("confs")) {
        confs = line.getOptionValues("confs");
    } else {
        confs = new String[] { "*" };
    }
    File ivyfile;
    if (line.hasOption("dependency")) {
        String[] dep = line.getOptionValues("dependency");
        ivyfile = File.createTempFile("ivy", ".xml");
        ivyfile.deleteOnExit();
        DefaultModuleDescriptor 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);
        for (String conf : confs) {
            dd.addDependencyConfiguration("default", conf);
        }
        md.addDependency(dd);
        XmlModuleDescriptorWriter.write(md, ivyfile);
        confs = new String[] { "default" };
    } else {
        ivyfile = new File(settings.substitute(line.getOptionValue("ivy", "ivy.xml")));
        if (!ivyfile.exists()) {
            error("ivy file not found: " + ivyfile);
        } else if (ivyfile.isDirectory()) {
            error("ivy file is not a file: " + ivyfile);
        }
    }
    if (line.hasOption("useOrigin")) {
        ivy.getSettings().useDeprecatedUseOrigin();
    }
    ResolveOptions resolveOptions = new ResolveOptions().setConfs(confs).setValidate(validate).setResolveMode(line.getOptionValue("mode")).setArtifactFilter(FilterHelper.getArtifactTypeFilter(line.getOptionValues("types")));
    if (line.hasOption("notransitive")) {
        resolveOptions.setTransitive(false);
    }
    if (line.hasOption("refresh")) {
        resolveOptions.setRefresh(true);
    }
    ResolveReport report = ivy.resolve(ivyfile.toURI().toURL(), resolveOptions);
    if (report.hasError()) {
        if (isCli) {
            System.exit(1);
        }
        StringBuilder sb = new StringBuilder();
        for (String problem : report.getAllProblemMessages()) {
            if (sb.length() > 0) {
                sb.append("\n");
            }
            sb.append(problem);
        }
        throw new ResolveProcessException(sb.toString());
    }
    ModuleDescriptor md = report.getModuleDescriptor();
    if (confs.length == 1 && "*".equals(confs[0])) {
        confs = md.getConfigurationsNames();
    }
    if (line.hasOption("retrieve")) {
        String retrievePattern = settings.substitute(line.getOptionValue("retrieve"));
        if (!retrievePattern.contains("[")) {
            retrievePattern += "/lib/[conf]/[artifact].[ext]";
        }
        String ivyPattern = settings.substitute(line.getOptionValue("ivypattern"));
        ivy.retrieve(md.getModuleRevisionId(), new RetrieveOptions().setConfs(confs).setSync(line.hasOption("sync")).setUseOrigin(line.hasOption("useOrigin")).setDestArtifactPattern(retrievePattern).setDestIvyPattern(ivyPattern).setOverwriteMode(line.getOptionValue("overwriteMode")).setArtifactFilter(FilterHelper.getArtifactTypeFilter(line.getOptionValues("types"))).setMakeSymlinks(line.hasOption("symlink")).setMakeSymlinksInMass(line.hasOption("symlinkmass")));
    }
    if (line.hasOption("cachepath")) {
        outputCachePath(ivy, cache, md, confs, line.getOptionValue("cachepath", "ivycachepath.txt"));
    }
    if (line.hasOption("revision")) {
        ivy.deliver(md.getResolvedModuleRevisionId(), settings.substitute(line.getOptionValue("revision")), settings.substitute(line.getOptionValue("deliverto", "ivy-[revision].xml")), DeliverOptions.newInstance(settings).setStatus(settings.substitute(line.getOptionValue("status", "release"))).setValidate(validate));
        if (line.hasOption("publish")) {
            ivy.publish(md.getResolvedModuleRevisionId(), Collections.singleton(settings.substitute(line.getOptionValue("publishpattern", "distrib/[type]s/[artifact]-[revision].[ext]"))), line.getOptionValue("publish"), new PublishOptions().setPubrevision(settings.substitute(line.getOptionValue("revision"))).setValidate(validate).setSrcIvyPattern(settings.substitute(line.getOptionValue("deliverto", "ivy-[revision].xml"))).setOverwrite(line.hasOption("overwrite")));
        }
    }
    if (line.hasOption("makepom")) {
        final String pomFilePath = line.getOptionValue("makepom", "pom.xml");
        final File pomFile = new File(pomFilePath);
        PomModuleDescriptorWriter.write(md, pomFile, new PomWriterOptions());
        Message.debug("Generated a pom file for module at " + pomFile);
    }
    if (line.hasOption("main")) {
        // check if the option cp has been set
        List<File> fileList = getExtraClasspathFileList(line);
        // merge -args and left over args
        String[] fargs = line.getOptionValues("args");
        if (fargs == null) {
            fargs = new String[0];
        }
        String[] extra = line.getLeftOverArgs();
        if (extra == null) {
            extra = new String[0];
        }
        String[] params = new String[fargs.length + extra.length];
        System.arraycopy(fargs, 0, params, 0, fargs.length);
        System.arraycopy(extra, 0, params, fargs.length, extra.length);
        // invoke with given main class and merged params
        invoke(ivy, cache, md, confs, fileList, line.getOptionValue("main"), params);
    }
    ivy.getLoggerEngine().popLogger();
    ivy.popContext();
    return report;
}
Also used : IvySettings(org.apache.ivy.core.settings.IvySettings) RetrieveOptions(org.apache.ivy.core.retrieve.RetrieveOptions) PublishOptions(org.apache.ivy.core.publish.PublishOptions) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ResolveReport(org.apache.ivy.core.report.ResolveReport) ResolveProcessException(org.apache.ivy.core.resolve.ResolveProcessException) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) ResolveOptions(org.apache.ivy.core.resolve.ResolveOptions) PomWriterOptions(org.apache.ivy.plugins.parser.m2.PomWriterOptions)

Example 3 with RetrieveOptions

use of org.apache.ivy.core.retrieve.RetrieveOptions in project ant-ivy by apache.

the class IvyArtifactReport method doExecute.

public void doExecute() throws BuildException {
    prepareAndCheck();
    if (tofile == null) {
        throw new BuildException("no destination file name: please provide it through parameter 'tofile'");
    }
    pattern = getProperty(pattern, getSettings(), "ivy.retrieve.pattern");
    try {
        String[] confs = splitToArray(getConf());
        ModuleDescriptor md = null;
        if (getResolveId() == null) {
            md = getResolvedDescriptor(getOrganisation(), getModule(), false);
        } else {
            md = getResolvedDescriptor(getResolveId());
        }
        IvyNode[] dependencies = getIvyInstance().getResolveEngine().getDependencies(md, ((ResolveOptions) new ResolveOptions().setLog(getLog())).setConfs(confs).setResolveId(getResolveId()).setValidate(doValidate(getSettings())), null);
        Map<ArtifactDownloadReport, Set<String>> artifactsToCopy = getIvyInstance().getRetrieveEngine().determineArtifactsToCopy(ModuleRevisionId.newInstance(getOrganisation(), getModule(), getRevision()), pattern, ((RetrieveOptions) new RetrieveOptions().setLog(getLog())).setConfs(confs).setResolveId(getResolveId()));
        Map<ModuleRevisionId, Set<ArtifactDownloadReport>> moduleRevToArtifactsMap = new HashMap<>();
        for (ArtifactDownloadReport artifact : artifactsToCopy.keySet()) {
            Set<ArtifactDownloadReport> moduleRevArtifacts = moduleRevToArtifactsMap.get(artifact.getArtifact().getModuleRevisionId());
            if (moduleRevArtifacts == null) {
                moduleRevArtifacts = new HashSet<>();
                moduleRevToArtifactsMap.put(artifact.getArtifact().getModuleRevisionId(), moduleRevArtifacts);
            }
            moduleRevArtifacts.add(artifact);
        }
        generateXml(dependencies, moduleRevToArtifactsMap, artifactsToCopy);
    } catch (ParseException e) {
        log(e.getMessage(), Project.MSG_ERR);
        throw new BuildException("syntax errors in ivy file: " + e, e);
    } catch (IOException e) {
        throw new BuildException("impossible to generate report: " + e, e);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) RetrieveOptions(org.apache.ivy.core.retrieve.RetrieveOptions) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) IOException(java.io.IOException) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) BuildException(org.apache.tools.ant.BuildException) ParseException(java.text.ParseException) ResolveOptions(org.apache.ivy.core.resolve.ResolveOptions) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Aggregations

RetrieveOptions (org.apache.ivy.core.retrieve.RetrieveOptions)3 File (java.io.File)2 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)2 ResolveOptions (org.apache.ivy.core.resolve.ResolveOptions)2 BuildException (org.apache.tools.ant.BuildException)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Artifact (org.apache.ivy.core.module.descriptor.Artifact)1 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)1 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)1 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)1 PublishOptions (org.apache.ivy.core.publish.PublishOptions)1 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)1 ResolveReport (org.apache.ivy.core.report.ResolveReport)1 IvyNode (org.apache.ivy.core.resolve.IvyNode)1 ResolveProcessException (org.apache.ivy.core.resolve.ResolveProcessException)1 RetrieveReport (org.apache.ivy.core.retrieve.RetrieveReport)1