Skip to content

Instantly share code, notes, and snippets.

@HyCraftHD
Created April 5, 2018 14:01
Show Gist options
  • Save HyCraftHD/22654fd59cec776f7865720857be0ae9 to your computer and use it in GitHub Desktop.
Save HyCraftHD/22654fd59cec776f7865720857be0ae9 to your computer and use it in GitHub Desktop.
Basic Ivy code to download maven dependecies
package test;
import java.io.File;
import org.apache.ivy.Ivy;
import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.core.report.ResolveReport;
import org.apache.ivy.core.resolve.ResolveOptions;
import org.apache.ivy.core.retrieve.RetrieveOptions;
import org.apache.ivy.core.settings.IvySettings;
import org.apache.ivy.plugins.resolver.IBiblioResolver;
public class Test {
public static void main(String[] args) throws Exception {
String groupId = "com.sedmelluq";
String artifactId = "lavaplayer";
String version = "1.2.62";
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.setUseMavenMetadata(true);
br.setRoot("https://jcenter.bintray.com");
br.setName("bintray");
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, true);
// 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", "runtime");
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" }));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment