Created
January 23, 2017 04:26
-
-
Save XuefengWu/a481d6086c23951a05445b02bc1e567e to your computer and use it in GitHub Desktop.
download jenkins plugin and dependencies offline for the internal jenkins server cannot access internet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileOutputStream | |
import java.net.URL | |
import java.nio.channels.{Channels, ReadableByteChannel} | |
import java.util.Scanner | |
import java.util.zip.ZipFile | |
object JenkinsPluginDownloader { | |
def main(args: Array[String]): Unit = { | |
if(args.length < 2) { | |
println(s"usage: scala JenkinsPluginDownloader junit 1.19") | |
System.exit(1) | |
} | |
val name = args(0) | |
val version = args(1) | |
downloadPlugin(name, version) | |
} | |
def downloadPlugin(name: String, version: String): Unit = { | |
val url = s"http://updates.jenkins-ci.org/download/plugins/$name/$version/${name}.hpi" | |
val fileUrl = new URL(url) | |
println(s"downloading from: $url") | |
val rbc: ReadableByteChannel = Channels.newChannel(fileUrl.openStream()) | |
val file = s"${name}.hpi" | |
val fos = new FileOutputStream(file) | |
fos.getChannel().transferFrom(rbc, 0, Long.MaxValue) | |
downloadDependency(file) | |
} | |
def downloadDependency(file: String): Unit = { | |
val zipFile = new ZipFile(file) | |
val entry = zipFile.getEntry("META-INF/MANIFEST.MF") | |
val inputStream = zipFile.getInputStream(entry) | |
val context = new Scanner(inputStream).useDelimiter("\\A").next() | |
val dependencies = context.split("\n").filter(_.contains("Plugin-Dependencies")).map(_.split(":")) | |
dependencies.foreach(v => downloadPlugin(v(1).trim, v(2).trim)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment