23
23
import java .net .PasswordAuthentication ;
24
24
import java .net .URL ;
25
25
import java .nio .file .Files ;
26
- import java .nio .file .LinkOption ;
27
26
import java .nio .file .Path ;
28
27
import java .nio .file .Paths ;
29
28
import java .nio .file .StandardCopyOption ;
30
- import java .nio .file .StandardOpenOption ;
31
- import java .util .Properties ;
32
29
33
30
public final class MavenWrapperDownloader
34
31
{
35
- private static final String WRAPPER_VERSION = "3.1.1 " ;
32
+ private static final String WRAPPER_VERSION = "3.2.0 " ;
36
33
37
34
private static final boolean VERBOSE = Boolean .parseBoolean ( System .getenv ( "MVNW_VERBOSE" ) );
38
35
39
- /**
40
- * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
41
- */
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" ;
45
-
46
- /**
47
- * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to use instead of the
48
- * default one.
49
- */
50
- private static final String MAVEN_WRAPPER_PROPERTIES_PATH = ".mvn/wrapper/maven-wrapper.properties" ;
51
-
52
- /**
53
- * Path where the maven-wrapper.jar will be saved to.
54
- */
55
- private static final String MAVEN_WRAPPER_JAR_PATH = ".mvn/wrapper/maven-wrapper.jar" ;
56
-
57
- /**
58
- * Name of the property which should be used to override the default download url for the wrapper.
59
- */
60
- private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl" ;
61
-
62
36
public static void main ( String [] args )
63
37
{
64
- if ( args .length == 0 )
65
- {
66
- System .err .println ( " - ERROR projectBasedir parameter missing" );
67
- System .exit ( 1 );
68
- }
38
+ log ( "Apache Maven Wrapper Downloader " + WRAPPER_VERSION );
69
39
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 ) )
40
+ if ( args .length != 2 )
74
41
{
75
- System .err .println ( " - ERROR projectBasedir not exists: " + projectBasedir );
42
+ System .err .println ( " - ERROR wrapperUrl or wrapperJarPath parameter missing" );
76
43
System .exit ( 1 );
77
44
}
78
45
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
-
86
46
try
87
47
{
88
- Path outputFile = projectBasedir .resolve ( MAVEN_WRAPPER_JAR_PATH );
89
- createDirectories ( outputFile .getParent () );
90
- downloadFileFromURL ( url , outputFile );
48
+ log ( " - Downloader started" );
49
+ final URL wrapperUrl = new URL ( args [0 ] );
50
+ final String jarPath = args [1 ].replace ( ".." , "" ); // Sanitize path
51
+ final Path wrapperJarPath = Paths .get ( jarPath ).toAbsolutePath ().normalize ();
52
+ downloadFileFromURL ( wrapperUrl , wrapperJarPath );
91
53
log ( "Done" );
92
- System .exit ( 0 );
93
54
}
94
55
catch ( IOException e )
95
56
{
96
- System .err .println ( "- Error downloading" );
97
- e .printStackTrace ();
57
+ System .err .println ( "- Error downloading: " + e .getMessage () );
58
+ if ( VERBOSE )
59
+ {
60
+ e .printStackTrace ();
61
+ }
98
62
System .exit ( 1 );
99
63
}
100
64
}
101
65
102
- private static void downloadFileFromURL ( String urlString , Path destination ) throws IOException
66
+ private static void downloadFileFromURL ( URL wrapperUrl , Path wrapperJarPath )
67
+ throws IOException
103
68
{
104
- log ( " - Downloading to: " + destination );
69
+ log ( " - Downloading to: " + wrapperJarPath );
105
70
if ( System .getenv ( "MVNW_USERNAME" ) != null && System .getenv ( "MVNW_PASSWORD" ) != null )
106
71
{
107
72
final String username = System .getenv ( "MVNW_USERNAME" );
@@ -115,40 +80,11 @@ protected PasswordAuthentication getPasswordAuthentication()
115
80
}
116
81
} );
117
82
}
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 ) )
83
+ try ( InputStream inStream = wrapperUrl .openStream () )
137
84
{
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
- }
85
+ Files .copy ( inStream , wrapperJarPath , StandardCopyOption .REPLACE_EXISTING );
149
86
}
150
- log ( " - Downloading from: " + url );
151
- return url ;
87
+ log ( " - Downloader complete" );
152
88
}
153
89
154
90
private static void log ( String msg )
0 commit comments