@@ -22,30 +22,32 @@ public class MavenWrapperDownloader {
2222
2323 private static final String WRAPPER_VERSION = "0.5.6" ;
2424 /**
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.
2627 */
2728 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" ;
2930
3031 /**
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.
3334 */
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" ;
3637
3738 /**
3839 * Path where the maven-wrapper.jar will be saved to.
3940 */
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" ;
4243
4344 /**
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.
4547 */
4648 private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl" ;
4749
48- public static void main (String args []) {
50+ public static void main (String args []) throws Exception {
4951 System .out .println ("- Downloader started" );
5052 File baseDirectory = new File (args [0 ]);
5153 System .out .println ("- Using base directory: " + baseDirectory .getAbsolutePath ());
@@ -54,30 +56,34 @@ public static void main(String args[]) {
5456 // wrapperUrl parameter.
5557 File mavenWrapperPropertyFile = new File (baseDirectory , MAVEN_WRAPPER_PROPERTIES_PATH );
5658 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 ;
6762 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 );
7167 } 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+ }
7377 }
7478 }
79+ } catch (IOException e ) {
80+ System .out .println ("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'" );
7581 }
7682 System .out .println ("- Downloading from: " + url );
7783
7884 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 ()) {
8187 System .out .println (
8288 "- ERROR creating output directory '" + outputFile .getParentFile ().getAbsolutePath () + "'" );
8389 }
@@ -87,7 +93,7 @@ public static void main(String args[]) {
8793 downloadFileFromURL (url , outputFile );
8894 System .out .println ("Done" );
8995 System .exit (0 );
90- } catch (Throwable e ) {
96+ } catch (IOException e ) {
9197 System .out .println ("- Error downloading" );
9298 e .printStackTrace ();
9399 System .exit (1 );
@@ -108,9 +114,9 @@ protected PasswordAuthentication getPasswordAuthentication() {
108114 URL website = new URL (urlString );
109115 ReadableByteChannel rbc ;
110116 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+ }
114120 rbc .close ();
115121 }
116122
0 commit comments