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
10 https://www.apache.org/licenses/LICENSE-2.0
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
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
25 import java.nio.channels.Channels;
26 import java.nio.channels.ReadableByteChannel;
27 import java.util.Properties;
29 public class MavenWrapperDownloader {
32 * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
34 private static final String DEFAULT_DOWNLOAD_URL =
35 "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
38 * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
39 * use instead of the default one.
41 private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
42 ".mvn/wrapper/maven-wrapper.properties";
45 * Path where the maven-wrapper.jar will be saved to.
47 private static final String MAVEN_WRAPPER_JAR_PATH =
48 ".mvn/wrapper/maven-wrapper.jar";
51 * Name of the property which should be used to override the default download url for the wrapper.
53 private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
55 public static void main(String args[]) {
56 System.out.println("- Downloader started");
57 File baseDirectory = new File(args[0]);
58 System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
60 // If the maven-wrapper.properties exists, read it and check if it contains a custom
61 // wrapperUrl parameter.
62 File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
63 String url = DEFAULT_DOWNLOAD_URL;
64 if(mavenWrapperPropertyFile.exists()) {
65 FileInputStream mavenWrapperPropertyFileInputStream = null;
67 mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
68 Properties mavenWrapperProperties = new Properties();
69 mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
70 url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
71 } catch (IOException e) {
72 System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
75 if(mavenWrapperPropertyFileInputStream != null) {
76 mavenWrapperPropertyFileInputStream.close();
78 } catch (IOException e) {
83 System.out.println("- Downloading from: : " + url);
85 File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
86 if(!outputFile.getParentFile().exists()) {
87 if(!outputFile.getParentFile().mkdirs()) {
89 "- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
92 System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
94 downloadFileFromURL(url, outputFile);
95 System.out.println("Done");
97 } catch (Throwable e) {
98 System.out.println("- Error downloading");
104 private static void downloadFileFromURL(String urlString, File destination) throws Exception {
105 URL website = new URL(urlString);
106 ReadableByteChannel rbc;
107 rbc = Channels.newChannel(website.openStream());
108 FileOutputStream fos = new FileOutputStream(destination);
109 fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);