-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MDEP-82] - resolve plugin dependencies #130
Open
berlam
wants to merge
1
commit into
apache:master
Choose a base branch
from
berlam:MDEP-82
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,15 @@ | |
* under the License. | ||
*/ | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedHashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.collections4.map.UnmodifiableMap; | ||
import org.apache.maven.RepositoryUtils; | ||
import org.apache.maven.artifact.Artifact; | ||
import org.apache.maven.artifact.ArtifactUtils; | ||
import org.apache.maven.artifact.DefaultArtifact; | ||
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; | ||
import org.apache.maven.model.Plugin; | ||
import org.apache.maven.model.ReportPlugin; | ||
import org.apache.maven.model.Reporting; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
|
@@ -37,21 +40,42 @@ | |
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException; | ||
import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate; | ||
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException; | ||
import org.codehaus.plexus.component.annotations.Requirement; | ||
import org.eclipse.aether.artifact.ArtifactTypeRegistry; | ||
import org.eclipse.aether.graph.Dependency; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* Goal that resolves all project plugins and reports and their dependencies. | ||
* | ||
* @author <a href="mailto:[email protected]">Brian Fox</a> | ||
* @since 2.0 | ||
*/ | ||
@Mojo( name = "resolve-plugins", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true ) | ||
//CHECKSTYLE_OFF: LineLength | ||
@Mojo( name = "resolve-plugins", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresOnline = true, threadSafe = true ) | ||
//CHECKSTYLE_ON: LineLength | ||
public class ResolvePluginsMojo | ||
extends AbstractResolveMojo | ||
{ | ||
/** | ||
* Maven artifact handler manager | ||
*/ | ||
@Requirement | ||
private ArtifactHandlerManager artifactHandlerManager; | ||
|
||
@Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" ) | ||
private String outputEncoding; | ||
|
||
private ArtifactTypeRegistry typeRegistry; | ||
|
||
/** | ||
* Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version. | ||
* | ||
|
@@ -63,29 +87,31 @@ protected void doExecute() | |
{ | ||
try | ||
{ | ||
typeRegistry = RepositoryUtils.newArtifactTypeRegistry( artifactHandlerManager ); | ||
// ideally this should either be DependencyCoordinates or DependencyNode | ||
final Set<Artifact> plugins = resolvePluginArtifacts(); | ||
final Map<String, Plugin> plugins = resolvePlugins(); | ||
final Map<String, Artifact> resolvedArtifacts = fetchArtifacts( plugins.values() ); | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
sb.append( System.lineSeparator() ); | ||
sb.append( "The following plugins have been resolved:" ); | ||
sb.append( System.lineSeparator() ); | ||
if ( plugins == null || plugins.isEmpty() ) | ||
if ( plugins.isEmpty() ) | ||
{ | ||
sb.append( " none" ); | ||
sb.append( System.lineSeparator() ); | ||
} | ||
else | ||
{ | ||
for ( Artifact plugin : plugins ) | ||
for ( Map.Entry<String, Artifact> plugin : resolvedArtifacts.entrySet() ) | ||
{ | ||
String artifactFilename = null; | ||
if ( outputAbsoluteArtifactFilename ) | ||
{ | ||
try | ||
{ | ||
// we want to print the absolute file name here | ||
artifactFilename = plugin.getFile().getAbsoluteFile().getPath(); | ||
artifactFilename = plugin.getValue().getFile().getAbsoluteFile().getPath(); | ||
} | ||
catch ( NullPointerException e ) | ||
{ | ||
|
@@ -94,7 +120,7 @@ protected void doExecute() | |
} | ||
} | ||
|
||
String id = plugin.toString(); | ||
String id = plugin.getKey(); | ||
sb.append( " " ) | ||
.append( id ) | ||
.append( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" ) | ||
|
@@ -103,11 +129,23 @@ protected void doExecute() | |
if ( !excludeTransitive ) | ||
{ | ||
DefaultDependableCoordinate pluginCoordinate = new DefaultDependableCoordinate(); | ||
pluginCoordinate.setGroupId( plugin.getGroupId() ); | ||
pluginCoordinate.setArtifactId( plugin.getArtifactId() ); | ||
pluginCoordinate.setVersion( plugin.getVersion() ); | ||
pluginCoordinate.setGroupId( plugin.getValue().getGroupId() ); | ||
pluginCoordinate.setArtifactId( plugin.getValue().getArtifactId() ); | ||
pluginCoordinate.setVersion( plugin.getValue().getVersion() ); | ||
|
||
for ( final Artifact artifact : resolveArtifactDependencies( pluginCoordinate ) ) | ||
Set<Artifact> artifacts = resolveArtifactDependencies( pluginCoordinate ); | ||
for ( org.apache.maven.model.Dependency d : plugins.get( plugin.getKey() ).getDependencies() ) | ||
{ | ||
Dependency dependency = RepositoryUtils.toDependency( d, typeRegistry ); | ||
Artifact artifact = RepositoryUtils.toArtifact( dependency.getArtifact() ); | ||
|
||
ProjectBuildingRequest buildingRequest = newResolvePluginProjectBuildingRequest(); | ||
getArtifactResolver().resolveArtifact( buildingRequest, artifact ); | ||
|
||
artifacts.add( artifact ); | ||
} | ||
|
||
for ( final Artifact artifact : artifacts ) | ||
{ | ||
artifactFilename = null; | ||
if ( outputAbsoluteArtifactFilename ) | ||
|
@@ -155,26 +193,63 @@ protected void doExecute() | |
/** | ||
* This method resolves the plugin artifacts from the project. | ||
* | ||
* @return set of resolved plugin artifacts | ||
* @throws ArtifactFilterException in case of an error | ||
* @throws ArtifactResolverException in case of an error | ||
* @return map of resolved plugins | ||
*/ | ||
protected Set<Artifact> resolvePluginArtifacts() | ||
protected Map<String, Plugin> resolvePlugins() | ||
throws ArtifactFilterException, ArtifactResolverException | ||
{ | ||
final Set<Artifact> plugins = getProject().getPluginArtifacts(); | ||
final Set<Artifact> reports = getProject().getReportArtifacts(); | ||
Set<Plugin> plugins = new LinkedHashSet<>( getProject().getBuildPlugins() ); | ||
|
||
Reporting reporting = getProject().getReporting(); | ||
if ( reporting != null ) | ||
{ | ||
List<ReportPlugin> reportPlugins = reporting.getPlugins(); | ||
for ( ReportPlugin reportPlugin : reportPlugins ) | ||
{ | ||
// Conversion borrowed from | ||
// org.apache.maven.project.MavenProject#getReportArtifacts | ||
Plugin plugin = new Plugin(); | ||
plugin.setGroupId( reportPlugin.getGroupId() ); | ||
plugin.setArtifactId( reportPlugin.getArtifactId() ); | ||
plugin.setVersion( reportPlugin.getVersion() ); | ||
plugins.add( plugin ); | ||
} | ||
} | ||
|
||
Set<Artifact> artifacts = new LinkedHashSet<>(); | ||
artifacts.addAll( reports ); | ||
artifacts.addAll( plugins ); | ||
HashMap<String, Plugin> result = new HashMap<>( plugins.size() ); | ||
for ( Plugin plugin : plugins ) | ||
{ | ||
result.put( plugin.getId(), plugin ); | ||
} | ||
|
||
return UnmodifiableMap.unmodifiableMap( result ); | ||
} | ||
|
||
private Map<String, Artifact> fetchArtifacts( Collection<Plugin> plugins ) | ||
throws ArtifactResolverException, ArtifactFilterException | ||
{ | ||
Set<Artifact> artifacts = new LinkedHashSet<>( plugins.size() ); | ||
for ( Plugin plugin : plugins ) | ||
{ | ||
artifacts.add( | ||
new DefaultArtifact( | ||
plugin.getGroupId(), | ||
plugin.getArtifactId(), | ||
plugin.getVersion(), | ||
Artifact.SCOPE_RUNTIME, | ||
"maven-plugin", | ||
null, | ||
artifactHandlerManager.getArtifactHandler( "maven-plugin" ) | ||
) | ||
); | ||
} | ||
|
||
final FilterArtifacts filter = getArtifactsFilter(); | ||
artifacts = filter.filter( artifacts ); | ||
|
||
Set<Artifact> resolvedArtifacts = new LinkedHashSet<>( artifacts.size() ); | ||
Map<String, Artifact> resolvedArtifacts = new HashMap<>( artifacts.size() ); | ||
// final ArtifactFilter filter = getPluginFilter(); | ||
for ( final Artifact artifact : new LinkedHashSet<>( artifacts ) ) | ||
for ( final Artifact artifact : artifacts ) | ||
{ | ||
// if ( !filter.include( artifact ) ) | ||
// { | ||
|
@@ -193,9 +268,10 @@ protected Set<Artifact> resolvePluginArtifacts() | |
ProjectBuildingRequest buildingRequest = newResolvePluginProjectBuildingRequest(); | ||
|
||
// resolve the new artifact | ||
resolvedArtifacts.add( getArtifactResolver().resolveArtifact( buildingRequest, artifact ).getArtifact() ); | ||
Artifact resolved = getArtifactResolver().resolveArtifact( buildingRequest, artifact ).getArtifact(); | ||
resolvedArtifacts.put( ArtifactUtils.key( resolved ), resolved ); | ||
} | ||
return artifacts; | ||
return resolvedArtifacts; | ||
} | ||
|
||
@Override | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be needed