You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 12 години
преди 12 години
преди 12 години
преди 11 години
преди 12 години
преди 11 години
преди 11 години
преди 12 години
преди 12 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2011 James Moger.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.iciql.build;
  17. import java.io.BufferedInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.RandomAccessFile;
  23. import java.net.URL;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import com.iciql.util.StringUtils;
  27. /**
  28. * The Build class downloads runtime and compile-time jar files from the Apache
  29. * Maven repositories.
  30. *
  31. * Its important that this class have minimal compile dependencies since its
  32. * called very early in the build script.
  33. *
  34. */
  35. public class Build {
  36. /**
  37. * BuildTypes
  38. */
  39. public static enum BuildType {
  40. RUNTIME, COMPILETIME;
  41. }
  42. public static void main(String... args) {
  43. runtime();
  44. compiletime();
  45. }
  46. public static void runtime() {
  47. }
  48. public static void compiletime() {
  49. downloadFromApache(MavenObject.H2, BuildType.RUNTIME);
  50. downloadFromApache(MavenObject.H2, BuildType.COMPILETIME);
  51. downloadFromApache(MavenObject.HSQLDB, BuildType.RUNTIME);
  52. downloadFromApache(MavenObject.DERBY, BuildType.RUNTIME);
  53. downloadFromApache(MavenObject.MYSQL, BuildType.RUNTIME);
  54. downloadFromApache(MavenObject.POSTGRESQL, BuildType.RUNTIME);
  55. downloadFromApache(MavenObject.JCOMMANDER, BuildType.RUNTIME);
  56. downloadFromApache(MavenObject.JCOMMANDER, BuildType.COMPILETIME);
  57. downloadFromApache(MavenObject.MARKDOWNPAPERS, BuildType.RUNTIME);
  58. downloadFromApache(MavenObject.MARKDOWNPAPERS, BuildType.COMPILETIME);
  59. downloadFromApache(MavenObject.JUNIT, BuildType.RUNTIME);
  60. downloadFromApache(MavenObject.DOCLAVA, BuildType.RUNTIME);
  61. downloadFromApache(MavenObject.DOCLAVA, BuildType.COMPILETIME);
  62. downloadFromApache(MavenObject.SLF4JAPI, BuildType.RUNTIME);
  63. downloadFromApache(MavenObject.SLF4JAPI, BuildType.COMPILETIME);
  64. downloadFromApache(MavenObject.COMMONSPOOL, BuildType.RUNTIME);
  65. downloadFromApache(MavenObject.COMMONSPOOL, BuildType.COMPILETIME);
  66. downloadFromApache(MavenObject.COMMONSDBCP, BuildType.RUNTIME);
  67. downloadFromApache(MavenObject.COMMONSDBCP, BuildType.COMPILETIME);
  68. // needed for site publishing
  69. downloadFromApache(MavenObject.COMMONSNET, BuildType.RUNTIME);
  70. }
  71. /**
  72. * Download a file from the official Apache Maven repository.
  73. *
  74. * @param mo
  75. * the maven object to download.
  76. * @return
  77. */
  78. private static List<File> downloadFromApache(MavenObject mo, BuildType type) {
  79. return downloadFromMaven("http://repo1.maven.org/maven2/", mo, type);
  80. }
  81. /**
  82. * Download a file from a Maven repository.
  83. *
  84. * @param mo
  85. * the maven object to download.
  86. * @return
  87. */
  88. private static List<File> downloadFromMaven(String mavenRoot, MavenObject mo, BuildType type) {
  89. List<File> downloads = new ArrayList<File>();
  90. String[] jars = { "" };
  91. if (BuildType.RUNTIME.equals(type)) {
  92. jars = new String[] { "" };
  93. } else if (BuildType.COMPILETIME.equals(type)) {
  94. jars = new String[] { "-sources", "-javadoc" };
  95. }
  96. for (String jar : jars) {
  97. File targetFile = mo.getLocalFile("ext", jar);
  98. if (targetFile.exists()) {
  99. downloads.add(targetFile);
  100. continue;
  101. }
  102. String expectedSHA1 = mo.getSHA1(jar);
  103. if (expectedSHA1 == null) {
  104. // skip this jar
  105. continue;
  106. }
  107. String mavenURL = mavenRoot + mo.getRepositoryPath(jar);
  108. if (!targetFile.getAbsoluteFile().getParentFile().exists()) {
  109. boolean success = targetFile.getAbsoluteFile().getParentFile().mkdirs();
  110. if (!success) {
  111. throw new RuntimeException("Failed to create destination folder structure!");
  112. }
  113. }
  114. ByteArrayOutputStream buff = new ByteArrayOutputStream();
  115. try {
  116. URL url = new URL(mavenURL);
  117. InputStream in = new BufferedInputStream(url.openStream());
  118. byte[] buffer = new byte[4096];
  119. System.out.println("d/l: " + targetFile.getName());
  120. while (true) {
  121. int len = in.read(buffer);
  122. if (len < 0) {
  123. break;
  124. }
  125. buff.write(buffer, 0, len);
  126. }
  127. in.close();
  128. } catch (IOException e) {
  129. throw new RuntimeException("Error downloading " + mavenURL + " to " + targetFile, e);
  130. }
  131. byte[] data = buff.toByteArray();
  132. String calculatedSHA1 = StringUtils.calculateSHA1(data);
  133. System.out.println();
  134. if (expectedSHA1.length() == 0) {
  135. System.out.println("sha: " + calculatedSHA1);
  136. System.out.println();
  137. } else {
  138. if (!calculatedSHA1.equals(expectedSHA1)) {
  139. throw new RuntimeException("SHA1 checksum mismatch; got: " + calculatedSHA1);
  140. }
  141. }
  142. try {
  143. RandomAccessFile ra = new RandomAccessFile(targetFile, "rw");
  144. ra.write(data);
  145. ra.setLength(data.length);
  146. ra.close();
  147. } catch (IOException e) {
  148. throw new RuntimeException("Error writing to file " + targetFile, e);
  149. }
  150. downloads.add(targetFile);
  151. }
  152. return downloads;
  153. }
  154. /**
  155. * Class that describes a retrievable Maven object.
  156. */
  157. private static class MavenObject {
  158. public static final MavenObject JCOMMANDER = new MavenObject("com/beust", "jcommander", "1.17",
  159. "219a3540f3b27d7cc3b1d91d6ea046cd8723290e", "0bb50eec177acf0e94d58e0cf07262fe5164331d",
  160. "c7adc475ca40c288c93054e0f4fe58f3a98c0cb5");
  161. public static final MavenObject H2 = new MavenObject("com/h2database", "h2", "1.3.168",
  162. "eb32936a239d95220f5b2d2973a7b17372f98b54", "61da28f8c48d07c099fc78d72c6152b84d89b4ca",
  163. "724ff8347553919e703a369a856713a8e7ef4fac");
  164. public static final MavenObject HSQLDB = new MavenObject("org/hsqldb", "hsqldb", "2.2.8",
  165. "8231a3ff71ba5889f9e2d01ce13503cbdd4038e9", "", "");
  166. public static final MavenObject DERBY = new MavenObject("org/apache/derby", "derby", "10.9.1.0",
  167. "4538cf5564ab3c262eec65c55fdb13965625589c", "", "");
  168. public static final MavenObject MYSQL = new MavenObject("mysql", "mysql-connector-java", "5.1.15",
  169. "0fbc80454d27cc65f3addfa516707e9f8e60c3eb", "", "");
  170. public static final MavenObject POSTGRESQL = new MavenObject("postgresql", "postgresql", "9.0-801.jdbc4",
  171. "153f2f92a786f12fc111d0111f709012df87c808", "", "");
  172. public static final MavenObject JUNIT = new MavenObject("junit", "junit", "4.8.2",
  173. "c94f54227b08100974c36170dcb53329435fe5ad", "", "");
  174. public static final MavenObject MARKDOWNPAPERS = new MavenObject("org/tautua/markdownpapers",
  175. "markdownpapers-core", "1.1.0", "b879b4720fa642d3c490ab559af132daaa16dbb4",
  176. "d98c53939815be2777d5a56dcdc3bbc9ddb468fa", "4c09d2d3073e85b973572292af00bd69681df76b");
  177. public static final MavenObject COMMONSNET = new MavenObject("commons-net", "commons-net", "1.4.0",
  178. "eb47e8cad2dd7f92fd7e77df1d1529cae87361f7", "", "");
  179. public static final MavenObject DOCLAVA = new MavenObject("com/google/doclava", "doclava", "1.0.3",
  180. "5a1e05977fd36480b0cf314410440f88e3a0049e", "6e314df1733455d66b98b56014363172773d0905",
  181. "1c1aa631b235439356e6e5803319caca80aaaa88");
  182. public static final MavenObject SLF4JAPI = new MavenObject("org/slf4j", "slf4j-api", "1.6.1",
  183. "6f3b8a24bf970f17289b234284c94f43eb42f0e4", "46a386136c901748e6a3af67ebde6c22bc6b4524",
  184. "e223571d77769cdafde59040da235842f3326453");
  185. public static final MavenObject COMMONSPOOL = new MavenObject("commons-pool", "commons-pool", "1.5.6",
  186. "16390e2d74df4ab08c06a85d42a74a951dc93ad7", "bbfb73ed3c341d9738c64da8157910b967f878d6",
  187. "d72204023b30cd9fecb64829586472f3c6806005");
  188. public static final MavenObject COMMONSDBCP = new MavenObject("commons-dbcp", "commons-dbcp", "1.4",
  189. "30be73c965cc990b153a100aaaaafcf239f82d39", "9b076ff231434d5403be6599a1347019b12c0def",
  190. "098bf7c8d5b026f6e3969259a36e813ac37432b3");
  191. public final String group;
  192. public final String artifact;
  193. public final String version;
  194. public final String librarySHA1;
  195. public final String sourcesSHA1;
  196. public final String javadocSHA1;
  197. private MavenObject(String group, String artifact, String version, String librarySHA1,
  198. String sourcesSHA1, String javadocSHA1) {
  199. this.group = group;
  200. this.artifact = artifact;
  201. this.version = version;
  202. this.librarySHA1 = librarySHA1;
  203. this.sourcesSHA1 = sourcesSHA1;
  204. this.javadocSHA1 = javadocSHA1;
  205. }
  206. private String getRepositoryPath(String jar) {
  207. return group + "/" + artifact + "/" + version + "/" + artifact + "-" + version + jar + ".jar";
  208. }
  209. private File getLocalFile(String basePath, String jar) {
  210. return new File(basePath, artifact + "-" + version + jar + ".jar");
  211. }
  212. private String getSHA1(String jar) {
  213. if (jar.equals("")) {
  214. return librarySHA1;
  215. } else if (jar.equals("-sources")) {
  216. return sourcesSHA1;
  217. } else if (jar.equals("-javadoc")) {
  218. return javadocSHA1;
  219. }
  220. return librarySHA1;
  221. }
  222. @Override
  223. public String toString() {
  224. return group;
  225. }
  226. }
  227. }