You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/07/05 06:00:34 UTC

[maven-wrapper] branch master updated: [MWRAPPER-68] fix issue with MVNW_REPOURL path resolving

This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-wrapper.git


The following commit(s) were added to refs/heads/master by this push:
     new d9f0b1d  [MWRAPPER-68] fix issue with MVNW_REPOURL path resolving
d9f0b1d is described below

commit d9f0b1db825b5facdc65adf1211e9a4a5e472c29
Author: luschmar <90...@users.noreply.github.com>
AuthorDate: Wed Jun 15 13:14:27 2022 +0200

    [MWRAPPER-68] fix issue with MVNW_REPOURL path resolving
---
 .../java/org/apache/maven/wrapper/Installer.java   |  9 ---
 .../org/apache/maven/wrapper/WrapperExecutor.java  | 28 ++++++++
 .../apache/maven/wrapper/WrapperExecutorTest.java  | 75 ++++++++++++++++++++++
 3 files changed, 103 insertions(+), 9 deletions(-)

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 @@ public class Installer
     {
         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.nio.file.Paths;
 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 @@ public class WrapperExecutor
         }
     }
 
+    protected String getEnv( String key )
+    {
+        return System.getenv( key );
+    }
+
     private URI prepareDistributionUri()
         throws URISyntaxException
     {
@@ -105,6 +112,27 @@ public class WrapperExecutor
         }
         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 @@ package org.apache.maven.wrapper;
  * 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.net.URI;
 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 @@ public class WrapperExecutorTest
     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
   {