You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2020/12/28 12:27:26 UTC

[maven-invoker] 01/01: [MSHARED-577] Remove M2_HOME

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

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

commit f10cb4ce6c8d61addeacd0d6a2941f71cfb1197c
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Sun Feb 23 15:51:35 2020 +0100

    [MSHARED-577] Remove M2_HOME
---
 pom.xml                                            |  1 +
 .../org/apache/maven/shared/invoker/Invoker.java   |  2 +-
 .../shared/invoker/MavenCommandLineBuilder.java    | 29 +++++++++-------------
 src/site/apt/usage.apt                             |  2 +-
 .../maven/shared/invoker/DefaultInvokerTest.java   | 11 ++------
 .../invoker/MavenCommandLineBuilderTest.java       | 14 ++++-------
 .../resources/test-build-should-succeed/pom.xml    |  2 +-
 .../resources/test-build-should-timeout/pom.xml    |  2 +-
 src/test/resources/test-space-properties/pom.xml   |  6 ++---
 9 files changed, 27 insertions(+), 42 deletions(-)

diff --git a/pom.xml b/pom.xml
index b8ad5f9..3443b79 100644
--- a/pom.xml
+++ b/pom.xml
@@ -94,6 +94,7 @@ under the License.
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
+        <version>3.0.0-M4</version>
         <configuration>
           <redirectTestOutputToFile>true</redirectTestOutputToFile>
           <systemProperties>
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 369be70..63f2fdd 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 57021a0..82a08e0 100644
--- a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
+++ b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
@@ -30,6 +30,7 @@ import org.apache.maven.shared.invoker.InvocationRequest.CheckSumPolicy;
 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;
 
@@ -118,15 +119,10 @@ public class MavenCommandLineBuilder
         }
 
         if ( ( mavenHome == null ) && ( System.getProperty( "maven.home" ) == null ) )
-        // can be restored with 1.5
-        // && ( System.getenv( "M2_HOME" ) != null ) )
         {
-            if ( !getSystemEnvVars().containsKey( "M2_HOME" ) )
-            {
-                throw new IllegalStateException( "Maven application directory was not "
-                    + "specified, and ${maven.home} is not provided in the system "
-                    + "properties. Specify at least one of these." );
-            }
+            throw new IllegalStateException( "Maven application directory was not "
+                + "specified, and ${maven.home} is not provided in the system "
+                + "properties. Specify at least one of these." );
         }
     }
 
@@ -203,9 +199,6 @@ public class MavenCommandLineBuilder
             {
                 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 ( Exception e )
             {
@@ -259,7 +252,14 @@ public class MavenCommandLineBuilder
 
         if ( ( goals != null ) && !goals.isEmpty() )
         {
-            cli.createArg().setLine( StringUtils.join( goals.iterator(), " " ) );
+            try
+            {
+                cli.createArg().setLine( StringUtils.join( goals.iterator(), " " ) );
+            }
+            catch ( CommandLineException e )
+            {
+                e.printStackTrace();
+            }
         }
     }
 
@@ -543,11 +543,6 @@ public class MavenCommandLineBuilder
                     }
                 }
             }
-
-            if ( ( mavenHome == null ) && ( getSystemEnvVars().getProperty( "M2_HOME" ) != null ) )
-            {
-                mavenHome = new File( getSystemEnvVars().getProperty( "M2_HOME" ) );
-            }
         }
 
         logger.debug( "Using ${maven.home} of: \'" + mavenHome + "\'." );
diff --git a/src/site/apt/usage.apt b/src/site/apt/usage.apt
index c870567..f23832f 100644
--- a/src/site/apt/usage.apt
+++ b/src/site/apt/usage.apt
@@ -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 874f51d..ce3d460 100644
--- a/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java
+++ b/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java
@@ -86,7 +86,7 @@ public class DefaultInvokerTest
         request.setBaseDirectory( basedir );
         request.setDebug( true );
         request.setGoals( Arrays.asList( "clean", "package" ) );
-        request.setTimeoutInSeconds( 4 );
+        request.setTimeoutInSeconds( 3 );
         request.setProperties( getProperties() );
 
         InvocationResult result = invoker.execute( request );
@@ -229,19 +229,12 @@ public class DefaultInvokerTest
     }
 
     private File findMavenHome()
-        throws IOException
     {
         String mavenHome = System.getProperty( "maven.home" );
-
-        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. Please 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 f9e29bb..b2a658a 100644
--- a/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
+++ b/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
@@ -42,10 +42,7 @@ import org.junit.rules.TemporaryFolder;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.notNullValue;
-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.Assert.*;
 import static org.junit.Assume.assumeThat;
 
 public class MavenCommandLineBuilderTest
@@ -817,10 +814,10 @@ public class MavenCommandLineBuilderTest
     }
 
     @Test
-    public void testShouldSetEnvVar_M2_HOME()
+    public void testShouldNotSetEnvVar_M2_HOME()
         throws Exception
     {
-        Assume.assumeNotNull( System.getenv( "M2_HOME" ) );
+        assertNull( System.getenv( "M2_HOME" ) );
 
         setupTempMavenHomeIfMissing( true );
 
@@ -845,15 +842,14 @@ public class MavenCommandLineBuilderTest
 
         String[] environmentVariables = commandline.getEnvironmentVariables();
         String m2Home = null;
-        for ( int i = 0; i < environmentVariables.length; i++ )
+        for ( String envVar : environmentVariables )
         {
-            String envVar = environmentVariables[i];
             if ( envVar.startsWith( "M2_HOME=" ) )
             {
                 m2Home = envVar;
             }
         }
-        assertEquals( "M2_HOME=" + mavenHome2.getAbsolutePath(), m2Home );
+        assertNull( "M2_HOME=" + mavenHome2.getAbsolutePath(), m2Home );
     }
 
     @Test
diff --git a/src/test/resources/test-build-should-succeed/pom.xml b/src/test/resources/test-build-should-succeed/pom.xml
index 9640135..892a39c 100644
--- a/src/test/resources/test-build-should-succeed/pom.xml
+++ b/src/test/resources/test-build-should-succeed/pom.xml
@@ -27,7 +27,7 @@ under the License.
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
-      <version>3.8.2</version>
+      <version>4.13</version>
       <scope>test</scope>
     </dependency>
   </dependencies>
diff --git a/src/test/resources/test-build-should-timeout/pom.xml b/src/test/resources/test-build-should-timeout/pom.xml
index 711ffd0..cd0f828 100644
--- a/src/test/resources/test-build-should-timeout/pom.xml
+++ b/src/test/resources/test-build-should-timeout/pom.xml
@@ -37,7 +37,7 @@ under the License.
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-plugin</artifactId>
-          <version>2.21.0</version>
+          <version>2.22.2</version>
         </plugin>
       </plugins>
     </pluginManagement>
diff --git a/src/test/resources/test-space-properties/pom.xml b/src/test/resources/test-space-properties/pom.xml
index d0378d7..b199951 100644
--- a/src/test/resources/test-space-properties/pom.xml
+++ b/src/test/resources/test-space-properties/pom.xml
@@ -28,9 +28,9 @@ under the License.
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-antrun-plugin</artifactId>
-        <version>1.1</version>
+        <version>3.0.0</version>
         <configuration>
-          <tasks>
+          <target>
             <property name="prop0" value="${key}"/>
             <echo>${prop0}</echo>
             <property name="prop1" value="${key with spaces}"/>
@@ -49,7 +49,7 @@ under the License.
                 </not>
               </condition>
             </fail>
-          </tasks>
+          </target>
         </configuration>
         <executions>
           <execution>