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
This commit is contained in:
Benjamin Bentmann 2010-03-25 21:55:08 +00:00
parent 51d640d13b
commit 5507063fc2
4 changed files with 117 additions and 7 deletions

View File

@ -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();

View File

@ -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.
*

View File

@ -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<Plugin> 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;
}
}

View File

@ -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;