[MSHARED-885] Maven Wrapper fails on non-Windows OSes
diff --git a/pom.xml b/pom.xml
index 1107bfa..025d2f8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -77,6 +77,12 @@
       <artifactId>junit</artifactId>
       <version>4.13</version>
     </dependency>
+    <dependency>
+      <groupId>org.hamcrest</groupId>
+      <artifactId>hamcrest-core</artifactId>
+      <version>2.2</version>
+    </dependency>
+    
   </dependencies>
 
   <build>
diff --git a/src/main/java/org/apache/maven/it/ForkedLauncher.java b/src/main/java/org/apache/maven/it/ForkedLauncher.java
index 3607be1..7a12193 100644
--- a/src/main/java/org/apache/maven/it/ForkedLauncher.java
+++ b/src/main/java/org/apache/maven/it/ForkedLauncher.java
@@ -26,6 +26,7 @@
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Properties;
 import java.util.regex.Matcher;
@@ -69,8 +70,20 @@
 
         if ( wrapper )
         {
-            String script = "mvnw" + ( debugJvm ? "Debug" : "" );
-            executable = new File( script ).getPath();
+            final StringBuilder script = new StringBuilder();
+            
+            if ( !isWindows() )
+            {
+                script.append( "./" );
+            }
+            
+            script.append( "mvnw" );
+            
+            if ( debugJvm )
+            {
+                script.append( "Debug" );
+            }
+            executable = script.toString();
         }
         else
         {
@@ -208,5 +221,12 @@
 
         return version;
     }
+    
+    private static boolean isWindows()
+    {
+        String osName = System.getProperty( "os.name" ).toLowerCase( Locale.US );
+
+        return ( osName.indexOf( "windows" ) > -1 );
+    }
 
 }
