17
17
* under the License.
18
18
*/
19
19
20
- import java .net .*;
21
- import java .io .*;
22
- import java .nio .channels .*;
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 ;
23
31
import java .util .Properties ;
24
32
25
- public class MavenWrapperDownloader
33
+ public final class MavenWrapperDownloader
26
34
{
27
- private static final String WRAPPER_VERSION = "3.1.0" ;
35
+ private static final String WRAPPER_VERSION = "3.1.1" ;
36
+
37
+ private static final boolean VERBOSE = Boolean .parseBoolean ( System .getenv ( "MVNW_VERBOSE" ) );
28
38
29
39
/**
30
40
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
@@ -49,78 +59,53 @@ public class MavenWrapperDownloader
49
59
*/
50
60
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl" ;
51
61
52
- public static void main ( String args [] )
62
+ public static void main ( String [] args )
53
63
{
54
- System .out .println ( "- Downloader started" );
55
- File baseDirectory = new File ( args [0 ] );
56
- System .out .println ( "- Using base directory: " + baseDirectory .getAbsolutePath () );
57
-
58
- // If the maven-wrapper.properties exists, read it and check if it contains a custom
59
- // wrapperUrl parameter.
60
- File mavenWrapperPropertyFile = new File ( baseDirectory , MAVEN_WRAPPER_PROPERTIES_PATH );
61
- String url = DEFAULT_DOWNLOAD_URL ;
62
- if ( mavenWrapperPropertyFile .exists () )
64
+ if ( args .length == 0 )
63
65
{
64
- FileInputStream mavenWrapperPropertyFileInputStream = null ;
65
- try
66
- {
67
- mavenWrapperPropertyFileInputStream = new FileInputStream ( mavenWrapperPropertyFile );
68
- Properties mavenWrapperProperties = new Properties ();
69
- mavenWrapperProperties .load ( mavenWrapperPropertyFileInputStream );
70
- url = mavenWrapperProperties .getProperty ( PROPERTY_NAME_WRAPPER_URL , url );
71
- }
72
- catch ( IOException e )
73
- {
74
- System .out .println ( "- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'" );
75
- }
76
- finally
77
- {
78
- try
79
- {
80
- if ( mavenWrapperPropertyFileInputStream != null )
81
- {
82
- mavenWrapperPropertyFileInputStream .close ();
83
- }
84
- }
85
- catch ( IOException e )
86
- {
87
- // Ignore ...
88
- }
89
- }
66
+ System .err .println ( " - ERROR projectBasedir parameter missing" );
67
+ System .exit ( 1 );
90
68
}
91
- System .out .println ( "- Downloading from: " + url );
92
69
93
- File outputFile = new File ( baseDirectory .getAbsolutePath (), MAVEN_WRAPPER_JAR_PATH );
94
- if ( !outputFile .getParentFile ().exists () )
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 ) )
95
74
{
96
- if ( !outputFile .getParentFile ().mkdirs () )
97
- {
98
- System .out .println ( "- ERROR creating output directory '" + outputFile .getParentFile ().getAbsolutePath ()
99
- + "'" );
100
- }
75
+ System .err .println ( " - ERROR projectBasedir not exists: " + projectBasedir );
76
+ System .exit ( 1 );
101
77
}
102
- System .out .println ( "- Downloading to: " + outputFile .getAbsolutePath () );
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
+
103
86
try
104
87
{
88
+ Path outputFile = projectBasedir .resolve ( MAVEN_WRAPPER_JAR_PATH );
89
+ createDirectories ( outputFile .getParent () );
105
90
downloadFileFromURL ( url , outputFile );
106
- System . out . println ( "Done" );
91
+ log ( "Done" );
107
92
System .exit ( 0 );
108
93
}
109
- catch ( Throwable e )
94
+ catch ( IOException e )
110
95
{
111
- System .out .println ( "- Error downloading" );
96
+ System .err .println ( "- Error downloading" );
112
97
e .printStackTrace ();
113
98
System .exit ( 1 );
114
99
}
115
100
}
116
101
117
- private static void downloadFileFromURL ( String urlString , File destination )
118
- throws Exception
102
+ private static void downloadFileFromURL ( String urlString , Path destination ) throws IOException
119
103
{
104
+ log ( " - Downloading to: " + destination );
120
105
if ( System .getenv ( "MVNW_USERNAME" ) != null && System .getenv ( "MVNW_PASSWORD" ) != null )
121
106
{
122
- String username = System .getenv ( "MVNW_USERNAME" );
123
- char [] password = System .getenv ( "MVNW_PASSWORD" ).toCharArray ();
107
+ final String username = System .getenv ( "MVNW_USERNAME" );
108
+ final char [] password = System .getenv ( "MVNW_PASSWORD" ).toCharArray ();
124
109
Authenticator .setDefault ( new Authenticator ()
125
110
{
126
111
@ Override
@@ -131,12 +116,47 @@ protected PasswordAuthentication getPasswordAuthentication()
131
116
} );
132
117
}
133
118
URL website = new URL ( urlString );
134
- ReadableByteChannel rbc ;
135
- rbc = Channels .newChannel ( website .openStream () );
136
- FileOutputStream fos = new FileOutputStream ( destination );
137
- fos .getChannel ().transferFrom ( rbc , 0 , Long .MAX_VALUE );
138
- fos .close ();
139
- rbc .close ();
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
+ }
140
160
}
141
161
142
162
}
0 commit comments