MavenWrapperDownloader.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import java.net.*;
  20. import java.io.*;
  21. import java.nio.channels.*;
  22. import java.util.Properties;
  23. public class MavenWrapperDownloader
  24. {
  25. private static final String WRAPPER_VERSION = "3.1.1";
  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/org/apache/maven/wrapper/maven-wrapper/" + WRAPPER_VERSION
  31. + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
  32. /**
  33. * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to use instead of the
  34. * default one.
  35. */
  36. private static final String MAVEN_WRAPPER_PROPERTIES_PATH = ".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 = ".mvn/wrapper/maven-wrapper.jar";
  41. /**
  42. * Name of the property which should be used to override the default download url for the wrapper.
  43. */
  44. private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
  45. public static void main( String args[] )
  46. {
  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. {
  56. FileInputStream mavenWrapperPropertyFileInputStream = null;
  57. try
  58. {
  59. mavenWrapperPropertyFileInputStream = new FileInputStream( mavenWrapperPropertyFile );
  60. Properties mavenWrapperProperties = new Properties();
  61. mavenWrapperProperties.load( mavenWrapperPropertyFileInputStream );
  62. url = mavenWrapperProperties.getProperty( PROPERTY_NAME_WRAPPER_URL, url );
  63. }
  64. catch ( IOException e )
  65. {
  66. System.out.println( "- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'" );
  67. }
  68. finally
  69. {
  70. try
  71. {
  72. if ( mavenWrapperPropertyFileInputStream != null )
  73. {
  74. mavenWrapperPropertyFileInputStream.close();
  75. }
  76. }
  77. catch ( IOException e )
  78. {
  79. // Ignore ...
  80. }
  81. }
  82. }
  83. System.out.println( "- Downloading from: " + url );
  84. File outputFile = new File( baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH );
  85. if ( !outputFile.getParentFile().exists() )
  86. {
  87. if ( !outputFile.getParentFile().mkdirs() )
  88. {
  89. System.out.println( "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath()
  90. + "'" );
  91. }
  92. }
  93. System.out.println( "- Downloading to: " + outputFile.getAbsolutePath() );
  94. try
  95. {
  96. downloadFileFromURL( url, outputFile );
  97. System.out.println( "Done" );
  98. System.exit( 0 );
  99. }
  100. catch ( Throwable e )
  101. {
  102. System.out.println( "- Error downloading" );
  103. e.printStackTrace();
  104. System.exit( 1 );
  105. }
  106. }
  107. private static void downloadFileFromURL( String urlString, File destination )
  108. throws Exception
  109. {
  110. if ( System.getenv( "MVNW_USERNAME" ) != null && System.getenv( "MVNW_PASSWORD" ) != null )
  111. {
  112. String username = System.getenv( "MVNW_USERNAME" );
  113. char[] password = System.getenv( "MVNW_PASSWORD" ).toCharArray();
  114. Authenticator.setDefault( new Authenticator()
  115. {
  116. @Override
  117. protected PasswordAuthentication getPasswordAuthentication()
  118. {
  119. return new PasswordAuthentication( username, password );
  120. }
  121. } );
  122. }
  123. URL website = new URL( urlString );
  124. ReadableByteChannel rbc;
  125. rbc = Channels.newChannel( website.openStream() );
  126. FileOutputStream fos = new FileOutputStream( destination );
  127. fos.getChannel().transferFrom( rbc, 0, Long.MAX_VALUE );
  128. fos.close();
  129. rbc.close();
  130. }
  131. }