From 5507063fc2966893fc68c2b61ed8a737c9a4ac3a Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Thu, 25 Mar 2010 21:55:08 +0000 Subject: [PATCH] o Aligned version resolver with prefix resolver and made it support a model as additional input git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@927604 13f79535-47bb-0310-9956-ffa450edef68 --- .../version/DefaultPluginVersionRequest.java | 15 ++++ .../plugin/version/PluginVersionRequest.java | 18 +++++ .../DefaultPluginVersionResolver.java | 81 +++++++++++++++++-- .../internal/DefaultPluginVersionResult.java | 10 +++ 4 files changed, 117 insertions(+), 7 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/plugin/version/DefaultPluginVersionRequest.java b/maven-core/src/main/java/org/apache/maven/plugin/version/DefaultPluginVersionRequest.java index 77c466e29c..f4422b896d 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/version/DefaultPluginVersionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/version/DefaultPluginVersionRequest.java @@ -26,6 +26,7 @@ import org.apache.maven.artifact.repository.DefaultRepositoryRequest; import org.apache.maven.artifact.repository.RepositoryCache; import org.apache.maven.artifact.repository.RepositoryRequest; import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; import org.apache.maven.project.MavenProject; import org.apache.maven.repository.ArtifactTransferListener; @@ -43,6 +44,8 @@ public class DefaultPluginVersionRequest private String artifactId; + private Model pom; + private RepositoryRequest repositoryRequest; /** @@ -126,6 +129,18 @@ public class DefaultPluginVersionRequest return this; } + public Model getPom() + { + return pom; + } + + public DefaultPluginVersionRequest setPom( Model pom ) + { + this.pom = pom; + + return this; + } + public RepositoryCache getCache() { return repositoryRequest.getCache(); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/version/PluginVersionRequest.java b/maven-core/src/main/java/org/apache/maven/plugin/version/PluginVersionRequest.java index 087212faf0..3774cf6e0a 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/version/PluginVersionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/version/PluginVersionRequest.java @@ -24,6 +24,7 @@ import java.util.List; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.RepositoryCache; import org.apache.maven.artifact.repository.RepositoryRequest; +import org.apache.maven.model.Model; import org.apache.maven.repository.ArtifactTransferListener; /** @@ -65,6 +66,23 @@ public interface PluginVersionRequest */ PluginVersionRequest setArtifactId( String artifactId ); + /** + * Gets the POM whose build plugins are to be scanned for the version. + * + * @return The POM whose build plugins are to be scanned for the verion or {@code null} to only search the plugin + * repositories. + */ + Model getPom(); + + /** + * Sets the POM whose build plugins are to be scanned for the version. + * + * @param pom The POM whose build plugins are to be scanned for the version, may be {@code null} to only search the + * plugin repositories. + * @return This request, never {@code null}. + */ + PluginVersionRequest setPom( Model pom ); + /** * Indicates whether network access to remote repositories has been disabled. * diff --git a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java index 39b74b6e93..8dcea96dfb 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResolver.java @@ -22,11 +22,14 @@ package org.apache.maven.plugin.version.internal; import java.io.File; import java.io.IOException; import java.util.Collections; +import java.util.List; import java.util.Map; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.metadata.Metadata; import org.apache.maven.artifact.repository.metadata.io.MetadataReader; +import org.apache.maven.model.Build; +import org.apache.maven.model.Plugin; import org.apache.maven.plugin.version.PluginVersionRequest; import org.apache.maven.plugin.version.PluginVersionResolutionException; import org.apache.maven.plugin.version.PluginVersionResolver; @@ -63,6 +66,38 @@ public class DefaultPluginVersionResolver { logger.debug( "Resolving plugin version for " + request.getGroupId() + ":" + request.getArtifactId() ); + PluginVersionResult result = resolveFromProject( request ); + + if ( result == null ) + { + result = resolveFromRepository( request ); + + if ( StringUtils.isEmpty( result.getVersion() ) ) + { + throw new PluginVersionResolutionException( request.getGroupId(), request.getArtifactId(), + request.getLocalRepository(), + request.getRemoteRepositories(), + "Plugin not found in any plugin repository" ); + } + else if ( logger.isDebugEnabled() ) + { + logger.debug( "Resolved plugin version for " + request.getGroupId() + ":" + request.getArtifactId() + + " to " + result.getVersion() + " from repository " + + ( result.getRepository() != null ? result.getRepository().getId() : "null" ) ); + } + } + else if ( logger.isDebugEnabled() ) + { + logger.debug( "Resolved plugin version for " + request.getGroupId() + ":" + request.getArtifactId() + + " to " + result.getVersion() + " from POM " + request.getPom() ); + } + + return result; + } + + private PluginVersionResult resolveFromRepository( PluginVersionRequest request ) + throws PluginVersionResolutionException + { DefaultPluginVersionResult result = new DefaultPluginVersionResult(); Metadata mergedMetadata = new Metadata(); @@ -155,13 +190,6 @@ public class DefaultPluginVersionResolver "Plugin not found in any plugin repository" ); } - if ( logger.isDebugEnabled() ) - { - logger.debug( "Resolved plugin version for " + request.getGroupId() + ":" + request.getArtifactId() - + " to " + result.getVersion() + " from repository " - + ( result.getRepository() != null ? result.getRepository().getId() : "null" ) ); - } - return result; } @@ -209,4 +237,43 @@ public class DefaultPluginVersionResolver return target.merge( source ); } + private PluginVersionResult resolveFromProject( PluginVersionRequest request ) + { + PluginVersionResult result = null; + + if ( request.getPom() != null && request.getPom().getBuild() != null ) + { + Build build = request.getPom().getBuild(); + + result = resolveFromProject( request, build.getPlugins() ); + + if ( result == null && build.getPluginManagement() != null ) + { + result = resolveFromProject( request, build.getPluginManagement().getPlugins() ); + } + } + + return result; + } + + private PluginVersionResult resolveFromProject( PluginVersionRequest request, List plugins ) + { + for ( Plugin plugin : plugins ) + { + if ( request.getGroupId().equals( plugin.getGroupId() ) + && request.getArtifactId().equals( plugin.getArtifactId() ) ) + { + if ( plugin.getVersion() != null ) + { + return new DefaultPluginVersionResult( plugin.getVersion() ); + } + else + { + return null; + } + } + } + return null; + } + } diff --git a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResult.java b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResult.java index 24116a1cb9..3277d1ffd3 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResult.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/version/internal/DefaultPluginVersionResult.java @@ -35,6 +35,16 @@ class DefaultPluginVersionResult private ArtifactRepository repository; + public DefaultPluginVersionResult() + { + // does nothing + } + + public DefaultPluginVersionResult( String version ) + { + this.version = version; + } + public String getVersion() { return version;