Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.gitblit;
  2. import java.io.BufferedInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.RandomAccessFile;
  8. import java.net.URL;
  9. import java.security.MessageDigest;
  10. import java.security.NoSuchAlgorithmException;
  11. public class Build {
  12. public static void main(String... args) {
  13. runtime();
  14. compiletime();
  15. }
  16. public static void runtime() {
  17. downloadFromMaven(MavenObject.JCOMMANDER);
  18. downloadFromMaven(MavenObject.JETTY);
  19. downloadFromMaven(MavenObject.SERVLET);
  20. downloadFromMaven(MavenObject.SLF4JAPI);
  21. downloadFromMaven(MavenObject.SLF4LOG4J);
  22. downloadFromMaven(MavenObject.LOG4J);
  23. downloadFromMaven(MavenObject.WICKET);
  24. downloadFromMaven(MavenObject.WICKET_EXT);
  25. downloadFromMaven(MavenObject.WICKET_AUTH_ROLES);
  26. }
  27. public static void compiletime() {
  28. downloadFromMaven(MavenObject.JUNIT);
  29. }
  30. /**
  31. * Download a file from a Maven repository.
  32. *
  33. * @param mo
  34. * the maven object to download.
  35. * @return
  36. */
  37. private static File downloadFromMaven(MavenObject mo) {
  38. File targetFile = mo.getLocalFile("ext");
  39. if (targetFile.exists()) {
  40. return targetFile;
  41. }
  42. String mavenURL = "http://repo1.maven.org/maven2/" + mo.getRepositoryPath();
  43. if (!targetFile.getAbsoluteFile().getParentFile().exists()) {
  44. boolean success = targetFile.getAbsoluteFile().getParentFile().mkdirs();
  45. if (!success) {
  46. throw new RuntimeException("Failed to create destination folder structure!");
  47. }
  48. }
  49. ByteArrayOutputStream buff = new ByteArrayOutputStream();
  50. try {
  51. System.out.println("Downloading " + mavenURL);
  52. URL url = new URL(mavenURL);
  53. InputStream in = new BufferedInputStream(url.openStream());
  54. long last = System.currentTimeMillis();
  55. int len = 0;
  56. while (true) {
  57. long now = System.currentTimeMillis();
  58. if (now > last + 200) {
  59. System.out.println(" downloaded " + len + " bytes");
  60. last = now;
  61. }
  62. int x = in.read();
  63. len++;
  64. if (x < 0) {
  65. break;
  66. }
  67. buff.write(x);
  68. }
  69. in.close();
  70. } catch (IOException e) {
  71. throw new RuntimeException("Error downloading " + mavenURL + " to " + targetFile, e);
  72. }
  73. byte[] data = buff.toByteArray();
  74. String got = getSHA1(data);
  75. if (mo.sha1 != null && !got.equals(mo.sha1)) {
  76. throw new RuntimeException("SHA1 checksum mismatch; got: " + got);
  77. }
  78. try {
  79. RandomAccessFile ra = new RandomAccessFile(targetFile, "rw");
  80. ra.write(data);
  81. ra.setLength(data.length);
  82. ra.close();
  83. } catch (IOException e) {
  84. throw new RuntimeException("Error writing to file " + targetFile, e);
  85. }
  86. return targetFile;
  87. }
  88. /**
  89. * Generate the SHA1 checksum of a byte array.
  90. *
  91. * @param data
  92. * the byte array
  93. * @return the SHA1 checksum
  94. */
  95. public static String getSHA1(byte[] data) {
  96. MessageDigest md;
  97. try {
  98. md = MessageDigest.getInstance("SHA-1");
  99. byte[] value = md.digest(data);
  100. StringBuilder buff = new StringBuilder(value.length * 2);
  101. for (byte c : value) {
  102. int x = c & 0xff;
  103. buff.append(Integer.toString(x >> 4, 16)).append(Integer.toString(x & 0xf, 16));
  104. }
  105. return buff.toString();
  106. } catch (NoSuchAlgorithmException e) {
  107. throw new RuntimeException(e);
  108. }
  109. }
  110. private static class MavenObject {
  111. public static final MavenObject JCOMMANDER = new MavenObject("jCommander", "com/beust", "jcommander", "1.17", "219a3540f3b27d7cc3b1d91d6ea046cd8723290e");
  112. public static final MavenObject JETTY = new MavenObject("Jetty", "org/eclipse/jetty/aggregate", "jetty-all", "7.2.2.v20101205", "b9b7c812a732721c427e208c54fbb71ca17a2ee1");
  113. public static final MavenObject SERVLET = new MavenObject("Servlet 2.5", "javax/servlet", "servlet-api", "2.5", "5959582d97d8b61f4d154ca9e495aafd16726e34");
  114. public static final MavenObject SLF4JAPI = new MavenObject("SLF4J API", "org/slf4j", "slf4j-api", "1.6.1", "6f3b8a24bf970f17289b234284c94f43eb42f0e4");
  115. public static final MavenObject SLF4LOG4J = new MavenObject("SLF4J LOG4J", "org/slf4j", "slf4j-log4j12", "1.6.1", "bd245d6746cdd4e6203e976e21d597a46f115802");
  116. public static final MavenObject LOG4J = new MavenObject("Apache LOG4J", "log4j", "log4j", "1.2.16", "7999a63bfccbc7c247a9aea10d83d4272bd492c6");
  117. public static final MavenObject WICKET = new MavenObject("Apache Wicket", "org/apache/wicket", "wicket", "1.4.17", "39815e37a6f56465b2d2c3d3017c4f3bf17db50a");
  118. public static final MavenObject WICKET_EXT = new MavenObject("Apache Wicket Extensions", "org/apache/wicket", "wicket-extensions", "1.4.17", "01111d0dbffdc425581b006a43864c22797ce72a");
  119. public static final MavenObject WICKET_AUTH_ROLES = new MavenObject("Apache Wicket Auth Roles", "org/apache/wicket", "wicket-auth-roles", "1.4.17", "86d20ff32f62d3026213ff11a78555da643bc676");
  120. public static final MavenObject JUNIT = new MavenObject("JUnit", "junit", "junit", "3.8.2", "07e4cde26b53a9a0e3fe5b00d1dbbc7cc1d46060");
  121. public final String name;
  122. public final String group;
  123. public final String artifact;
  124. public final String version;
  125. public final String sha1;
  126. private MavenObject(String name, String group, String artifact, String version, String sha1) {
  127. this.name = name;
  128. this.group = group;
  129. this.artifact = artifact;
  130. this.version = version;
  131. this.sha1 = sha1;
  132. }
  133. private String getRepositoryPath() {
  134. return group + "/" + artifact + "/" + version + "/" + artifact + "-" + version + ".jar";
  135. }
  136. private File getLocalFile(String basePath) {
  137. return new File(basePath, artifact + "-" + version + ".jar");
  138. }
  139. @Override
  140. public String toString() {
  141. return name;
  142. }
  143. }
  144. }