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.

Build.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.JCOMMANDER, BuildType.RUNTIME);
  52. downloadFromApache(MavenObject.JCOMMANDER, BuildType.COMPILETIME);
  53. downloadFromApache(MavenObject.MARKDOWNPAPERS, BuildType.RUNTIME);
  54. downloadFromApache(MavenObject.MARKDOWNPAPERS, BuildType.COMPILETIME);
  55. downloadFromApache(MavenObject.JUNIT, BuildType.RUNTIME);
  56. downloadFromApache(MavenObject.DOCLAVA, BuildType.RUNTIME);
  57. downloadFromApache(MavenObject.DOCLAVA, BuildType.COMPILETIME);
  58. downloadFromApache(MavenObject.SLF4JAPI, BuildType.RUNTIME);
  59. downloadFromApache(MavenObject.SLF4JAPI, BuildType.COMPILETIME);
  60. // needed for site publishing
  61. downloadFromApache(MavenObject.COMMONSNET, BuildType.RUNTIME);
  62. }
  63. /**
  64. * Download a file from the official Apache Maven repository.
  65. *
  66. * @param mo
  67. * the maven object to download.
  68. * @return
  69. */
  70. private static List<File> downloadFromApache(MavenObject mo, BuildType type) {
  71. return downloadFromMaven("http://repo1.maven.org/maven2/", mo, type);
  72. }
  73. /**
  74. * Download a file from a Maven repository.
  75. *
  76. * @param mo
  77. * the maven object to download.
  78. * @return
  79. */
  80. private static List<File> downloadFromMaven(String mavenRoot, MavenObject mo, BuildType type) {
  81. List<File> downloads = new ArrayList<File>();
  82. String[] jars = { "" };
  83. if (BuildType.RUNTIME.equals(type)) {
  84. jars = new String[] { "" };
  85. } else if (BuildType.COMPILETIME.equals(type)) {
  86. jars = new String[] { "-sources", "-javadoc" };
  87. }
  88. for (String jar : jars) {
  89. File targetFile = mo.getLocalFile("ext", jar);
  90. if (targetFile.exists()) {
  91. downloads.add(targetFile);
  92. continue;
  93. }
  94. String expectedSHA1 = mo.getSHA1(jar);
  95. if (expectedSHA1 == null) {
  96. // skip this jar
  97. continue;
  98. }
  99. String mavenURL = mavenRoot + mo.getRepositoryPath(jar);
  100. if (!targetFile.getAbsoluteFile().getParentFile().exists()) {
  101. boolean success = targetFile.getAbsoluteFile().getParentFile().mkdirs();
  102. if (!success) {
  103. throw new RuntimeException("Failed to create destination folder structure!");
  104. }
  105. }
  106. ByteArrayOutputStream buff = new ByteArrayOutputStream();
  107. try {
  108. URL url = new URL(mavenURL);
  109. InputStream in = new BufferedInputStream(url.openStream());
  110. byte[] buffer = new byte[4096];
  111. System.out.println("d/l: " + targetFile.getName());
  112. while (true) {
  113. int len = in.read(buffer);
  114. if (len < 0) {
  115. break;
  116. }
  117. buff.write(buffer, 0, len);
  118. }
  119. in.close();
  120. } catch (IOException e) {
  121. throw new RuntimeException("Error downloading " + mavenURL + " to " + targetFile, e);
  122. }
  123. byte[] data = buff.toByteArray();
  124. String calculatedSHA1 = StringUtils.calculateSHA1(data);
  125. System.out.println();
  126. if (expectedSHA1.length() == 0) {
  127. System.out.println("sha: " + calculatedSHA1);
  128. System.out.println();
  129. } else {
  130. if (!calculatedSHA1.equals(expectedSHA1)) {
  131. throw new RuntimeException("SHA1 checksum mismatch; got: " + calculatedSHA1);
  132. }
  133. }
  134. try {
  135. RandomAccessFile ra = new RandomAccessFile(targetFile, "rw");
  136. ra.write(data);
  137. ra.setLength(data.length);
  138. ra.close();
  139. } catch (IOException e) {
  140. throw new RuntimeException("Error writing to file " + targetFile, e);
  141. }
  142. downloads.add(targetFile);
  143. }
  144. return downloads;
  145. }
  146. /**
  147. * Class that describes a retrievable Maven object.
  148. */
  149. private static class MavenObject {
  150. public static final MavenObject JCOMMANDER = new MavenObject("com/beust", "jcommander", "1.17",
  151. "219a3540f3b27d7cc3b1d91d6ea046cd8723290e", "0bb50eec177acf0e94d58e0cf07262fe5164331d",
  152. "c7adc475ca40c288c93054e0f4fe58f3a98c0cb5");
  153. public static final MavenObject H2 = new MavenObject("com/h2database", "h2", "1.3.158",
  154. "4bac13427caeb32ef6e93b70101e61f370c7b5e2", "6bb165156a0831879fa7797df6e18bdcd4421f2d",
  155. "446d3f58c44992534cb54f67134532d95961904a");
  156. public static final MavenObject JUNIT = new MavenObject("junit", "junit", "4.8.2",
  157. "c94f54227b08100974c36170dcb53329435fe5ad", "", "");
  158. public static final MavenObject MARKDOWNPAPERS = new MavenObject("org/tautua/markdownpapers",
  159. "markdownpapers-core", "1.1.0", "b879b4720fa642d3c490ab559af132daaa16dbb4",
  160. "d98c53939815be2777d5a56dcdc3bbc9ddb468fa", "4c09d2d3073e85b973572292af00bd69681df76b");
  161. public static final MavenObject COMMONSNET = new MavenObject("commons-net", "commons-net", "1.4.0",
  162. "eb47e8cad2dd7f92fd7e77df1d1529cae87361f7", "", "");
  163. public static final MavenObject DOCLAVA = new MavenObject("com/google/doclava", "doclava", "1.0.3",
  164. "5a1e05977fd36480b0cf314410440f88e3a0049e", "6e314df1733455d66b98b56014363172773d0905",
  165. "1c1aa631b235439356e6e5803319caca80aaaa88");
  166. public static final MavenObject SLF4JAPI = new MavenObject("org/slf4j", "slf4j-api", "1.6.1",
  167. "6f3b8a24bf970f17289b234284c94f43eb42f0e4", "46a386136c901748e6a3af67ebde6c22bc6b4524",
  168. "e223571d77769cdafde59040da235842f3326453");
  169. public final String group;
  170. public final String artifact;
  171. public final String version;
  172. public final String librarySHA1;
  173. public final String sourcesSHA1;
  174. public final String javadocSHA1;
  175. private MavenObject(String group, String artifact, String version, String librarySHA1,
  176. String sourcesSHA1, String javadocSHA1) {
  177. this.group = group;
  178. this.artifact = artifact;
  179. this.version = version;
  180. this.librarySHA1 = librarySHA1;
  181. this.sourcesSHA1 = sourcesSHA1;
  182. this.javadocSHA1 = javadocSHA1;
  183. }
  184. private String getRepositoryPath(String jar) {
  185. return group + "/" + artifact + "/" + version + "/" + artifact + "-" + version + jar + ".jar";
  186. }
  187. private File getLocalFile(String basePath, String jar) {
  188. return new File(basePath, artifact + "-" + version + jar + ".jar");
  189. }
  190. private String getSHA1(String jar) {
  191. if (jar.equals("")) {
  192. return librarySHA1;
  193. } else if (jar.equals("-sources")) {
  194. return sourcesSHA1;
  195. } else if (jar.equals("-javadoc")) {
  196. return javadocSHA1;
  197. }
  198. return librarySHA1;
  199. }
  200. @Override
  201. public String toString() {
  202. return group;
  203. }
  204. }
  205. }