001 | /* |
002 | * Copyright 2007-present the original author or authors. |
003 | * |
004 | * Licensed under the Apache License, Version 2.0 (the "License"); |
005 | * you may not use this file except in compliance with the License. |
006 | * You may obtain a copy of the License at |
007 | * |
009 | * |
010 | * Unless required by applicable law or agreed to in writing, software |
011 | * distributed under the License is distributed on an "AS IS" BASIS, |
012 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
013 | * See the License for the specific language governing permissions and |
014 | * limitations under the License. |
015 | */ |
016 |
017 | import java.net.*; |
018 | import java.io.*; |
019 | import java.nio.channels.*; |
020 | import java.util.Properties; |
021 |
022 | public class MavenWrapperDownloader { |
023 |
024 | private static final String WRAPPER_VERSION = "0.5.6"; |
025 | /** |
026 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. |
027 | */ |
028 | private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" |
029 | + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; |
030 |
031 | /** |
032 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to |
033 | * use instead of the default one. |
034 | */ |
035 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = |
036 | ".mvn/wrapper/maven-wrapper.properties"; |
037 |
038 | /** |
039 | * Path where the maven-wrapper.jar will be saved to. |
040 | */ |
041 | private static final String MAVEN_WRAPPER_JAR_PATH = |
042 | ".mvn/wrapper/maven-wrapper.jar"; |
043 |
044 | /** |
045 | * Name of the property which should be used to override the default download url for the wrapper. |
046 | */ |
047 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl" ; |
048 |
049 | public static void main(String args[]) { |
050 | System.out.println( "- Downloader started" ); |
051 | File baseDirectory = new File(args[ 0 ]); |
052 | System.out.println( "- Using base directory: " + baseDirectory.getAbsolutePath()); |
053 |
054 | // If the maven-wrapper.properties exists, read it and check if it contains a custom |
055 | // wrapperUrl parameter. |
056 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); |
057 | String url = DEFAULT_DOWNLOAD_URL; |
058 | if (mavenWrapperPropertyFile.exists()) { |
059 | FileInputStream mavenWrapperPropertyFileInputStream = null ; |
060 | try { |
061 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); |
062 | Properties mavenWrapperProperties = new Properties(); |
063 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); |
064 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); |
065 | } catch (IOException e) { |
066 | System.out.println( "- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'" ); |
067 | } finally { |
068 | try { |
069 | if (mavenWrapperPropertyFileInputStream != null ) { |
070 | mavenWrapperPropertyFileInputStream.close(); |
071 | } |
072 | } catch (IOException e) { |
073 | // Ignore ... |
074 | } |
075 | } |
076 | } |
077 | System.out.println( "- Downloading from: " + url); |
078 |
079 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); |
080 | if (!outputFile.getParentFile().exists()) { |
081 | if (!outputFile.getParentFile().mkdirs()) { |
082 | System.out.println( |
083 | "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'" ); |
084 | } |
085 | } |
086 | System.out.println( "- Downloading to: " + outputFile.getAbsolutePath()); |
087 | try { |
088 | downloadFileFromURL(url, outputFile); |
089 | System.out.println( "Done" ); |
090 | System.exit( 0 ); |
091 | } catch (Throwable e) { |
092 | System.out.println( "- Error downloading" ); |
093 | e.printStackTrace(); |
094 | System.exit( 1 ); |
095 | } |
096 | } |
097 |
098 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { |
099 | if (System.getenv( "MVNW_USERNAME" ) != null && System.getenv( "MVNW_PASSWORD" ) != null ) { |
100 | String username = System.getenv( "MVNW_USERNAME" ); |
101 | char [] password = System.getenv( "MVNW_PASSWORD" ).toCharArray(); |
102 | Authenticator.setDefault( new Authenticator() { |
103 | @Override |
104 | protected PasswordAuthentication getPasswordAuthentication() { |
105 | return new PasswordAuthentication(username, password); |
106 | } |
107 | }); |
108 | } |
109 | URL website = new URL(urlString); |
110 | ReadableByteChannel rbc; |
111 | rbc = Channels.newChannel(website.openStream()); |
112 | FileOutputStream fos = new FileOutputStream(destination); |
113 | fos.getChannel().transferFrom(rbc, 0 , Long.MAX_VALUE); |
114 | fos.close(); |
115 | rbc.close(); |
116 | } |
117 |
118 | } |

大神程序员 LV23
7月10日
微信网友_6368711690080256 LV3
2023年2月28日
0592lyj LV9
2023年2月16日
906933050 LV1
2022年10月9日
zhulei886 LV14
2021年5月19日
xhmpmail LV17
2021年2月22日
lincolnpan LV9
2021年2月2日
diadream LV3
2021年1月18日
最代码官方 LV168
2021年1月10日