@@ -22,30 +22,32 @@ public class MavenWrapperDownloader {
22
22
23
23
private static final String WRAPPER_VERSION = "0.5.6" ;
24
24
/**
25
- * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
25
+ * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl'
26
+ * is provided.
26
27
*/
27
28
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
28
- + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar" ;
29
+ + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar" ;
29
30
30
31
/**
31
- * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
32
- * use instead of the default one.
32
+ * Path to the maven-wrapper.properties file, which might contain a
33
+ * downloadUrl property to use instead of the default one.
33
34
*/
34
- private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
35
- ".mvn/wrapper/maven-wrapper.properties" ;
35
+ private static final String MAVEN_WRAPPER_PROPERTIES_PATH
36
+ = ".mvn/wrapper/maven-wrapper.properties" ;
36
37
37
38
/**
38
39
* Path where the maven-wrapper.jar will be saved to.
39
40
*/
40
- private static final String MAVEN_WRAPPER_JAR_PATH =
41
- ".mvn/wrapper/maven-wrapper.jar" ;
41
+ private static final String MAVEN_WRAPPER_JAR_PATH
42
+ = ".mvn/wrapper/maven-wrapper.jar" ;
42
43
43
44
/**
44
- * Name of the property which should be used to override the default download url for the wrapper.
45
+ * Name of the property which should be used to override the default
46
+ * download url for the wrapper.
45
47
*/
46
48
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl" ;
47
49
48
- public static void main (String args []) {
50
+ public static void main (String args []) throws Exception {
49
51
System .out .println ("- Downloader started" );
50
52
File baseDirectory = new File (args [0 ]);
51
53
System .out .println ("- Using base directory: " + baseDirectory .getAbsolutePath ());
@@ -54,30 +56,34 @@ public static void main(String args[]) {
54
56
// wrapperUrl parameter.
55
57
File mavenWrapperPropertyFile = new File (baseDirectory , MAVEN_WRAPPER_PROPERTIES_PATH );
56
58
String url = DEFAULT_DOWNLOAD_URL ;
57
- if (mavenWrapperPropertyFile .exists ()) {
58
- FileInputStream mavenWrapperPropertyFileInputStream = null ;
59
- try {
60
- mavenWrapperPropertyFileInputStream = new FileInputStream (mavenWrapperPropertyFile );
61
- Properties mavenWrapperProperties = new Properties ();
62
- mavenWrapperProperties .load (mavenWrapperPropertyFileInputStream );
63
- url = mavenWrapperProperties .getProperty (PROPERTY_NAME_WRAPPER_URL , url );
64
- } catch (IOException e ) {
65
- System .out .println ("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'" );
66
- } finally {
59
+ try {
60
+ if (mavenWrapperPropertyFile .exists () && mavenWrapperPropertyFile .getCanonicalPath ().startsWith (baseDirectory .getCanonicalPath ())) {
61
+ FileInputStream mavenWrapperPropertyFileInputStream = null ;
67
62
try {
68
- if (mavenWrapperPropertyFileInputStream != null ) {
69
- mavenWrapperPropertyFileInputStream .close ();
70
- }
63
+ mavenWrapperPropertyFileInputStream = new FileInputStream (mavenWrapperPropertyFile );
64
+ Properties mavenWrapperProperties = new Properties ();
65
+ mavenWrapperProperties .load (mavenWrapperPropertyFileInputStream );
66
+ url = mavenWrapperProperties .getProperty (PROPERTY_NAME_WRAPPER_URL , url );
71
67
} catch (IOException e ) {
72
- // Ignore ...
68
+ System .out .println ("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'" );
69
+ } finally {
70
+ try {
71
+ if (mavenWrapperPropertyFileInputStream != null ) {
72
+ mavenWrapperPropertyFileInputStream .close ();
73
+ }
74
+ } catch (IOException e ) {
75
+ // Ignore ...
76
+ }
73
77
}
74
78
}
79
+ } catch (IOException e ) {
80
+ System .out .println ("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'" );
75
81
}
76
82
System .out .println ("- Downloading from: " + url );
77
83
78
84
File outputFile = new File (baseDirectory .getAbsolutePath (), MAVEN_WRAPPER_JAR_PATH );
79
- if (!outputFile .getParentFile ().exists ()) {
80
- if (!outputFile .getParentFile ().mkdirs ()) {
85
+ if (!outputFile .getParentFile ().exists ()) {
86
+ if (!outputFile .getParentFile ().mkdirs ()) {
81
87
System .out .println (
82
88
"- ERROR creating output directory '" + outputFile .getParentFile ().getAbsolutePath () + "'" );
83
89
}
@@ -87,7 +93,7 @@ public static void main(String args[]) {
87
93
downloadFileFromURL (url , outputFile );
88
94
System .out .println ("Done" );
89
95
System .exit (0 );
90
- } catch (Throwable e ) {
96
+ } catch (IOException e ) {
91
97
System .out .println ("- Error downloading" );
92
98
e .printStackTrace ();
93
99
System .exit (1 );
@@ -108,9 +114,9 @@ protected PasswordAuthentication getPasswordAuthentication() {
108
114
URL website = new URL (urlString );
109
115
ReadableByteChannel rbc ;
110
116
rbc = Channels .newChannel (website .openStream ());
111
- FileOutputStream fos = new FileOutputStream (destination );
112
- fos .getChannel ().transferFrom (rbc , 0 , Long .MAX_VALUE );
113
- fos . close ();
117
+ try ( FileOutputStream fos = new FileOutputStream (destination . getCanonicalPath (). replaceAll ( "^/+" , "" ). split ( " \\ ?" )[ 0 ])) {
118
+ fos .getChannel ().transferFrom (rbc , 0 , Long .MAX_VALUE );
119
+ }
114
120
rbc .close ();
115
121
}
116
122
0 commit comments