本文介绍了最简单的常春藤代码,以编程方式从Maven Central检索依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现Ivy API非常复杂。

I find the Ivy API to be immensely complicated.

使用常春藤100%从Maven Central检索工件到特定本地目录的最简单的片段是什么?以编程方式(没有Ant,没有Xml文件,......)?

What is the simplest possible snippet to retrieve an artifact from Maven Central into a specific local directory using Ivy 100% programmatically (no Ant, no Xml files, ...)?

为了举例说明检索commons-logging:commons-logging:1.1:jar into /我/目的地。

For the sake of an example say retrieving commons-logging:commons-logging:1.1:jar into /my/destination.

推荐答案

我一直在使用Ivy来远程解析Maven资源库中的工件(和依赖项)。下面是一个下载一个工件(没有依赖项)的代码示例。

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.

注意事项:


  1. Ivy使用缓存存储以前检索过的工件及其常春藤翻译(你会发现从缓存中的Maven工件派生的ivy模块)

  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)

一般的概念是你以编程方式创建一个依赖于Maven存储库存储的伪模块的Ivy模块(即引擎盖下有一个映射由解析器实现 - 我相信)。

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).

一般来说,一个好的起点,如果你想知道如何以编程方式使用Ivy,那么主要的是 org.apache.ivy.Main

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"})
        );
    }

这篇关于最简单的常春藤代码,以编程方式从Maven Central检索依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 08:17