Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-577] Remove usage of M2_HOME environment variable #35

Merged
merged 1 commit into from Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -776,7 +776,7 @@ enum CheckSumPolicy
/**
* 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.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/apache/maven/shared/invoker/Invoker.java
Expand Up @@ -80,7 +80,7 @@ InvocationResult execute( InvocationRequest request )
/**
* 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.
Expand Down
Expand Up @@ -31,7 +31,6 @@
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;

/**
Expand All @@ -52,8 +51,6 @@ public class MavenCommandLineBuilder

private File mavenExecutable;

private Properties systemEnvVars;

/**
* <p>build.</p>
*
Expand Down Expand Up @@ -207,23 +204,7 @@ protected void setShellEnvironment( InvocationRequest request, Commandline cli )
{
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 )
Expand Down Expand Up @@ -585,19 +566,9 @@ void setupMavenHome( InvocationRequest request )
{
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() )
Expand Down Expand Up @@ -676,16 +647,6 @@ else if ( Os.isFamily( "windows" ) )
}
}

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>
*
Expand Down
4 changes: 2 additions & 2 deletions src/site/apt/usage.apt
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -29,7 +29,6 @@
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;
Expand All @@ -39,7 +38,7 @@ public class DefaultInvokerTest

@Test
public void testBuildShouldSucceed()
throws IOException, MavenInvocationException, URISyntaxException
throws MavenInvocationException, URISyntaxException
{
File basedir = getBasedirForBuild();

Expand All @@ -58,7 +57,7 @@ public void testBuildShouldSucceed()

@Test
public void testBuildShouldFail()
throws IOException, MavenInvocationException, URISyntaxException
throws MavenInvocationException, URISyntaxException
{
File basedir = getBasedirForBuild();

Expand All @@ -77,7 +76,7 @@ public void testBuildShouldFail()

@Test
public void testBuildShouldTimeout()
throws IOException, MavenInvocationException, URISyntaxException
throws MavenInvocationException, URISyntaxException
{
File basedir = getBasedirForBuild();

Expand Down Expand Up @@ -237,15 +236,10 @@ private File findMavenHome()
{
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. Specify 'maven.home' system property" );
}

return new File( mavenHome );
Expand Down
Expand Up @@ -45,6 +45,7 @@
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
Expand Down Expand Up @@ -795,41 +796,6 @@ public void testBuildTypicalMavenInvocationEndToEnd()
assertEquals( projectDir.getCanonicalPath(), commandline.getWorkingDirectory().getCanonicalPath() );
}

@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
Expand All @@ -852,50 +818,12 @@ public void testShouldInsertActivatedProfiles()
assertArgumentsPresentInOrder( commandline, "-P", profile1 + "," + profile2 );
}

@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" );

Expand All @@ -911,8 +839,8 @@ public void testMvnExecutableFromInvoker()
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" );

Expand All @@ -927,8 +855,8 @@ public void testMvnExecutableFormRequest()
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() );

Expand Down