Tuesday, October 15, 2013

How to Programmatically Download Dependencies using Ivy

Below is an example how to programmatically download dependencies using Ivy.
apply plugin: "java"
apply plugin: "eclipse"

repositories {
    mavenCentral()
}

dependencies {
    compile "org.apache.ivy:ivy:2.3.0"
    testCompile "junit:junit:4.10"
}
import java.util.ArrayList;
import java.util.List;

import org.apache.ivy.Main;

public class Test {
    public static void main(String[] args) throws Exception {
        List<String> list = new ArrayList<>();
        list.add("-settings");
        list.add("ivysettings.xml");
        list.add("-dependency");
        list.add("testgroup");
        list.add("testartifact");
        list.add("1.0.0");
        list.add("-retrieve");
        list.add("lib/[artifact]-[revision].[ext]");
        String[] newArgs = new String[list.size()];
        Main.main(list.toArray(newArgs));
    }
}
<ivysettings>
  <settings defaultResolver="myResolver"/>
  <property name="ivy.checksums" value="" />
  <include url="${ivy.default.settings.dir}/ivysettings-public.xml"/>
  <include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>
  <include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>
  <include url="${ivy.default.settings.dir}/ivysettings-main-chain.xml"/>
  <include url="${ivy.default.settings.dir}/ivysettings-default-chain.xml"/>
  <resolvers>
    <url name="testivy">
      <ivy pattern="http://myivyserver/[organisation]/[module]/[revision]/ivy-[revision].xml" /> -->
      <artifact pattern="http://myivyserver/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
    </url>
    <ibiblio name="testmaven" m2compatible="true" root="http://mymavenserver/maven" />
    <chain name="myResolver">
      <resolver ref="default" />
      <resolver ref="testivy" />
      <resolver ref="testmaven" />
    </chain>
  </resolvers>
</ivysettings>

No comments:

Post a Comment