Skip to content

Instantly share code, notes, and snippets.

@dbazile
Created February 6, 2021 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbazile/5ee15212699c85000f7076fd2c8725cb to your computer and use it in GitHub Desktop.
Save dbazile/5ee15212699c85000f7076fd2c8725cb to your computer and use it in GitHub Desktop.
Jenkins plugin download
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.jar.JarFile;
class PluginDownloader {
private static final String REPO = "https://updates.jenkins-ci.org/latest/";
public static void main(String[] args) throws Exception {
final Set<String> needed = new HashSet<>(Set.of(args));
final Set<String> onHand = new HashSet<>();
while (!needed.isEmpty()) {
final String plugin = needed.stream().sorted().findFirst().get();
needed.addAll(fetch(plugin));
// Mark the plugin as downloaded
onHand.add(plugin);
// Remove the stuff we already have
needed.removeAll(onHand);
}
}
private static Set<String> fetch(String plugin) throws IOException, MalformedURLException {
final Path path = Paths.get(plugin + ".hpi");
if (!Files.exists(path)) {
final URL url = new URL(REPO + path);
System.out.println("\033[33mfetch:\033[0m " + url);
Files.copy(url.openStream(), path);
}
else {
System.out.println("\033[32mpresent:\033[0m " + path);
}
// Parse dependencies
final String dependencies = new JarFile(path.toFile())
.getManifest()
.getMainAttributes()
.getValue("Plugin-Dependencies");
if (dependencies == null) {
return Collections.emptySet();
}
// Return list of dependencies
return Arrays.stream(dependencies.split(","))
.map(s -> s.replaceAll(":.*$", ""))
.sorted()
.collect(Collectors.toSet());
}
private static void print(String message, Object... args) {
System.out.printf(message + "\n", args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment