[5937] Maven-Wrapper for unified project environments
diff --git a/core-it-suite/pom.xml b/core-it-suite/pom.xml
index 774602f..926e116 100644
--- a/core-it-suite/pom.xml
+++ b/core-it-suite/pom.xml
@@ -57,6 +57,12 @@
   
   ITs that don't require to fork Maven can also be run from the IDE using the Maven projects from the workspace if the
   Maven dependencies are added to the test class path.
+
+  To test with the maven wrapper, you should add the wrapperDistroDir too.
+
+    mvn clean test -Prun-its -Dmaven.repo.local=<path-to-local-repo>  -DmavenDistro=<path-to-bin-archive> -DwrapperDistroDir=<path-to-wrapper-distro-directory> -DmavenWrapper=<path-to-wrapper-jar>
+	
+  Tests can pick up the maven.wrapper.distrodir system property to unpack it into their testDir to verify the wrapper.
   
   If you're behind a proxy, use the system properties proxy.host, proxy.port, proxy.user, proxy.pass and
   proxy.nonProxyHosts to specify the required proxy setup for the ITs. Alternatively, set the system property
@@ -141,6 +147,17 @@
       <artifactId>guava</artifactId>
       <version>16.0</version>
     </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-archiver</artifactId>
+      <version>3.6.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.eclipse.sisu</groupId>
+      <artifactId>org.eclipse.sisu.plexus</artifactId>
+      <version>0.3.4</version>
+      <scope>provided</scope>
+    </dependency>
   </dependencies>
 
   <build>
@@ -176,6 +193,14 @@
               <name>maven.it.global-settings.dir</name>
               <value>${project.build.testOutputDirectory}</value>
             </property>
+            <property>
+              <name>maven.wrapper.distrodir</name>
+              <value>${wrapperDistroDir}</value>
+            </property>
+            <property>
+              <name>maven.distro</name>
+              <value>${mavenDistro}</value>
+            </property>
           </systemProperties>
         </configuration>
       </plugin>
@@ -453,6 +478,36 @@
       </build>
     </profile>
     <profile>
+      <id>maven-wrapper</id>
+      <activation>
+        <property>
+          <name>mavenWrapper</name>
+        </property>
+      </activation>
+      <build>
+        <plugins>
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-install-plugin</artifactId>
+            <version>2.5.2</version>
+            <executions>
+              <!-- install, so it can be called via a URL -->
+              <execution>
+                <id>prepare-wrapper</id>
+                <phase>process-test-resources</phase>
+                <goals>
+                  <goal>install-file</goal>
+                </goals>
+                <configuration>
+                  <file>${mavenWrapper}</file>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
+    <profile>
       <id>emma</id>
       <properties>
         <preparedMavenHome>${project.build.directory}/distro</preparedMavenHome>
