Skip to content

Commit

Permalink
[MSHARED-885] Maven Wrapper fails on non-Windows OSes
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte committed May 3, 2020
1 parent 6280b41 commit b25d7f2
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/org/apache/maven/it/ForkedLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,8 +70,20 @@ class ForkedLauncher

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
{
Expand Down Expand Up @@ -208,5 +221,12 @@ static String extractMavenVersion( List<String> logLines )

return version;
}

private static boolean isWindows()
{
String osName = System.getProperty( "os.name" ).toLowerCase( Locale.US );

return ( osName.indexOf( "windows" ) > -1 );
}

}
99 changes: 99 additions & 0 deletions src/test/java/org/apache/maven/it/ForkedLauncherTest.java
Original file line number Diff line number Diff line change
@@ -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 ) );
}
}

}
20 changes: 20 additions & 0 deletions src/test/resources/wrapper-project/mvnw
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions src/test/resources/wrapper-project/mvnw.cmd
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions src/test/resources/wrapper-project/mvnwDebug
Original file line number Diff line number Diff line change
@@ -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" "$@"
33 changes: 33 additions & 0 deletions src/test/resources/wrapper-project/mvnwDebug.cmd
Original file line number Diff line number Diff line change
@@ -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 %*

0 comments on commit b25d7f2

Please sign in to comment.