|
| 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 | + |
| 17 | +importjava.net.*; |
| 18 | +importjava.io.*; |
| 19 | +importjava.nio.channels.*; |
| 20 | +importjava.util.Properties; |
| 21 | + |
| 22 | +publicclassMavenWrapperDownloader{ |
| 23 | + |
| 24 | +privatestaticfinalStringWRAPPER_VERSION = "0.5.6"; |
| 25 | +/** |
| 26 | + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. |
| 27 | + */ |
| 28 | +privatestaticfinalStringDEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" |
| 29 | + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; |
| 30 | + |
| 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 | +privatestaticfinalStringMAVEN_WRAPPER_PROPERTIES_PATH = |
| 36 | +".mvn/wrapper/maven-wrapper.properties"; |
| 37 | + |
| 38 | +/** |
| 39 | + * Path where the maven-wrapper.jar will be saved to. |
| 40 | + */ |
| 41 | +privatestaticfinalStringMAVEN_WRAPPER_JAR_PATH = |
| 42 | +".mvn/wrapper/maven-wrapper.jar"; |
| 43 | + |
| 44 | +/** |
| 45 | + * Name of the property which should be used to override the default download url for the wrapper. |
| 46 | + */ |
| 47 | +privatestaticfinalStringPROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; |
| 48 | + |
| 49 | +publicstaticvoidmain(Stringargs[]){ |
| 50 | +System.out.println("- Downloader started"); |
| 51 | +FilebaseDirectory = newFile(args[0]); |
| 52 | +System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); |
| 53 | + |
| 54 | +// If the maven-wrapper.properties exists, read it and check if it contains a custom |
| 55 | +// wrapperUrl parameter. |
| 56 | +FilemavenWrapperPropertyFile = newFile(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); |
| 57 | +Stringurl = DEFAULT_DOWNLOAD_URL; |
| 58 | +if (mavenWrapperPropertyFile.exists()){ |
| 59 | +FileInputStreammavenWrapperPropertyFileInputStream = null; |
| 60 | +try{ |
| 61 | +mavenWrapperPropertyFileInputStream = newFileInputStream(mavenWrapperPropertyFile); |
| 62 | +PropertiesmavenWrapperProperties = newProperties(); |
| 63 | +mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); |
| 64 | +url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); |
| 65 | + } catch (IOExceptione){ |
| 66 | +System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); |
| 67 | + } finally{ |
| 68 | +try{ |
| 69 | +if (mavenWrapperPropertyFileInputStream != null){ |
| 70 | +mavenWrapperPropertyFileInputStream.close(); |
| 71 | + } |
| 72 | + } catch (IOExceptione){ |
| 73 | +// Ignore ... |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +System.out.println("- Downloading from: " + url); |
| 78 | + |
| 79 | +FileoutputFile = newFile(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); |
| 80 | +if (!outputFile.getParentFile().exists()){ |
| 81 | +if (!outputFile.getParentFile().mkdirs()){ |
| 82 | +System.out.println( |
| 83 | +"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); |
| 84 | + } |
| 85 | + } |
| 86 | +System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); |
| 87 | +try{ |
| 88 | +downloadFileFromURL(url, outputFile); |
| 89 | +System.out.println("Done"); |
| 90 | +System.exit(0); |
| 91 | + } catch (Throwablee){ |
| 92 | +System.out.println("- Error downloading"); |
| 93 | +e.printStackTrace(); |
| 94 | +System.exit(1); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +privatestaticvoiddownloadFileFromURL(StringurlString, Filedestination) throwsException{ |
| 99 | +if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null){ |
| 100 | +Stringusername = System.getenv("MVNW_USERNAME"); |
| 101 | +char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); |
| 102 | +Authenticator.setDefault(newAuthenticator(){ |
| 103 | +@Override |
| 104 | +protectedPasswordAuthenticationgetPasswordAuthentication(){ |
| 105 | +returnnewPasswordAuthentication(username, password); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | +URLwebsite = newURL(urlString); |
| 110 | +ReadableByteChannelrbc; |
| 111 | +rbc = Channels.newChannel(website.openStream()); |
| 112 | +FileOutputStreamfos = newFileOutputStream(destination); |
| 113 | +fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); |
| 114 | +fos.close(); |
| 115 | +rbc.close(); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments