|
| 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 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.net.Authenticator; |
| 23 | +import java.net.PasswordAuthentication; |
| 24 | +import java.net.URL; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.LinkOption; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.nio.file.Paths; |
| 29 | +import java.nio.file.StandardCopyOption; |
| 30 | +import java.nio.file.StandardOpenOption; |
| 31 | +import java.util.Properties; |
| 32 | + |
| 33 | +public final class MavenWrapperDownloader |
| 34 | +{ |
| 35 | + private static final String WRAPPER_VERSION = "3.1.1"; |
| 36 | + |
| 37 | + private static final boolean VERBOSE = Boolean.parseBoolean( System.getenv( "MVNW_VERBOSE" ) ); |
| 38 | + |
| 39 | + /** |
| 40 | + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. |
| 41 | + */ |
| 42 | + private static final String DEFAULT_DOWNLOAD_URL = |
| 43 | + "https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/" + WRAPPER_VERSION |
| 44 | + + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; |
| 45 | + |
| 46 | + /** |
| 47 | + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to use instead of the |
| 48 | + * default one. |
| 49 | + */ |
| 50 | + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = ".mvn/wrapper/maven-wrapper.properties"; |
| 51 | + |
| 52 | + /** |
| 53 | + * Path where the maven-wrapper.jar will be saved to. |
| 54 | + */ |
| 55 | + private static final String MAVEN_WRAPPER_JAR_PATH = ".mvn/wrapper/maven-wrapper.jar"; |
| 56 | + |
| 57 | + /** |
| 58 | + * Name of the property which should be used to override the default download url for the wrapper. |
| 59 | + */ |
| 60 | + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; |
| 61 | + |
| 62 | + public static void main( String[] args ) |
| 63 | + { |
| 64 | + if ( args.length == 0 ) |
| 65 | + { |
| 66 | + System.err.println( " - ERROR projectBasedir parameter missing" ); |
| 67 | + System.exit( 1 ); |
| 68 | + } |
| 69 | + |
| 70 | + log( " - Downloader started" ); |
| 71 | + final String dir = args[0].replace( "..", "" ); // Sanitize path |
| 72 | + final Path projectBasedir = Paths.get( dir ).toAbsolutePath().normalize(); |
| 73 | + if ( !Files.isDirectory( projectBasedir, LinkOption.NOFOLLOW_LINKS ) ) |
| 74 | + { |
| 75 | + System.err.println( " - ERROR projectBasedir not exists: " + projectBasedir ); |
| 76 | + System.exit( 1 ); |
| 77 | + } |
| 78 | + |
| 79 | + log( " - Using base directory: " + projectBasedir ); |
| 80 | + |
| 81 | + // If the maven-wrapper.properties exists, read it and check if it contains a custom |
| 82 | + // wrapperUrl parameter. |
| 83 | + Path mavenWrapperPropertyFile = projectBasedir.resolve( MAVEN_WRAPPER_PROPERTIES_PATH ); |
| 84 | + String url = readWrapperUrl( mavenWrapperPropertyFile ); |
| 85 | + |
| 86 | + try |
| 87 | + { |
| 88 | + Path outputFile = projectBasedir.resolve( MAVEN_WRAPPER_JAR_PATH ); |
| 89 | + createDirectories( outputFile.getParent() ); |
| 90 | + downloadFileFromURL( url, outputFile ); |
| 91 | + log( "Done" ); |
| 92 | + System.exit( 0 ); |
| 93 | + } |
| 94 | + catch ( IOException e ) |
| 95 | + { |
| 96 | + System.err.println( "- Error downloading" ); |
| 97 | + e.printStackTrace(); |
| 98 | + System.exit( 1 ); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static void downloadFileFromURL( String urlString, Path destination ) throws IOException |
| 103 | + { |
| 104 | + log( " - Downloading to: " + destination ); |
| 105 | + if ( System.getenv( "MVNW_USERNAME" ) != null && System.getenv( "MVNW_PASSWORD" ) != null ) |
| 106 | + { |
| 107 | + final String username = System.getenv( "MVNW_USERNAME" ); |
| 108 | + final char[] password = System.getenv( "MVNW_PASSWORD" ).toCharArray(); |
| 109 | + Authenticator.setDefault( new Authenticator() |
| 110 | + { |
| 111 | + @Override |
| 112 | + protected PasswordAuthentication getPasswordAuthentication() |
| 113 | + { |
| 114 | + return new PasswordAuthentication( username, password ); |
| 115 | + } |
| 116 | + } ); |
| 117 | + } |
| 118 | + URL website = new URL( urlString ); |
| 119 | + try ( InputStream inStream = website.openStream() ) { |
| 120 | + Files.copy( inStream, destination, StandardCopyOption.REPLACE_EXISTING ); |
| 121 | + } |
| 122 | + log( " - Downloader complete" ); |
| 123 | + } |
| 124 | + |
| 125 | + private static void createDirectories(Path outputPath) throws IOException |
| 126 | + { |
| 127 | + if ( !Files.isDirectory( outputPath, LinkOption.NOFOLLOW_LINKS ) ) { |
| 128 | + Path createDirectories = Files.createDirectories( outputPath ); |
| 129 | + log( " - Directories created: " + createDirectories ); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static String readWrapperUrl( Path mavenWrapperPropertyFile ) |
| 134 | + { |
| 135 | + String url = DEFAULT_DOWNLOAD_URL; |
| 136 | + if ( Files.exists( mavenWrapperPropertyFile, LinkOption.NOFOLLOW_LINKS ) ) |
| 137 | + { |
| 138 | + log( " - Reading property file: " + mavenWrapperPropertyFile ); |
| 139 | + try ( InputStream in = Files.newInputStream( mavenWrapperPropertyFile, StandardOpenOption.READ ) ) |
| 140 | + { |
| 141 | + Properties mavenWrapperProperties = new Properties(); |
| 142 | + mavenWrapperProperties.load( in ); |
| 143 | + url = mavenWrapperProperties.getProperty( PROPERTY_NAME_WRAPPER_URL, DEFAULT_DOWNLOAD_URL ); |
| 144 | + } |
| 145 | + catch ( IOException e ) |
| 146 | + { |
| 147 | + System.err.println( " - ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'" ); |
| 148 | + } |
| 149 | + } |
| 150 | + log( " - Downloading from: " + url ); |
| 151 | + return url; |
| 152 | + } |
| 153 | + |
| 154 | + private static void log( String msg ) |
| 155 | + { |
| 156 | + if ( VERBOSE ) |
| 157 | + { |
| 158 | + System.out.println( msg ); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments