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/03/15 20:08:28 UTC

[maven-verifier] branch master updated: Test Embedded3xLauncher with Maven Home

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-verifier.git


The following commit(s) were added to refs/heads/master by this push:
     new 3ce0eee  Test Embedded3xLauncher with Maven Home
3ce0eee is described below

commit 3ce0eeed78f749460c618cb17702acf2c3118e4d
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Mon Mar 14 23:04:25 2022 +0100

    Test Embedded3xLauncher with Maven Home
---
 pom.xml                                            | 10 ++++++
 src/site/markdown/getting-started.md               | 39 ++++++++++++++++------
 .../apache/maven/it/Embedded3xLauncherTest.java    | 22 +++++++++---
 .../java/org/apache/maven/it/VerifierTest.java     | 30 +++++++++++------
 4 files changed, 74 insertions(+), 27 deletions(-)

diff --git a/pom.xml b/pom.xml
index b45c088..24fab42 100644
--- a/pom.xml
+++ b/pom.xml
@@ -133,6 +133,16 @@
             </excludes>
           </configuration>
         </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>2.22.2</version>
+          <configuration>
+            <systemPropertyVariables>
+              <maven.home>${maven.home}</maven.home>
+            </systemPropertyVariables>
+          </configuration>
+        </plugin>
       </plugins>
     </pluginManagement>
   </build>
diff --git a/src/site/markdown/getting-started.md b/src/site/markdown/getting-started.md
index 6290c35..fc1f620 100644
--- a/src/site/markdown/getting-started.md
+++ b/src/site/markdown/getting-started.md
@@ -63,9 +63,35 @@ The following mechanism determines the binary which is used for executing Maven.
 
 Whether the embedded or the forked launcher are used depends on the field `forkJvm` set through the constructor or `setForkJvm` or as fallback on the value of system property `verifier.forkMode`.
 
+### Determining Maven Home Directory
+
+The following directories are considered as potential Maven home directory (relevant for both forked launcher and embedded launcher with  [Plexus Classworlds Loader][plexus-classwords]). The first existing directory from the list is used.
+
+1. Maven Home path given in the constructor
+2. System property `maven.home`
+3. Environment variable `M2_HOME`
+4. System property `user.home` suffixed with `/m2` (only considered if it contains `bin/mvn`)
+
+### Setting Maven Home for Embedded Launcher
+
+In order to pass `Maven Home` used for executing project itself to tests execution, `maven-surefire` can be configured like:
+
+```
+  <plugin>
+    <groupId>org.apache.maven.plugins</groupId>
+    <artifactId>maven-surefire-plugin</artifactId>
+    <configuration>
+      <systemPropertyVariables>
+        <maven.home>${maven.home}</maven.home>
+      </systemPropertyVariables>
+    </configuration>
+  </plugin>
+```
+
 ### Class Path For Embedded Launcher
 
-To use the embedded launcher it is important that some artifacts are in the class path. For the Context Class Loader case this would mean the following dependencies are needed at least (for Maven 3.8.4):
+In case when `Maven Home Directory` can not be determined, to use the embedded launcher it is important that some artifacts are in the class path. 
+For the Context Class Loader case this would mean the following dependencies are needed at least (for Maven 3.8.4):
 
 ```
 <!-- embedder for testing Embedded3xLauncher with classpath -->
@@ -103,15 +129,6 @@ To use the embedded launcher it is important that some artifacts are in the clas
 </dependency>
 ```
 
-### Determining Maven Home Directory
-
-The following directories are considered as potential Maven home directory (relevant for both forked launcher and embedded launcher with  [Plexus Classworlds Loader][plexus-classwords]). The first existing directory from the list is used. 
-
-1. Maven Home path given in the constructor
-2. System property `maven.home`
-3. Environment variable `M2_HOME`
-4. System property `user.home` suffixed with `/m2` (only considered if it contains `bin/mvn`)
-
 ## Run
 
 Calling `executeGoals` runs Maven with the given goals or phases and optionally some additional environment variables. It throws a `VerificationException` in case the execution is not successful (e.g. binary not found or exit code > 0). It is either using a forked JVM or is executed in the same JVM depending on the configuration.
@@ -130,4 +147,4 @@ The main method `verify(boolean)` takes into consideration a file named `expecte
 verifier.verify( true ); // if true, throws an exception in case of errors in the build log
 ```
 
