1
0

MavenWrapperDownloader.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2007-present the original author or authors.
  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. * https://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. import java.util.Properties;
  17. public class MavenWrapperDownloader {
  18. private static final String WRAPPER_VERSION = "0.5.6";
  19. /**
  20. * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
  21. */
  22. private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
  23. + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
  24. /**
  25. * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
  26. * use instead of the default one.
  27. */
  28. private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
  29. ".mvn/wrapper/maven-wrapper.properties";
  30. /**
  31. * Path where the maven-wrapper.jar will be saved to.
  32. */
  33. private static final String MAVEN_WRAPPER_JAR_PATH =
  34. ".mvn/wrapper/maven-wrapper.jar";
  35. /**
  36. * Name of the property which should be used to override the default download url for the wrapper.
  37. */
  38. private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
  39. public static void main(String args[]) {
  40. System.out.println("- Downloader started");
  41. File baseDirectory = new File(args[0]);
  42. System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
  43. // If the maven-wrapper.properties exists, read it and check if it contains a custom
  44. // wrapperUrl parameter.
  45. File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
  46. String url = DEFAULT_DOWNLOAD_URL;
  47. if (mavenWrapperPropertyFile.exists()) {
  48. FileInputStream mavenWrapperPropertyFileInputStream = null;
  49. try {
  50. mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
  51. Properties mavenWrapperProperties = new Properties();
  52. mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
  53. url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
  54. } catch (IOException e) {
  55. System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
  56. } finally {
  57. try {
  58. if (mavenWrapperPropertyFileInputStream != null) {
  59. mavenWrapperPropertyFileInputStream.close();
  60. }
  61. } catch (IOException e) {
  62. // Ignore ...
  63. }
  64. }
  65. }
  66. System.out.println("- Downloading from: " + url);
  67. File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
  68. if (!outputFile.getParentFile().exists()) {
  69. if (!outputFile.getParentFile().mkdirs()) {
  70. System.out.println(
  71. "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
  72. }
  73. }
  74. System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
  75. try {
  76. downloadFileFromURL(url, outputFile);
  77. System.out.println("Done");
  78. System.exit(0);
  79. } catch (Throwable e) {
  80. System.out.println("- Error downloading");
  81. e.printStackTrace();
  82. System.exit(1);
  83. }
  84. }
  85. private static void downloadFileFromURL(String urlString, File destination) throws Exception {
  86. if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
  87. String username = System.getenv("MVNW_USERNAME");
  88. char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
  89. Authenticator.setDefault(new Authenticator() {
  90. @Override
  91. protected PasswordAuthentication getPasswordAuthentication() {
  92. return new PasswordAuthentication(username, password);
  93. }
  94. });
  95. }
  96. URL website = new URL(urlString);
  97. ReadableByteChannel rbc;
  98. rbc = Channels.newChannel(website.openStream());
  99. FileOutputStream fos = new FileOutputStream(destination);
  100. fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  101. fos.close();
  102. rbc.close();
  103. }
  104. }