Ho lavorato sull'uso edera risolvere remoto artefatti (e) da dipendenze repository Maven. Ecco un esempio di codice che scarica un artefatto (senza dipendenze).
Se sono necessarie delle dipendenze, è necessario adattare il descrittore di dipendenza.
Qualche nota:
Ivy utilizza una cache per memorizzare i manufatti recuperati in precedenza e le loro "traduzioni edera" (troverete i moduli di edera derivati da artefatti Maven nella cache)
Il generale il concetto è che si crea un modulo Ivy che ha dipendenze sul repository Maven memorizzato "pseudo-moduli" (cioè c'è una mappatura sotto il cappuccio implementata dal resolver - credo).
In generale, un buon punto di partenza, se si desidera sapere come utilizzare a livello di programmazione Ivy, è la classe principale 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"})
);
}
Perché Ivy un requisito? Sono accettabili altre librerie/snippet di codice? – kdgregory
Certo, se si dispone di una soluzione alternativa che memorizza nella cache localmente e garantisce che i download non vengano danneggiati, va bene anche questo. –
Originariamente raccomandavo [Aether] (http://www.sonatype.org/aether), ma poi ho guardato i loro [esempi] (http://git.eclipse.org/c/aether/aether- demo.git/tree /) e [API] (http://download.eclipse.org/aether/aether-core/0.9.0.M2/apidocs/), e capito perché le persone prendono in giro i programmatori Java. – kdgregory