[MWRAPPER-68] fix issue with MVNW_REPOURL path resolving
diff --git a/maven-wrapper/src/main/java/org/apache/maven/wrapper/Installer.java b/maven-wrapper/src/main/java/org/apache/maven/wrapper/Installer.java
index 48340ee..29b63e3 100644
--- a/maven-wrapper/src/main/java/org/apache/maven/wrapper/Installer.java
+++ b/maven-wrapper/src/main/java/org/apache/maven/wrapper/Installer.java
@@ -65,15 +65,6 @@
     {
         URI distributionUrl = configuration.getDistribution();
 
-        String mvnwRepoUrl = System.getenv( MavenWrapperMain.MVNW_REPOURL );
-        if ( mvnwRepoUrl != null && !mvnwRepoUrl.isEmpty() )
-        {
-            Logger.info( "Detected MVNW_REPOURL environment variable " + mvnwRepoUrl );
-            String mvnPath = distributionUrl.toURL().toString();
-            mvnPath = mvnPath.substring( mvnPath.indexOf( "org/apache/maven" ) );
-            distributionUrl = new URI( mvnwRepoUrl ).resolve( "/" ).resolve( mvnPath );
-        }
-
         boolean alwaysDownload = configuration.isAlwaysDownload();
         boolean alwaysUnpack = configuration.isAlwaysUnpack();
 
diff --git a/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java b/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
index f382b84..f69d7cc 100644
--- a/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
+++ b/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
@@ -29,6 +29,8 @@
 import java.util.Locale;
 import java.util.Properties;
 
+import static org.apache.maven.wrapper.MavenWrapperMain.MVNW_REPOURL;
+
 /**
  * Wrapper executor, running {@link Installer} to get a Maven distribution ready, followed by
  * {@link BootstrapMainStarter} to launch the Maven bootstrap.
@@ -94,6 +96,11 @@
         }
     }
 
+    protected String getEnv( String key )
+    {
+        return System.getenv( key );
+    }
+
     private URI prepareDistributionUri()
         throws URISyntaxException
     {
@@ -105,6 +112,27 @@
         }
         else
         {
+            String mvnwRepoUrl = getEnv( MVNW_REPOURL );
+            if ( mvnwRepoUrl != null && !mvnwRepoUrl.isEmpty() )
+            {
+                Logger.info( "Detected MVNW_REPOURL environment variable " + mvnwRepoUrl );
+                if ( mvnwRepoUrl.endsWith( "/" ) )
+                {
+                    mvnwRepoUrl = mvnwRepoUrl.substring( 0, mvnwRepoUrl.length() - 1 );
+                }
+                String distributionPath = source.getPath();
+                int index = distributionPath.indexOf( "org/apache/maven" );
+                if ( index > 1 )
+                {
+                    distributionPath = "/".concat( distributionPath.substring( index ) );
+                }
+                else
+                {
+                    Logger.warn( "distributionUrl don't contain package name " + source.getPath() );
+                }
+                return new URI( mvnwRepoUrl + distributionPath );
+            }
+
             return source;
         }
     }
diff --git a/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java b/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
index 368f3a0..fb91f2d 100644
--- a/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
+++ b/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
@@ -19,6 +19,7 @@
  * under the License.
  */
 
+import static org.apache.maven.wrapper.MavenWrapperMain.MVNW_REPOURL;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -28,6 +29,8 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
 
 import org.junit.Assert;
@@ -191,6 +194,78 @@
     Assert.assertTrue( wrapper.getDistribution().getSchemeSpecificPart().endsWith( "some/relative/url/to/bin.zip" ) );
   }
 
+  @Test
+  public void testEnvironmentVariableOverwrite_simpleCase()
+    throws Exception
+  {
+    final Map<String, String> environmentVariables = new HashMap<>();
+    environmentVariables.put( MVNW_REPOURL, "https://repo/test" );
+
+    properties = new Properties();
+    properties.put( "distributionUrl", "https://server/path/to/bin.zip" );
+    writePropertiesFile( properties, propertiesFile, "header" );
+
+    WrapperExecutor wrapper = prepareWrapperExecutorWithEnvironmentVariables(environmentVariables);
+
+    Assert.assertEquals( "https://repo/test/path/to/bin.zip", wrapper.getDistribution().toString() );
+  }
+
+  @Test
+  public void testEnvironmentVariableOverwrite_mvnwRepoUrl_trailingSlash()
+          throws Exception
+  {
+    final Map<String, String> environmentVariables = new HashMap<>();
+    environmentVariables.put( MVNW_REPOURL, "https://repo/test/" );
+    properties = new Properties();
+    properties.put( "distributionUrl", "https://server/path/to/bin.zip" );
+    writePropertiesFile( properties, propertiesFile, "header" );
+
+    WrapperExecutor wrapper = prepareWrapperExecutorWithEnvironmentVariables(environmentVariables);
+
+    Assert.assertEquals( "https://repo/test/path/to/bin.zip", wrapper.getDistribution().toString() );
+  }
+
+  @Test
+  public void testEnvironmentVariableOverwrite_packageName()
+          throws Exception
+  {
+    final Map<String, String> environmentVariables = new HashMap<>();
+    environmentVariables.put( MVNW_REPOURL, "https://repo/test" );
+    properties = new Properties();
+    properties.put( "distributionUrl", "https://server/org/apache/maven/to/bin.zip" );
+    writePropertiesFile( properties, propertiesFile, "header" );
+
+    WrapperExecutor wrapper = prepareWrapperExecutorWithEnvironmentVariables(environmentVariables);
+
+    Assert.assertEquals( "https://repo/test/org/apache/maven/to/bin.zip", wrapper.getDistribution().toString() );
+  }
+
+  @Test
+  public void testEnvironmentVariableOverwrite_packageName_trailingSpace()
+          throws Exception
+  {
+    final Map<String, String> environmentVariables = new HashMap<>();
+    environmentVariables.put( MVNW_REPOURL, "https://repo/test/" );
+    properties = new Properties();
+    properties.put( "distributionUrl", "https://server/whatever/org/apache/maven/to/bin.zip" );
+    writePropertiesFile( properties, propertiesFile, "header" );
+
+    WrapperExecutor wrapper = prepareWrapperExecutorWithEnvironmentVariables(environmentVariables);
+
+    Assert.assertEquals( "https://repo/test/org/apache/maven/to/bin.zip", wrapper.getDistribution().toString() );
+  }
+
+  private WrapperExecutor prepareWrapperExecutorWithEnvironmentVariables(final Map<String, String> environmentVariables )
+  {
+    return new WrapperExecutor( propertiesFile, new Properties() ) {
+      @Override
+      protected String getEnv( String key )
+      {
+        return environmentVariables.get( key );
+      }
+    };
+  }
+
   private void writePropertiesFile( Properties properties, Path propertiesFile, String message )
     throws Exception
   {