diff --git a/src/test/java/org/apache/maven/it/ForkedLauncherTest.java b/src/test/java/org/apache/maven/it/ForkedLauncherTest.java
new file mode 100644
index 0000000..f295a6c
--- /dev/null
+++ b/src/test/java/org/apache/maven/it/ForkedLauncherTest.java
@@ -0,0 +1,99 @@
+package org.apache.maven.it;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+
+/*
+ * 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.Matchers.is;
+import static org.junit.Assert.fail;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Properties;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class ForkedLauncherTest
+{
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+    
+    private ForkedLauncher launcher;
+    
+    private final String workingDir = Paths.get( "src/test/resources/wrapper-project" ).toAbsolutePath().toString();
+    
+    @Test
+    public void mvnw() throws Exception
+    {
+        launcher = new ForkedLauncher( ".", Collections.<String, String>emptyMap(), false, true );
+        File logFile = temporaryFolder.newFile( "build.log" );
+
+        int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile );
+
+        // most likely this contains the exception in case exitCode != 0
+        expectFileLine( logFile, "Hello World" );
+
+        assertThat( "exit code", exitCode , is ( 0 ) );
+    }
+
+    @Test
+    public void mvnwDebug() throws Exception
+    {
+        launcher = new ForkedLauncher( ".", Collections.<String, String>emptyMap(), true, true );
+        File logFile = temporaryFolder.newFile( "build.log" );
+
+        int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile );
+
+        // most likely this contains the exception in case exitCode != 0
+        expectFileLine( logFile, "Hello World" );
+
+        assertThat( "exit code", exitCode , is ( 0 ) );
+    }
+    
+    private void expectFileLine( File file, String expectedline ) throws IOException
+    {
+        try ( FileReader fr = new FileReader( file );
+              BufferedReader br = new BufferedReader( fr ) )
+        {
+            Collection<String> text = new ArrayList<>();
+            String line;
+            while ( ( line = br.readLine() ) != null )
+            {
+                if ( expectedline.equals( line ) )
+                {
+                    return;
+                }
+                text.add( line );
+            }
+
+            String message = "%s doesn't contain '%s', was:\n%s";
+            fail( String.format( message, file.getName(), expectedline, text ) );
+        }
+    }
+
+}
diff --git a/src/test/resources/wrapper-project/mvnw b/src/test/resources/wrapper-project/mvnw
new file mode 100755
index 0000000..e8c29e9
--- /dev/null
+++ b/src/test/resources/wrapper-project/mvnw
@@ -0,0 +1,20 @@
+#!/bin/sh
+# ----------------------------------------------------------------------------
+# 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.
+# ----------------------------------------------------------------------------
+echo Hello World
diff --git a/src/test/resources/wrapper-project/mvnw.cmd b/src/test/resources/wrapper-project/mvnw.cmd
new file mode 100644
index 0000000..bf71f55
--- /dev/null
+++ b/src/test/resources/wrapper-project/mvnw.cmd
@@ -0,0 +1,21 @@
+@REM ----------------------------------------------------------------------------
+@REM Licensed to the Apache Software Foundation (ASF) under one
+@REM or more contributor license agreements.  See the NOTICE file
+@REM distributed with this work for additional information
+@REM regarding copyright ownership.  The ASF licenses this file
+@REM to you under the Apache License, Version 2.0 (the
+@REM "License"); you may not use this file except in compliance
+@REM with the License.  You may obtain a copy of the License at
+@REM
+@REM    http://www.apache.org/licenses/LICENSE-2.0
+@REM
+@REM Unless required by applicable law or agreed to in writing,
+@REM software distributed under the License is distributed on an
+@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+@REM KIND, either express or implied.  See the License for the
+@REM specific language governing permissions and limitations
+@REM under the License.
+@REM ----------------------------------------------------------------------------
+@echo off
+@ECHO Hello World
+@echo on
\ No newline at end of file
diff --git a/src/test/resources/wrapper-project/mvnwDebug b/src/test/resources/wrapper-project/mvnwDebug
new file mode 100755
index 0000000..cee08c7
--- /dev/null
+++ b/src/test/resources/wrapper-project/mvnwDebug
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+# 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.
+
+# -----------------------------------------------------------------------------
+# Apache Maven Debug Script
+#
+# Environment Variable Prerequisites
+#
+#   JAVA_HOME       Must point at your Java Development Kit installation.
+#   MAVEN_OPTS      (Optional) Java runtime options used when Maven is executed.
+#   MAVEN_SKIP_RC   (Optional) Flag to disable loading of mavenrc files.
+# -----------------------------------------------------------------------------
+
+MAVEN_DEBUG_OPTS=
+
+echo Preparing to execute Maven Wrapper in debug mode
+
+env MAVEN_OPTS="$MAVEN_OPTS" MAVEN_DEBUG_OPTS="$MAVEN_DEBUG_OPTS" "`dirname "$0"`/mvnw" "$@"
diff --git a/src/test/resources/wrapper-project/mvnwDebug.cmd b/src/test/resources/wrapper-project/mvnwDebug.cmd
new file mode 100644
index 0000000..8a40367
--- /dev/null
+++ b/src/test/resources/wrapper-project/mvnwDebug.cmd
@@ -0,0 +1,33 @@
+@REM Licensed to the Apache Software Foundation (ASF) under one
+@REM or more contributor license agreements.  See the NOTICE file
+@REM distributed with this work for additional information
+@REM regarding copyright ownership.  The ASF licenses this file
+@REM to you under the Apache License, Version 2.0 (the
+@REM "License"); you may not use this file except in compliance
+@REM with the License.  You may obtain a copy of the License at
+@REM
+@REM    http://www.apache.org/licenses/LICENSE-2.0
+@REM
+@REM Unless required by applicable law or agreed to in writing,
+@REM software distributed under the License is distributed on an
+@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+@REM KIND, either express or implied.  See the License for the
+@REM specific language governing permissions and limitations
+@REM under the License.
+
+@REM -----------------------------------------------------------------------------
+@REM Apache Maven Debug Script
+@REM
+@REM Environment Variable Prerequisites
+@REM
+@REM   JAVA_HOME          Must point at your Java Development Kit installation.
+@REM   MAVEN_BATCH_ECHO  (Optional) Set to 'on' to enable the echoing of the batch commands.
+@REM   MAVEN_BATCH_PAUSE (Optional) set to 'on' to wait for a key stroke before ending.
+@REM   MAVEN_OPTS        (Optional) Java runtime options used when Maven is executed.
+@REM   MAVEN_SKIP_RC     (Optional) Flag to disable loading of mavenrc files.
+@REM -----------------------------------------------------------------------------
+
+@setlocal
+@set MAVEN_DEBUG_OPTS=
+
+@call "%~dp0"mvnw.cmd %*