2013-03-24 41 views
6

我发现常春藤API非常复杂。最简单的常春藤代码以编程方式从Maven中心检索依赖关系

什么是最简单的可能代码片段从Maven Central检索工件到使用Ivy 100%编程的特定本地目录(没有Ant,没有Xml文件,...)?

为了举例说明检索commons-logging:commons-logging:1.1:jar into/my/destination。

+0

为什么艾维的要求?其他库/代码片段是否可以接受? – kdgregory 2013-03-24 15:10:15

+0

当然,如果你有一个本地缓存的替代解决方案,并确保下载不会被破坏,那也没关系。 – 2013-03-24 15:41:18

+0

我原本打算推荐[Aether](http://www.sonatype.org/aether),但后来我看了他们的[例子](http://git.eclipse.org/c/aether/aether- demo.git/tree /)和[API](http://download.eclipse.org/aether/aether-core/0.9.0.M2/apidocs/),并意识到人们为什么取笑Java程序员。 – kdgregory 2013-03-25 12:26:36

回答

5

(和它的依赖)检索神器简单的方法是use ivy from the command-line

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

这将检索文件到目录“/我/目标”。

其他例子:

+0

由于它完全避免了API,因此它稍微拉伸了“100%编程”。 – 2013-03-24 23:17:37

+0

@AxelFontaine获取完成的工作:-)如果你按照第一个例子列表,我有一个常规的例子,它调用ivy任务。就我个人而言,我从来没有必要使用ivy的Java API。正如大卫所说的那样,在任何情况下都没有记录。 – 2013-03-24 23:53:42

7

我一直在使用的Ivy远程解决从Maven仓库的文物(和依赖性)。下面是一个下载一个工件(不依赖关系)的代码示例。

如果您需要依赖关系,则需要修改依赖关系描述符。

一些注意事项:

  1. 常青藤使用一个缓存来存储以前提取的文物和自己的“常春藤翻译”(你会发现从Maven工件在高速缓存中得到的常春藤模块)

  2. 一般概念是,你以编程方式创建一个常驻程序模块,它依赖于Maven存储库中存储的“伪模块”(即我认为解析器实现了一个映射)。

  3. 一般而言,如果您想知道如何以编程方式使用Ivy,那么一个好的起点是主类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"}) 
     ); 
    } 

+0

我们如何获得它还可以下载组/工件/版本的源代码? – ticktock 2014-11-04 00:40:54

+0

'ResolveOptions'的'transitive'和'download'标志具有默认值'true',所以不需要调用它们的setter。 – lyomi 2014-12-12 05:42:51

+0

你将如何添加凭据到你的常春藤设置? – 2016-01-26 18:37:48

相关问题