123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import java.net.*;
- import java.io.*;
- import java.nio.channels.*;
- import java.util.Properties;
- public class MavenWrapperDownloader
- {
- private static final String WRAPPER_VERSION = "3.1.1";
-
- private static final String DEFAULT_DOWNLOAD_URL =
- "https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/" + WRAPPER_VERSION
- + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
-
- private static final String MAVEN_WRAPPER_PROPERTIES_PATH = ".mvn/wrapper/maven-wrapper.properties";
-
- private static final String MAVEN_WRAPPER_JAR_PATH = ".mvn/wrapper/maven-wrapper.jar";
-
- private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
- public static void main( String args[] )
- {
- System.out.println( "- Downloader started" );
- File baseDirectory = new File( args[0] );
- System.out.println( "- Using base directory: " + baseDirectory.getAbsolutePath() );
-
-
- File mavenWrapperPropertyFile = new File( baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH );
- String url = DEFAULT_DOWNLOAD_URL;
- if ( mavenWrapperPropertyFile.exists() )
- {
- FileInputStream mavenWrapperPropertyFileInputStream = null;
- try
- {
- mavenWrapperPropertyFileInputStream = new FileInputStream( mavenWrapperPropertyFile );
- Properties mavenWrapperProperties = new Properties();
- mavenWrapperProperties.load( mavenWrapperPropertyFileInputStream );
- url = mavenWrapperProperties.getProperty( PROPERTY_NAME_WRAPPER_URL, url );
- }
- catch ( IOException e )
- {
- System.out.println( "- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'" );
- }
- finally
- {
- try
- {
- if ( mavenWrapperPropertyFileInputStream != null )
- {
- mavenWrapperPropertyFileInputStream.close();
- }
- }
- catch ( IOException e )
- {
-
- }
- }
- }
- System.out.println( "- Downloading from: " + url );
- File outputFile = new File( baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH );
- if ( !outputFile.getParentFile().exists() )
- {
- if ( !outputFile.getParentFile().mkdirs() )
- {
- System.out.println( "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath()
- + "'" );
- }
- }
- System.out.println( "- Downloading to: " + outputFile.getAbsolutePath() );
- try
- {
- downloadFileFromURL( url, outputFile );
- System.out.println( "Done" );
- System.exit( 0 );
- }
- catch ( Throwable e )
- {
- System.out.println( "- Error downloading" );
- e.printStackTrace();
- System.exit( 1 );
- }
- }
- private static void downloadFileFromURL( String urlString, File destination )
- throws Exception
- {
- if ( System.getenv( "MVNW_USERNAME" ) != null && System.getenv( "MVNW_PASSWORD" ) != null )
- {
- String username = System.getenv( "MVNW_USERNAME" );
- char[] password = System.getenv( "MVNW_PASSWORD" ).toCharArray();
- Authenticator.setDefault( new Authenticator()
- {
- @Override
- protected PasswordAuthentication getPasswordAuthentication()
- {
- return new PasswordAuthentication( username, password );
- }
- } );
- }
- URL website = new URL( urlString );
- ReadableByteChannel rbc;
- rbc = Channels.newChannel( website.openStream() );
- FileOutputStream fos = new FileOutputStream( destination );
- fos.getChannel().transferFrom( rbc, 0, Long.MAX_VALUE );
- fos.close();
- rbc.close();
- }
- }
|