我发现常春藤API非常复杂。
使用Ivy 100%以编程方式(没有Ant,没有Xml文件,......)将Maven Central中的工件从Maven Central检索到特定本地目录的最简单的片段是什么?
为了举例说明检索commons-logging:commons-logging:1.1:jar into / my / destination。
答案 0 :(得分:8)
我一直在使用Ivy从Maven存储库远程解析工件(和依赖项)。下面是一个下载一个工件(没有依赖项)的代码示例。
如果需要依赖项,则需要调整依赖项描述符。
有些注意事项:
Ivy使用缓存来存储以前检索到的工件及其“常春藤翻译”(您将在缓存中找到从Maven工件派生的常春藤模块)
一般的概念是你以编程方式创建一个依赖于Maven存储库存储的“伪模块”的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"})
);
}
答案 1 :(得分:7)
检索工件(及其依赖项)的最简单方法是use ivy from the command-line
java -jar ivy.jar -dependency commons-logging commons-logging 1.1 -retrieve "/my/destination/[artifact](-[classifier]).[ext]"
这会将文件检索到目录“/ my / destination”。
其他例子: