MavenWrapperDownloader.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. https://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.net.URL;
  22. import java.nio.channels.Channels;
  23. import java.nio.channels.ReadableByteChannel;
  24. import java.util.Properties;
  25. public class MavenWrapperDownloader {
  26. /**
  27. * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
  28. */
  29. private static final String DEFAULT_DOWNLOAD_URL =
  30. "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
  31. /**
  32. * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
  33. * use instead of the default one.
  34. */
  35. private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
  36. ".mvn/wrapper/maven-wrapper.properties";
  37. /**
  38. * Path where the maven-wrapper.jar will be saved to.
  39. */
  40. private static final String MAVEN_WRAPPER_JAR_PATH =
  41. ".mvn/wrapper/maven-wrapper.jar";
  42. /**
  43. * Name of the property which should be used to override the default download url for the wrapper.
  44. */
  45. private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
  46. public static void main(String args[]) {
  47. System.out.println("- Downloader started");
  48. File baseDirectory = new File(args[0]);
  49. System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
  50. // If the maven-wrapper.properties exists, read it and check if it contains a custom
  51. // wrapperUrl parameter.
  52. File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
  53. String url = DEFAULT_DOWNLOAD_URL;
  54. if (mavenWrapperPropertyFile.exists()) {
  55. FileInputStream mavenWrapperPropertyFileInputStream = null;
  56. try {
  57. mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
  58. Properties mavenWrapperProperties = new Properties();
  59. mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
  60. url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
  61. } catch (IOException e) {
  62. System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
  63. } finally {
  64. try {
  65. if (mavenWrapperPropertyFileInputStream != null) {
  66. mavenWrapperPropertyFileInputStream.close();
  67. }
  68. } catch (IOException e) {
  69. // Ignore ...
  70. }
  71. }
  72. }
  73. System.out.println("- Downloading from: : " + url);
  74. File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
  75. if (!outputFile.getParentFile().exists()) {
  76. if (!outputFile.getParentFile().mkdirs()) {
  77. System.out.println(
  78. "- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
  79. }
  80. }
  81. System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
  82. try {
  83. downloadFileFromURL(url, outputFile);
  84. System.out.println("Done");
  85. System.exit(0);
  86. } catch (Throwable e) {
  87. System.out.println("- Error downloading");
  88. e.printStackTrace();
  89. System.exit(1);
  90. }
  91. }
  92. private static void downloadFileFromURL(String urlString, File destination) throws Exception {
  93. URL website = new URL(urlString);
  94. ReadableByteChannel rbc;
  95. rbc = Channels.newChannel(website.openStream());
  96. FileOutputStream fos = new FileOutputStream(destination);
  97. fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  98. fos.close();
  99. rbc.close();
  100. }
  101. }