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