[MSHARED-817] Change eclipse aether dependency scope to provided

diff --git a/pom.xml b/pom.xml
index a87744a..8511433 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,40 +68,6 @@
         </executions>
       </plugin>
       <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-shade-plugin</artifactId>
-        <!--
-         ! explicit overwrite cause otherwise we get an 2.4.3...instead. 
-         -->
-        <version>3.1.0</version>
-        <executions>
-          <execution>
-            <phase>package</phase>
-            <goals>
-              <goal>shade</goal>
-            </goals>
-            <configuration>
-              <artifactSet>
-                <includes>
-                  <include>org.eclipse.aether:aether-util</include>
-                </includes>
-              </artifactSet>
-              <filters>
-                <filter>
-                  <artifact>org.eclipse.aether:aether-util</artifact>
-                  <includes>
-                    <!-- to prevent java.lang.ClassNotFoundException: org.eclipse.aether.util.artifact.SubArtifact (M3.1.1 - M3.3.3) -->
-                    <include>org/eclipse/aether/util/artifact/SubArtifact.class</include>
-                    <!-- to prevent java.lang.ClassNotFoundException: org.eclipse.aether.util.filter.* (M3.1.1+ ) -->
-                    <include>org/eclipse/aether/util/filter/*</include>
-                  </includes>
-                </filter>
-              </filters>
-            </configuration>
-          </execution>
-        </executions>
-      </plugin>
-      <plugin>
         <groupId>org.apache.rat</groupId>
         <artifactId>apache-rat-plugin</artifactId>
         <configuration>
@@ -206,7 +172,7 @@
       <groupId>org.sonatype.aether</groupId>
       <artifactId>aether-impl</artifactId>
       <version>1.7</version>
-      <scope>test</scope>
+      <scope>provided</scope>
     </dependency>
 
     <!-- Maven 3.1.x and above -->
@@ -220,7 +186,7 @@
       <groupId>org.eclipse.aether</groupId>
       <artifactId>aether-util</artifactId>
       <version>0.9.0.M2</version>
-      <!-- provided scoped dependencies aren't shaded -->
+      <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>org.eclipse.aether</groupId>
@@ -229,6 +195,14 @@
       <scope>provided</scope>
     </dependency>
 
+    <!-- Allow importing the aether-util library from the user's distribution -->
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-classworlds</artifactId>
+      <version>2.2.3</version>
+      <optional>true</optional>
+    </dependency>
+
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
diff --git a/src/main/java/org/apache/maven/shared/transfer/project/MavenAetherUtils.java b/src/main/java/org/apache/maven/shared/transfer/project/MavenAetherUtils.java
new file mode 100644
index 0000000..610b314
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/transfer/project/MavenAetherUtils.java
@@ -0,0 +1,104 @@
+package org.apache.maven.shared.transfer.project;
+
+/*
+ * 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 org.apache.maven.plugin.MojoExecutionException;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This util class will import the Aether library available from the installed Maven distribution. It will do nothing if
+ * it is called from outside of a ClassRealm.
+ *
+ * @since 0.11.1
+ * @author Gabriel Belingueres <a href="mailto:belingueres@gmail.com">belingueres@gmail.com</a>
+ */
+public class MavenAetherUtils
+{
+
+    private static final Logger LOGGER = LoggerFactory.getLogger( MavenAetherUtils.class );
+
+    private static final String NO_SUCH_REALM_EXCEPTION = "org.codehaus.plexus.classworlds.realm.NoSuchRealmException";
+
+    /**
+     * Import the core Aether library from the maven distribution.
+     *
+     * @param pluginDescriptor the plugin descriptor where the operation will be executed.
+     * @throws MojoExecutionException if there is an error when importing the library.
+     */
+    public static void importAetherLibrary()
+    {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        if ( isClassRealm( classLoader ) )
+        {
+            importAether( classLoader );
+        }
+    }
+
+    /**
+     * Imports aether-util library from the user's Maven distribution.
+     * <p>
+     * PRECONDITION: the classLoader parameter is an instance of ClassRealm.
+     * </p>
+     *
+     * @param classLoader the Classloader which needs to access aether-util.
+     */
+    private static void importAether( ClassLoader classLoader )
+    {
+        ClassRealm classRealm = (ClassRealm) classLoader;
+        try
+        {
+            classRealm.importFrom( "plexus.core", "org.eclipse.aether.util" );
+        }
+        catch ( Exception e )
+        {
+            if ( NO_SUCH_REALM_EXCEPTION.equals( e.getClass().getCanonicalName() ) )
+            {
+                LOGGER.info( "'plexus.core' ClassRealm could not be found. "
+                    + "Ignore this message if you are using the library outside of a Maven execution.", e );
+            }
+            else
+            {
+                // another exception
+                LOGGER.error( "Unexpected exception when importing Aether library to the '{}' ClassRealm", classLoader,
+                              e );
+            }
+        }
+    }
+
+    /**
+     * Using reflection, check if the Classloader is actually an instance of a ClassRealm.
+     *
+     * @param classLoader the Classloader to test.
+     * @return true if it an instance of ClassRealm; false otherwise.
+     */
+    private static boolean isClassRealm( ClassLoader classLoader )
+    {
+        for ( Class<?> clazz = classLoader.getClass(); clazz != null; clazz = clazz.getSuperclass() )
+        {
+            if ( "org.codehaus.plexus.classworlds.realm.ClassRealm".equals( clazz.getCanonicalName() ) )
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/transfer/project/deploy/internal/DefaultProjectDeployer.java b/src/main/java/org/apache/maven/shared/transfer/project/deploy/internal/DefaultProjectDeployer.java
index c46f520..0fae478 100644
--- a/src/main/java/org/apache/maven/shared/transfer/project/deploy/internal/DefaultProjectDeployer.java
+++ b/src/main/java/org/apache/maven/shared/transfer/project/deploy/internal/DefaultProjectDeployer.java
@@ -32,6 +32,7 @@
 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
 import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployer;
 import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException;
+import org.apache.maven.shared.transfer.project.MavenAetherUtils;
 import org.apache.maven.shared.transfer.project.NoFileAssignedException;
 import org.apache.maven.shared.transfer.project.deploy.ProjectDeployer;
 import org.apache.maven.shared.transfer.project.deploy.ProjectDeployerRequest;
@@ -71,6 +72,8 @@
     {
         validateParameters( buildingRequest, projectDeployerRequest, artifactRepository );
 
+        MavenAetherUtils.importAetherLibrary();
+
         Artifact artifact = projectDeployerRequest.getProject().getArtifact();
         String packaging = projectDeployerRequest.getProject().getPackaging();
         File pomFile = projectDeployerRequest.getProject().getFile();
diff --git a/src/main/java/org/apache/maven/shared/transfer/project/install/internal/DefaultProjectInstaller.java b/src/main/java/org/apache/maven/shared/transfer/project/install/internal/DefaultProjectInstaller.java
index 8804ee3..dd6de0d 100644
--- a/src/main/java/org/apache/maven/shared/transfer/project/install/internal/DefaultProjectInstaller.java
+++ b/src/main/java/org/apache/maven/shared/transfer/project/install/internal/DefaultProjectInstaller.java
@@ -34,6 +34,7 @@
 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
 import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
 import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
+import org.apache.maven.shared.transfer.project.MavenAetherUtils;
 import org.apache.maven.shared.transfer.project.NoFileAssignedException;
 import org.apache.maven.shared.transfer.project.install.ProjectInstaller;
 import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest;
@@ -70,6 +71,9 @@
     {
 
         validateParameters( buildingRequest, installerRequest );
+
+        MavenAetherUtils.importAetherLibrary();
+
         MavenProject project = installerRequest.getProject();
 
         Artifact artifact = project.getArtifact();