-[plexus-classwords]: https://codehaus-plexus.github.io/plexus-classworlds/launcher.html
\ No newline at end of file
+[plexus-classwords]: https://codehaus-plexus.github.io/plexus-classworlds/launcher.html
diff --git a/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java b/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java
index af99d9d..9548a1b 100644
--- a/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java
+++ b/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java
@@ -19,9 +19,6 @@ package org.apache.maven.it;
  * under the License.
  */
 
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-
 import java.io.File;
 import java.nio.file.Files;
 import java.nio.file.Paths;
@@ -31,19 +28,34 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
 public class Embedded3xLauncherTest
 {
     @Rule
     public TemporaryFolder temporaryFolder = new TemporaryFolder();
 
-    private Embedded3xLauncher launcher;
 
     private final String workingDir = Paths.get( "src/test/resources" ).toAbsolutePath().toString();
 
     @Test
     public void testWithClasspath() throws Exception
     {
-        launcher = Embedded3xLauncher.createFromClasspath();
+        MavenLauncher launcher = Embedded3xLauncher.createFromClasspath();
+        runLauncher( launcher );
+    }
+
+    @Test
+    public void testWithMavenHome() throws Exception
+    {
+        MavenLauncher launcher = Embedded3xLauncher.createFromMavenHome(
+            System.getProperty( "maven.home" ), null, null );
+        runLauncher( launcher );
+    }
+
+    private void runLauncher( MavenLauncher launcher ) throws Exception
+    {
         File logFile = temporaryFolder.newFile( "build.log" );
 
         int exitCode = launcher.run( new String[] {"clean"}, new Properties(), workingDir, logFile );
diff --git a/src/test/java/org/apache/maven/it/VerifierTest.java b/src/test/java/org/apache/maven/it/VerifierTest.java
index fc45455..69cd80a 100644
--- a/src/test/java/org/apache/maven/it/VerifierTest.java
+++ b/src/test/java/org/apache/maven/it/VerifierTest.java
@@ -19,21 +19,21 @@ package org.apache.maven.it;
  * under the License.
  */
 
-import static org.junit.Assert.assertEquals;
-import static org.hamcrest.CoreMatchers.isA;
-
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.net.URISyntaxException;
 import java.nio.file.Paths;
 import java.util.Arrays;
+import java.util.Properties;
 
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 
+import static org.hamcrest.CoreMatchers.isA;
+import static org.junit.Assert.assertEquals;
+
 public class VerifierTest
 {
     @Rule
@@ -50,9 +50,17 @@ public class VerifierTest
     @Test
     public void testSunBug9009028ForJdk()
     {
-        final String version = System.getProperty( "java.version" );
-        System.setProperties( null );
-        assertEquals( version, System.getProperty( "java.version" ) );
+        Properties oldProperties = System.getProperties();
+        try
+        {
+            final String version = System.getProperty( "java.version" );
+            System.setProperties( null );
+            assertEquals( version, System.getProperty( "java.version" ) );
+        }
+        finally
+        {
+            System.setProperties( oldProperties );
+        }
     }
 
     @Test
@@ -91,22 +99,22 @@ public class VerifierTest
     {
         assertEquals( "--- plugin:version:goal (id) @ artifactId ---",
                       Verifier.stripAnsi( "\u001B[1m--- \u001B[0;32mplugin:version:goal\u001B[0;1m (id)\u001B[m @ "
-                          + "\u001B[36martifactId\u001B[0;1m ---\u001B[m" ) );
+                                              + "\u001B[36martifactId\u001B[0;1m ---\u001B[m" ) );
     }
 
     @Test
     public void testLoadPropertiesFNFE() throws VerificationException
     {
-        exception.expectCause( isA( FileNotFoundException.class) );
+        exception.expectCause( isA( FileNotFoundException.class ) );
 
         Verifier verifier = new Verifier( "src/test/resources" );
         verifier.loadProperties( "unknown.properties" );
     }
 
     @Test
-    public void testDedicatedMavenHome() throws VerificationException, IOException, URISyntaxException 
+    public void testDedicatedMavenHome() throws VerificationException, IOException
     {
-        String mavenHome  = Paths.get( "src/test/resources/maven-home" ).toAbsolutePath().toString();;
+        String mavenHome = Paths.get( "src/test/resources/maven-home" ).toAbsolutePath().toString();
         Verifier verifier = new Verifier( temporaryFolder.getRoot().toString(), null, false, mavenHome );
         verifier.executeGoal( "some-goal" );
         File logFile = new File( verifier.getBasedir(), verifier.getLogFileName() );