MavenConfigurationImpl config = new MavenConfigurationImpl(new PropertiesPropertyResolver(System.getProperties()), "org.ops4j.pax.url.mvn");
config.setSettings(new MavenSettingsImpl(config.getSettingsFileUrl(), config.useFallbackRepositories()));
DownloadManager dm = new DownloadManager(config, Executors.newSingleThreadExecutor());
final CountDownLatch latch = new CountDownLatch(1);
final DownloadFuture df = dm.download(String.format("mvn:%s/%s/%s", archetype.groupId, archetype.artifactId, archetype.version));
df.addListener(new FutureListener<DownloadFuture>() {
@Override
public void operationComplete(DownloadFuture future) {
latch.countDown();
}
});
// wait for download
try {
boolean init = false;
for (int i = 0; i < 2 * 60 && latch.getCount() > 0; i++) {
// dont do anything in the first 3 seconds as we likely can download it faster
if (i > 3) {
if (!init) {
System.out.print("Downloading archetype in progress: ");
init = true;
}
System.out.print(".");
}
// only sleep 0.5 sec so we can react faster
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.err.println("\nFailed to download " + archetype);
throw new IOException(e.getMessage(), e);
}
if (latch.getCount() == 0) {
return df.getFile();
} else {
System.err.println("\nFailed to download archetype within 60 seconds: " + archetype);
throw new IOException("Failed to download archetype within 60 seconds: " + archetype);
}
}