Skip to content

Commit

Permalink
Test Embedded3xLauncher with Maven Home
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Mar 14, 2022
1 parent 07e7423 commit 96979dc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/maven-verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ jobs:
build:
name: Verify
uses: apache/maven-gh-actions-shared/.github/workflows/maven-verify.yml@v2
with:
ff-goal: '-X verify'
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.test>${maven.home}</maven.home.test>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
39 changes: 28 additions & 11 deletions src/site/markdown/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down Expand Up @@ -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.
Expand All @@ -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
[plexus-classwords]: https://codehaus-plexus.github.io/plexus-classworlds/launcher.html
24 changes: 20 additions & 4 deletions src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
* under the License.
*/

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import javax.sound.midi.Soundbank;

import java.io.File;
import java.nio.file.Files;
Expand All @@ -31,19 +30,36 @@
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();
System.out.println("1. maven.home=" + System.getProperty( "maven.home.test" ));
MavenLauncher launcher = Embedded3xLauncher.createFromClasspath();
runLauncher( launcher );
}

@Test
public void testWithMavenHome() throws Exception
{
System.out.println("2. maven.home=" + System.getProperty( "maven.home.test" ));
MavenLauncher launcher = Embedded3xLauncher.createFromMavenHome(
System.getProperty( "maven.home.test" ), 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 );
Expand Down

0 comments on commit 96979dc

Please sign in to comment.