Simplest Ivy code to programmatically retrieve dependency from Maven Central

為{幸葍}努か 提交于 2019-11-29 10:50:28

I have been working on using Ivy to remotely resolve artifacts (and dependencies) from Maven repository. Here is one code sample that downloads one artifact (w/o dependencies).

If you need dependencies, you need to adapt the dependency descriptor.

Some note:

  1. Ivy uses a cache to store previously retrieved artifacts and their "ivy translations" (you will find ivy modules derived from Maven artifacts in the cache)

  2. The general concept is that you programmatically create an Ivy module that has dependencies on Maven repository stored "pseudo-modules" (i.e. there is a mapping under the hood implemented by the resolver - I believe).

  3. In general a good starting point, if you want to know how to programmatically use Ivy, is the main class org.apache.ivy.Main.


        public static void main(String[] args) throws Exception {

        String groupId = "org.springframework";
        String artifactId = "spring-context-support";
        String version = "4.0.2.RELEASE";
        File   out = new File("out");

        // create an ivy instance
        IvySettings ivySettings = new IvySettings();
        ivySettings.setDefaultCache(new File("ivy/cache"));

        // use the biblio resolver, if you consider resolving 
        // POM declared dependencies
        IBiblioResolver br = new IBiblioResolver();
        br.setM2compatible(true);
        br.setUsepoms(true);
        br.setName("central");

        ivySettings.addResolver(br);
        ivySettings.setDefaultResolver(br.getName());

        Ivy ivy = Ivy.newInstance(ivySettings);

        // Step 1: you always need to resolve before you can retrieve
        //
        ResolveOptions ro = new ResolveOptions();
        // this seems to have no impact, if you resolve by module descriptor (in contrast to resolve by ModuleRevisionId)
        ro.setTransitive(true);
        // if set to false, nothing will be downloaded
        ro.setDownload(true);

        // 1st create an ivy module (this always(!) has a "default" configuration already)
        DefaultModuleDescriptor md = DefaultModuleDescriptor.newDefaultInstance(
            // give it some related name (so it can be cached)
            ModuleRevisionId.newInstance(
                groupId, 
                artifactId+"-envelope", 
                version
            )
        );

        // 2. add dependencies for what we are really looking for
        ModuleRevisionId ri = ModuleRevisionId.newInstance(
            groupId, 
            artifactId,
            version
        );
        // don't go transitive here, if you want the single artifact
        DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, ri, false, false, false);

        // map to master to just get the code jar. See generated ivy module xmls from maven repo
        // on how configurations are mapped into ivy. Or check 
        // e.g. http://lightguard-jp.blogspot.de/2009/04/ivy-configurations-when-pulling-from.html
        dd.addDependencyConfiguration("default", "master");
        md.addDependency(dd);

        // now resolve
        ResolveReport rr = ivy.resolve(md,ro);
        if (rr.hasError()) {
            throw new RuntimeException(rr.getAllProblemMessages().toString());
        }

        // Step 2: retrieve
        ModuleDescriptor m = rr.getModuleDescriptor();

        ivy.retrieve(
            m.getModuleRevisionId(),
            out.getAbsolutePath()+"/[artifact](-[classifier]).[ext]",
            new RetrieveOptions()
                // this is from the envelop module
                .setConfs(new String[]{"default"})
        );
    }

Mark O'Connor

The simplest way to retrieve an artifact (and it's dependencies) is to use ivy from the command-line

java -jar ivy.jar -dependency commons-logging commons-logging 1.1 -retrieve "/my/destination/[artifact](-[classifier]).[ext]"

This will retrieve the files into the directory "/my/destination".

Other examples:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!