Skip to content

Commit

Permalink
[MSHARED-817] Change eclipse aether dependency scope to provided
Browse files Browse the repository at this point in the history
  • Loading branch information
belingueres authored and rfscholte committed Oct 25, 2019
1 parent 0bdd00a commit 5d86480
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 36 deletions.
46 changes: 10 additions & 36 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,40 +67,6 @@
</execution>
</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>
Expand Down Expand Up @@ -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 -->
Expand All @@ -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>
Expand All @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,6 +72,8 @@ public void deploy( ProjectBuildingRequest buildingRequest, ProjectDeployerReque
{
validateParameters( buildingRequest, projectDeployerRequest, artifactRepository );

MavenAetherUtils.importAetherLibrary();

Artifact artifact = projectDeployerRequest.getProject().getArtifact();
String packaging = projectDeployerRequest.getProject().getPackaging();
File pomFile = projectDeployerRequest.getProject().getFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +71,9 @@ public void install( ProjectBuildingRequest buildingRequest, ProjectInstallerReq
{

validateParameters( buildingRequest, installerRequest );

MavenAetherUtils.importAetherLibrary();

MavenProject project = installerRequest.getProject();

Artifact artifact = project.getArtifact();
Expand Down

0 comments on commit 5d86480

Please sign in to comment.