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;