diff --git a/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java b/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java
index 3f0cbc8..01fdb89 100644
--- a/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java
+++ b/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java
@@ -86,7 +86,7 @@
         suite.addTestSuite( MavenITBootstrapTest.class );
 
         /*
-         * Add tests in reverse alpha order by number below. This makes testing new
+         * Add tests in reverse order of implementation. This makes testing new
          * ITs quicker and since it counts down to zero, it's easier to judge how close
          * the tests are to finishing. Newer tests are also more likely to fail, so this is
          * a fail fast technique as well.
@@ -107,6 +107,7 @@
         // -------------------------------------------------------------------------------------------------------------
         // suite.addTestSuite( MavenIT0108SnapshotUpdateTest.class ); -- MNG-3137
 
+        suite.addTestSuite( MavenITmng5937MavenWrapper.class );
         suite.addTestSuite( MavenITmng4660ResumeFromTest.class );
         suite.addTestSuite( MavenITmng4660OutdatedPackagedArtifact.class );
         suite.addTestSuite( MavenITmng6759TransitiveDependencyRepositoriesTest.class );
diff --git a/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng5937MavenWrapper.java b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng5937MavenWrapper.java
new file mode 100644
index 0000000..3beb03c
--- /dev/null
+++ b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng5937MavenWrapper.java
@@ -0,0 +1,148 @@
+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 java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
+import org.codehaus.plexus.logging.console.ConsoleLogger;
+
+/**
+ * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-5937">MNG-5937</a>.
+ *
+ */
+public class MavenITmng5937MavenWrapper
+    extends AbstractMavenIntegrationTestCase
+{
+    private final Path wrapperDistro;
+    
+    private final Map<String,String> envVars;
+    
+    private final Path baseDir = Paths.get( "target/test-classes/mng-5937-wrapper" );
+    
+    private ZipUnArchiver zipUnArchiver = new ZipUnArchiver();
+    
+    public MavenITmng5937MavenWrapper() throws Exception
+    {
+        super( "[3.7.0,)" );
+        
+        String mavenDist = System.getProperty( "maven.distro" );
+        if ( mavenDist == null )
+        {
+            throw new IllegalStateException( "Missing maven.distro to test maven-wrapper" );
+        }
+        
+        Verifier distInstaller = newVerifier( baseDir.toAbsolutePath().toFile().toString() );
+        distInstaller.setSystemProperty( "file", mavenDist );
+        distInstaller.setSystemProperty( "groupId", "org.apache.maven" );
+        distInstaller.setSystemProperty( "artifactId", "apache-maven" );
+        distInstaller.setSystemProperty( "version", getMavenVersion().toString() );
+        distInstaller.setSystemProperty( "classifier", "bin" );
+        distInstaller.setSystemProperty( "packaging", "zip" );
+        distInstaller.executeGoal( "org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file" );
+        
+        String distroValue = System.getProperty( "maven.wrapper.distrodir" );
+        if ( distroValue == null )
+        {
+            throw new IllegalStateException( "Missing maven.wrapper.distrodir to test maven-wrapper" );
+        }
+        wrapperDistro = Paths.get( distroValue );
+        
+        String localRepo = System.getProperty("maven.repo.local");
+        
+        envVars = new HashMap<>( 4 );
+        envVars.put( "MVNW_REPOURL", Paths.get( localRepo ).toUri().toURL().toString() );
+        envVars.put( "MVNW_VERBOSE", "true" );
+        String javaHome = System.getenv( "JAVA_HOME" );
+        if ( javaHome != null )
+        {
+            // source needs to call the javac executable.
+            // if JAVA_HOME is not set, ForkedLauncher sets it to java.home, which is the JRE home
+            envVars.put( "JAVA_HOME", javaHome );
+        }
+    }
+
+    public void testitMNG5937Bin()
+        throws Exception
+    {
+        final File testDir = baseDir.resolve( "bin" ).toFile();
+        
+        unpack( testDir.toPath(), "bin" );
+
+        envVars.put( "MAVEN_BASEDIR", testDir.getAbsolutePath() );
+
+        Verifier verifier = newVerifier( testDir.getAbsolutePath() );
+        verifier.setAutoclean( false );
+        verifier.setDebug( true );
+        verifier.executeGoal( "validate", envVars );
+        verifier.verifyErrorFreeLog();
+        verifier.resetStreams();
+    }
+
+    public void testitMNG5937Script()
+                    throws Exception
+    {
+        final File testDir = baseDir.resolve( "script" ).toFile();
+        
+        unpack( testDir.toPath(), "script" );
+        
+        envVars.put( "MAVEN_BASEDIR", testDir.getAbsolutePath() );
+
+        Verifier verifier = newVerifier( testDir.getAbsolutePath() );
+        verifier.setAutoclean( false );
+        verifier.setDebug( true );
+        verifier.executeGoal( "validate", envVars );
+        verifier.verifyErrorFreeLog();
+        verifier.resetStreams();
+    }
+
+    public void testitMNG5937Source()
+                    throws Exception
+    {
+        final File testDir = baseDir.resolve( "source" ).toFile();
+        
+        unpack( testDir.toPath(), "source" );
+        
+        envVars.put( "MAVEN_BASEDIR", testDir.getAbsolutePath() );
+
+        Verifier verifier = newVerifier( testDir.getAbsolutePath() );
+        verifier.setAutoclean( false );
+        verifier.setDebug( true );
+        verifier.executeGoal( "validate", envVars );
+        verifier.verifyErrorFreeLog();
+        verifier.resetStreams();
+    }
+
+    private void unpack( Path target, String classifier ) throws IOException
+    {
+        Path distro = wrapperDistro.resolve( "apache-maven-wrapper-" + getMavenVersion() + '-' + classifier + ".zip" );
+        
+        zipUnArchiver.setSourceFile( distro.toFile() );
+        zipUnArchiver.setDestDirectory( target.toFile() );
+        zipUnArchiver.enableLogging( new ConsoleLogger() );
+        
+        zipUnArchiver.extract();
+    }
+}
diff --git a/core-it-suite/src/test/resources/mng-5937-wrapper/bin/pom.xml b/core-it-suite/src/test/resources/mng-5937-wrapper/bin/pom.xml
new file mode 100644
index 0000000..5ceadeb
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5937-wrapper/bin/pom.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.mng5936</groupId>
+  <artifactId>test</artifactId>
+  <version>1</version>
+
+  <name>Maven Integration Test :: MNG-5979</name> 
+
+</project>
diff --git a/core-it-suite/src/test/resources/mng-5937-wrapper/script/pom.xml b/core-it-suite/src/test/resources/mng-5937-wrapper/script/pom.xml
new file mode 100644
index 0000000..5ceadeb
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5937-wrapper/script/pom.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.mng5936</groupId>
+  <artifactId>test</artifactId>
+  <version>1</version>
+
+  <name>Maven Integration Test :: MNG-5979</name> 
+
+</project>
diff --git a/core-it-suite/src/test/resources/mng-5937-wrapper/source/pom.xml b/core-it-suite/src/test/resources/mng-5937-wrapper/source/pom.xml
new file mode 100644
index 0000000..5ceadeb
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5937-wrapper/source/pom.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.mng5936</groupId>
+  <artifactId>test</artifactId>
+  <version>1</version>
+
+  <name>Maven Integration Test :: MNG-5979</name> 
+
+</project>
diff --git a/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java b/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java
index 7e3c42a..9e0edc4 100644
--- a/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java
+++ b/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/AbstractMavenIntegrationTestCase.java
@@ -130,7 +130,7 @@
      *
      * @return The Maven version or <code>null</code> if unknown.
      */
-    private ArtifactVersion getMavenVersion()
+    protected final ArtifactVersion getMavenVersion()
     {
         if ( mavenVersion == null )
         {
diff --git a/pom.xml b/pom.xml
index 9ceb300..b15063d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -81,7 +81,7 @@
       <dependency>
         <groupId>org.apache.maven.shared</groupId>
         <artifactId>maven-verifier</artifactId>
-        <version>1.7.1</version>
+        <version>1.7.2</version>
       </dependency>
     </dependencies>
   </dependencyManagement>
diff --git a/run-its.bat b/run-its.bat
index 7fbbbb3..0045694 100644
--- a/run-its.bat
+++ b/run-its.bat
@@ -19,7 +19,7 @@
 
 @REM How JvZ runs the ITs from a clean slate if it would be on Windows
 
-mvn clean install -Prun-its,embedded -Dmaven.repo.local=%cd%\repo
+mvn clean install -U -Prun-its,embedded -Dmaven.repo.local=%cd%\repo
 
 @REM If behind a proxy try this..