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

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

commit b54a8017a75cc9b72b7dfdceab017a4a40d520cf
Author: Slawomir Jaranowski <s.jaranow...@gmail.com>
AuthorDate: Thu Dec 30 01:10:18 2021 +0100

    [MSHARED-577] Remove usage of M2_HOME environment variable
---
 .../maven/shared/invoker/InvocationRequest.java    |  2 +-
 .../org/apache/maven/shared/invoker/Invoker.java   |  2 +-
 .../shared/invoker/MavenCommandLineBuilder.java    | 45 +----------
 src/site/apt/usage.apt                             |  4 +-
 .../maven/shared/invoker/DefaultInvokerTest.java   | 14 +---
 .../invoker/MavenCommandLineBuilderTest.java       | 86 ++--------------------
 6 files changed, 18 insertions(+), 135 deletions(-)

diff --git 
a/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java 
b/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java
index fd603a2..3db994d 100644
--- a/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java
+++ b/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java
@@ -776,7 +776,7 @@ public interface InvocationRequest
     /**
      * Sets the path to the base directory of the Maven installation used to 
invoke Maven. This parameter may be left
      * unspecified to use the default Maven installation which will be 
discovered by evaluating the system property
-     * <code>maven.home</code> and the environment variable 
<code>M2_HOME</code>.
+     * <code>maven.home</code>.
      *
      * @param mavenHome The path to the base directory of the Maven 
installation, may be <code>null</code> to use the
      *            default Maven installation.
diff --git a/src/main/java/org/apache/maven/shared/invoker/Invoker.java 
b/src/main/java/org/apache/maven/shared/invoker/Invoker.java
index 869d29e..4349a0a 100644
--- a/src/main/java/org/apache/maven/shared/invoker/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/invoker/Invoker.java
@@ -80,7 +80,7 @@ public interface Invoker
     /**
      * Sets the path to the base directory of the Maven installation used to 
invoke Maven. This parameter may be left
      * unspecified to use the default Maven installation which will be 
discovered by evaluating the system property
-     * <code>maven.home</code> and the environment variable 
<code>M2_HOME</code>.
+     * <code>maven.home</code>.
      *
      * @param mavenHome The path to the base directory of the Maven 
installation, may be <code>null</code> to use the
      *            default Maven installation.
diff --git 
a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java 
b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
index 5767a4b..186da75 100644
--- a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
+++ b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
@@ -31,7 +31,6 @@ import 
org.apache.maven.shared.invoker.InvocationRequest.ReactorFailureBehavior;
 import org.apache.maven.shared.utils.Os;
 import org.apache.maven.shared.utils.StringUtils;
 import org.apache.maven.shared.utils.cli.CommandLineException;
-import org.apache.maven.shared.utils.cli.CommandLineUtils;
 import org.apache.maven.shared.utils.cli.Commandline;
 
 /**
@@ -52,8 +51,6 @@ public class MavenCommandLineBuilder
 
     private File mavenExecutable;
 
-    private Properties systemEnvVars;
-
     /**
      * <p>build.</p>
      *
@@ -207,23 +204,7 @@ public class MavenCommandLineBuilder
     {
         if ( request.isShellEnvironmentInherited() )
         {
-            try
-            {
-                cli.addSystemEnvironment();
-                cli.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
-                // MSHARED-261: Ensure M2_HOME is not inherited, but gets a
-                // proper value
-                cli.addEnvironment( "M2_HOME", 
getMavenHome().getAbsolutePath() );
-            }
-            catch ( RuntimeException e )
-            {
-                throw e;
-            }
-            catch ( Exception e )
-            {
-                throw new IllegalStateException(
-                        "Unknown error retrieving shell environment variables. 
Reason: " + e.getMessage(), e );
-            }
+            cli.addSystemEnvironment();
         }
 
         if ( request.getJavaHome() != null )
@@ -585,19 +566,9 @@ public class MavenCommandLineBuilder
         {
             mavenHome = request.getMavenHome();
         }
-
-        if ( mavenHome == null )
+        else if ( System.getProperty( "maven.home" ) != null )
         {
-            String mavenHomeProperty = System.getProperty( "maven.home" );
-            if ( mavenHomeProperty == null && getSystemEnvVars().getProperty( 
"M2_HOME" ) != null )
-            {
-                mavenHomeProperty = getSystemEnvVars().getProperty( "M2_HOME" 
);
-            }
-
-            if ( mavenHomeProperty != null )
-            {
-                mavenHome = new File( mavenHomeProperty );
-            }
+            mavenHome = new File( System.getProperty( "maven.home" ) );
         }
 
         if ( mavenHome != null && !mavenHome.isDirectory() )
@@ -676,16 +647,6 @@ public class MavenCommandLineBuilder
         }
     }
 
-    private Properties getSystemEnvVars()
-    {
-        if ( this.systemEnvVars == null )
-        {
-            // with 1.5 replace with System.getenv()
-            this.systemEnvVars = CommandLineUtils.getSystemEnvVars();
-        }
-        return this.systemEnvVars;
-    }
-
     /**
      * <p>Getter for the field <code>localRepositoryDirectory</code>.</p>
      *
diff --git a/src/site/apt/usage.apt b/src/site/apt/usage.apt
index 44d0cb3..30cc7a1 100644
--- a/src/site/apt/usage.apt
+++ b/src/site/apt/usage.apt
@@ -67,7 +67,7 @@ if ( result.getExitCode() != 0 )
   This will retrieve the exit code from the invocation result, and throw
   an exception if it's not <<<0>>> (the traditional all-clear code). Note that
   we could capture the build output by adding an <<<InvocationOutputHandler>>>
-  instance to either the <<<invoker>>> or the <<<request>>>.
+  instance to the <<<request>>>.
   
 * Caching the Invoker
 
@@ -127,7 +127,7 @@ public void publishSite( File siteDirectory ) throws 
PublishException
 
   You can use the method <<<Invoker.setMavenHome()>>> to specify which Maven 
executable it should use.
   If you don't provide an explicit value for this setting, the <<<Invoker>>> 
will automatically try to detect
-  a Maven installation by evaluating the system property <<<maven.home>>> and 
the environment variable <<<M2_HOME>>>.
+  a Maven installation by evaluating the system property <<<maven.home>>>.
 
   <<Note:>> If you use the invocation API in tests run by the 
{{{../../plugins/maven-surefire-plugin}Maven Surefire Plugin}},
   you need to tell Surefire to pass the system property <<<maven.home>>> to 
the tests in order for the automatic Maven
diff --git 
a/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java 
b/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java
index a5aae00..70a4d6d 100644
--- a/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java
+++ b/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java
@@ -29,7 +29,6 @@ import java.util.Collections;
 import java.util.Properties;
 
 import org.apache.maven.shared.utils.StringUtils;
-import org.apache.maven.shared.utils.cli.CommandLineUtils;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -39,7 +38,7 @@ public class DefaultInvokerTest
 
     @Test
     public void testBuildShouldSucceed()
-        throws IOException, MavenInvocationException, URISyntaxException
+        throws MavenInvocationException, URISyntaxException
     {
         File basedir = getBasedirForBuild();
 
@@ -58,7 +57,7 @@ public class DefaultInvokerTest
 
     @Test
     public void testBuildShouldFail()
-        throws IOException, MavenInvocationException, URISyntaxException
+        throws MavenInvocationException, URISyntaxException
     {
         File basedir = getBasedirForBuild();
 
@@ -77,7 +76,7 @@ public class DefaultInvokerTest
 
     @Test
     public void testBuildShouldTimeout()
-        throws IOException, MavenInvocationException, URISyntaxException
+        throws MavenInvocationException, URISyntaxException
     {
         File basedir = getBasedirForBuild();
 
@@ -239,13 +238,8 @@ public class DefaultInvokerTest
 
         if ( mavenHome == null )
         {
-            mavenHome = CommandLineUtils.getSystemEnvVars().getProperty( 
"M2_HOME" );
-        }
-
-        if ( mavenHome == null )
-        {
             throw new IllegalStateException( "Cannot find Maven application "
-                + "directory. Either specify 'maven.home' system property, or 
M2_HOME environment variable." );
+                + "directory. Specify 'maven.home' system property" );
         }
 
         return new File( mavenHome );
diff --git 
a/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
 
b/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
index f7b2b0c..9e96652 100644
--- 
a/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
+++ 
b/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
@@ -45,6 +45,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeThat;
 import static org.junit.Assume.assumeTrue;
 
 public class MavenCommandLineBuilderTest
@@ -796,41 +797,6 @@ public class MavenCommandLineBuilderTest
     }
 
     @Test
-    public void testShouldSetEnvVar_MAVEN_TERMINATE_CMD()
-        throws Exception
-    {
-        setupTempMavenHomeIfMissing( false );
-
-        InvocationRequest request = newRequest();
-
-        File projectDir = temporaryFolder.newFolder( "invoker-tests", 
"maven-terminate-cmd-options-set" );
-
-        request.setBaseDirectory( projectDir );
-
-        createDummyFile( projectDir, "pom.xml" );
-
-        List<String> goals = new ArrayList<>();
-
-        goals.add( "clean" );
-        request.setGoals( goals );
-
-        Commandline commandline = mclb.build( request );
-
-        String[] environmentVariables = commandline.getEnvironmentVariables();
-        String envVarMavenTerminateCmd = null;
-        for ( String envVar : environmentVariables )
-        {
-            if ( envVar.startsWith( "MAVEN_TERMINATE_CMD=" ) )
-            {
-                envVarMavenTerminateCmd = envVar;
-                break;
-            }
-        }
-        assertEquals( "MAVEN_TERMINATE_CMD=on", envVarMavenTerminateCmd );
-
-    }
-
-    @Test
     public void testShouldInsertActivatedProfiles()
         throws Exception
     {
@@ -853,49 +819,11 @@ public class MavenCommandLineBuilderTest
     }
 
     @Test
-    public void testShouldSetEnvVar_M2_HOME()
-        throws Exception
-    {
-        Assume.assumeNotNull( System.getenv( "M2_HOME" ) );
-
-        setupTempMavenHomeIfMissing( true );
-
-        InvocationRequest request = newRequest();
-
-        File projectDir = temporaryFolder.newFolder( 
"invoker-tests/maven-terminate-cmd-options-set" );
-
-        request.setBaseDirectory( projectDir );
-
-        createDummyFile( projectDir, "pom.xml" );
-
-        List<String> goals = new ArrayList<>();
-
-        goals.add( "clean" );
-        request.setGoals( goals );
-
-        File mavenHome2 = new File( System.getProperty( "maven.home" ) );
-        mclb.setMavenHome( mavenHome2 );
-
-        Commandline commandline = mclb.build( request );
-
-        String[] environmentVariables = commandline.getEnvironmentVariables();
-        String m2Home = null;
-        for ( String envVar : environmentVariables )
-        {
-            if ( envVar.startsWith( "M2_HOME=" ) )
-            {
-                m2Home = envVar;
-            }
-        }
-        assertEquals( "M2_HOME=" + mavenHome2.getAbsolutePath(), m2Home );
-    }
-
-    @Test
     public void testMvnExecutableFromInvoker()
         throws Exception
     {
-        assumeTrue( "Test only works when maven home can be assigned",
-            System.getProperty( "maven.home" ) != null || System.getenv( 
"M2_HOME" ) != null );
+        assumeThat( "Test only works when maven.home is set",
+            System.getProperty( "maven.home" ), is(notNullValue()));
 
         File mavenExecutable = new File( "mvnDebug" );
 
@@ -911,8 +839,8 @@ public class MavenCommandLineBuilderTest
     public void testMvnExecutableFormRequest()
         throws Exception
     {
-        assumeTrue( "Test only works when maven home can be assigned",
-            System.getProperty( "maven.home" ) != null || System.getenv( 
"M2_HOME" ) != null );
+        assumeThat( "Test only works when maven.home is set",
+            System.getProperty( "maven.home" ), is(notNullValue()));
 
         File mavenExecutable = new File( "mvnDebug" );
 
@@ -927,8 +855,8 @@ public class MavenCommandLineBuilderTest
     public void testDefaultMavenCommand()
         throws Exception
     {
-        assumeTrue( "Test only works when maven home can be assigned",
-            System.getProperty( "maven.home" ) != null || System.getenv( 
"M2_HOME" ) != null );
+        assumeThat( "Test only works when maven.home is set",
+            System.getProperty( "maven.home" ), is(notNullValue()));
 
         mclb.build( newRequest() );
 

Reply via email to