MavenWrapperDownloader.java 4.2 KB

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