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/08 18:48:37 UTC

[maven-verifier] branch master updated: [MSHARED-1015] correctly set system property "maven.multiModuleProjectDirectory"

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 b6eded9  [MSHARED-1015] correctly set system property "maven.multiModuleProjectDirectory"
b6eded9 is described below

commit b6eded916a3891df651f9f0bb10fb9ae3f54d565
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Tue Jan 4 11:03:21 2022 +0100

    [MSHARED-1015] correctly set system property
    "maven.multiModuleProjectDirectory"
    
    Add tests for using Embedded3xLauncher with Maven embedder on classpath
---
 pom.xml                                            | 35 +++++++++++++-
 .../org/apache/maven/it/Embedded3xLauncher.java    | 27 ++++++++++-
 .../apache/maven/it/Embedded3xLauncherTest.java    | 55 ++++++++++++++++++++++
 .../org/apache/maven/it/ForkedLauncherTest.java    |  2 +-
 4 files changed, 116 insertions(+), 3 deletions(-)

diff --git a/pom.xml b/pom.xml
index 8c75b16..9be8c2a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -82,7 +82,40 @@
       <artifactId>hamcrest-core</artifactId>
       <version>2.2</version>
     </dependency>
-    
+    <!-- embedder for testing Embedded3xLauncher with classpath -->
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-embedder</artifactId>
+      <version>3.8.4</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- START transitive dependencies of embedder -->
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-simple</artifactId>
+      <version>1.7.32</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- required due to https://issues.apache.org/jira/browse/MNG-6561 -->
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-compat</artifactId>
+      <version>3.8.4</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.resolver</groupId>
+      <artifactId>maven-resolver-connector-basic</artifactId>
+      <version>1.6.3</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.resolver</groupId>
+      <artifactId>maven-resolver-transport-http</artifactId>
+      <version>1.6.3</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- END transitive dependencies of embedder -->
   </dependencies>
 
   <build>
diff --git a/src/main/java/org/apache/maven/it/Embedded3xLauncher.java b/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
index 55b69b3..998fa88 100644
--- a/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
+++ b/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
@@ -196,10 +196,13 @@ class Embedded3xLauncher
         PrintStream out = ( logFile != null ) ? new PrintStream( new FileOutputStream( logFile ) ) : System.out;
         try
         {
+            File workingDirectoryPath = new File( workingDirectory );
             Properties originalProperties = System.getProperties();
             System.setProperties( null );
             System.setProperty( "maven.home", originalProperties.getProperty( "maven.home", "" ) );
-            System.setProperty( "user.dir", new File( workingDirectory ).getAbsolutePath() );
+            System.setProperty( "user.dir", workingDirectoryPath.getAbsolutePath() );
+            System.setProperty( "maven.multiModuleProjectDirectory", 
+                    findProjectBaseDirectory( workingDirectoryPath ).getAbsolutePath() );
 
             for ( Object o : systemProperties.keySet() )
             {
@@ -268,4 +271,26 @@ class Embedded3xLauncher
         throw new LauncherException( "Could not determine embedded Maven version" );
     }
 
+    /**
+     * Replicates the logic from <a href="https://git.io/JSMug">Maven start script</a> to find 
+     * the project's base directory.
+     * @param workingDirectory the working directory
+     * @return the project's base directory
+     */
+    private static File findProjectBaseDirectory( File workingDirectory )
+    {
+        File currentDirectory = workingDirectory;
+        // traverses directory structure from process work directory to filesystem root
+        // first directory with .mvn subdirectory is considered project base directory
+        while ( currentDirectory != null && currentDirectory.getParentFile() != null )
+        {
+            // see if /.mvn exists
+            if ( new File( currentDirectory, ".mvn" ).isDirectory() )
+            {
+                return currentDirectory;
+            }
+            currentDirectory = currentDirectory.getParentFile();
+        }
+        return workingDirectory;
+    }
 }
diff --git a/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java b/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java
new file mode 100644
index 0000000..fd0b333
--- /dev/null
+++ b/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java
@@ -0,0 +1,55 @@
+package org.apache.maven.it;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * 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;
+import java.util.Properties;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+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();
+        File logFile = temporaryFolder.newFile( "build.log" );
+
+        int exitCode = launcher.run( new String[]{ "clean" }, new Properties(), workingDir, logFile );
+
+        assertThat( "exit code unexpected, build log: " + System.lineSeparator() +
+                String.join(System.lineSeparator(), Files.readAllLines( logFile.toPath() ) ), exitCode, is ( 0 ) );
+    }
+
+}
diff --git a/src/test/java/org/apache/maven/it/ForkedLauncherTest.java b/src/test/java/org/apache/maven/it/ForkedLauncherTest.java
index f295a6c..8642fb9 100644
--- a/src/test/java/org/apache/maven/it/ForkedLauncherTest.java
+++ b/src/test/java/org/apache/maven/it/ForkedLauncherTest.java
@@ -58,7 +58,7 @@ public class ForkedLauncherTest
         // most likely this contains the exception in case exitCode != 0
         expectFileLine( logFile, "Hello World" );
 
-        assertThat( "exit code", exitCode , is ( 0 ) );
+        assertThat( "exit code", exitCode, is ( 0 ) );
     }
 
     @Test