Centralized repository handling in AbstractVersionTransformation, for consistency (should fix MNG-527).

Changed download strategy for plugins.xml metadata to download only when non-existent locally or when plugin prefix cannot be located within local metadata. NOTE: This could lead to local-only installs of plugins having their prefix mappings overwritten.

Next step is to change the maven-plugin-plugin.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@219615 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-07-19 04:15:26 +00:00
parent c28872c779
commit 6f81043b7b
16 changed files with 294 additions and 164 deletions

View File

@ -23,6 +23,12 @@ public class DefaultRepositoryMetadataManager
// only resolve repository metadata once per session... // only resolve repository metadata once per session...
private Map cachedMetadata = new HashMap(); private Map cachedMetadata = new HashMap();
public void resolveLocally( RepositoryMetadata metadata, ArtifactRepository local )
throws RepositoryMetadataManagementException
{
resolve( metadata, null, local );
}
public void resolve( RepositoryMetadata metadata, ArtifactRepository remote, ArtifactRepository local ) public void resolve( RepositoryMetadata metadata, ArtifactRepository remote, ArtifactRepository local )
throws RepositoryMetadataManagementException throws RepositoryMetadataManagementException
{ {
@ -30,29 +36,25 @@ public class DefaultRepositoryMetadataManager
if ( metadataFile == null ) if ( metadataFile == null )
{ {
metadataFile = constructLocalRepositoryFile( metadata, local, remote.getId() ); metadataFile = constructLocalRepositoryFile( metadata, local );
if ( remote == null ) if ( !metadataFile.exists() && remote != null )
{
throw new RepositoryMetadataManagementException( metadata,
"Cannot retrieve repository metadata from null repository." );
}
else
{ {
try try
{ {
File tempMetadataFile = File.createTempFile( "plugins.xml", null ); File tempMetadataFile = File.createTempFile( "plugins.xml", null );
try try
{ {
wagonManager.getRepositoryMetadata( metadata, remote, tempMetadataFile ); wagonManager.getRepositoryMetadata( metadata, remote, tempMetadataFile );
if( !metadataFile.exists() || ( metadataFile.lastModified() <= tempMetadataFile.lastModified() ) ) if ( !metadataFile.exists()
|| ( metadataFile.lastModified() <= tempMetadataFile.lastModified() ) )
{ {
if ( !tempMetadataFile.renameTo( metadataFile ) ) if ( !tempMetadataFile.renameTo( metadataFile ) )
{ {
FileUtils.copyFile( tempMetadataFile, metadataFile ); FileUtils.copyFile( tempMetadataFile, metadataFile );
tempMetadataFile.delete(); tempMetadataFile.delete();
} }
} }
@ -61,20 +63,18 @@ public class DefaultRepositoryMetadataManager
{ {
if ( !metadataFile.exists() ) if ( !metadataFile.exists() )
{ {
throw new RepositoryMetadataManagementException( metadata, "Remote repository metadata not found.", throw new RepositoryMetadataManagementException( metadata,
e ); "Remote repository metadata not found.", e );
} }
else else
{ {
String message = "Cannot find " + metadata + " in remote repository - Using local copy."; String message = "Cannot find " + metadata + " in remote repository - Using local copy.";
getLogger().info( message ); getLogger().info( message );
getLogger().debug( message, e ); getLogger().debug( message, e );
} }
} }
metadata.setFile( metadataFile );
} }
catch ( TransferFailedException e ) catch ( TransferFailedException e )
{ {
@ -83,9 +83,18 @@ public class DefaultRepositoryMetadataManager
} }
catch ( IOException e ) catch ( IOException e )
{ {
throw new RepositoryMetadataManagementException( metadata, "Error constructing temporary metadata download file.", e ); throw new RepositoryMetadataManagementException(
metadata,
"Error constructing temporary metadata download file.",
e );
} }
} }
else
{
getLogger().info( "Using local copy of " + metadata + " from: " + metadataFile );
}
metadata.setFile( metadataFile );
} }
} }
@ -110,18 +119,7 @@ public class DefaultRepositoryMetadataManager
public void install( RepositoryMetadata metadata, ArtifactRepository local, String remoteRepositoryId ) public void install( RepositoryMetadata metadata, ArtifactRepository local, String remoteRepositoryId )
throws RepositoryMetadataManagementException throws RepositoryMetadataManagementException
{ {
String realignedPath = local.formatAsFile( metadata.getRepositoryPath() ); File metadataFile = constructLocalRepositoryFile( metadata, local );
realignedPath = realignedPath.replace( File.separatorChar, '/' );
if ( !realignedPath.startsWith( "/" ) )
{
realignedPath = "/" + realignedPath;
}
realignedPath = "/REPOSITORY-INF/" + remoteRepositoryId + realignedPath;
File metadataFile = new File( local.getBasedir(), realignedPath ).getAbsoluteFile();
try try
{ {
@ -141,20 +139,27 @@ public class DefaultRepositoryMetadataManager
} }
private File constructLocalRepositoryFile( RepositoryMetadata metadata, ArtifactRepository local, String remoteId ) public void purgeLocalCopy( RepositoryMetadata metadata, ArtifactRepository local )
throws RepositoryMetadataManagementException
{
File metadataFile = constructLocalRepositoryFile( metadata, local );
if ( metadataFile.exists() )
{
if ( !metadataFile.delete() )
{
throw new RepositoryMetadataManagementException( metadata, "Failed to purge local copy from: " + metadataFile );
}
}
}
private File constructLocalRepositoryFile( RepositoryMetadata metadata, ArtifactRepository local )
{ {
String metadataPath = local.formatAsFile( metadata.getRepositoryPath() ); String metadataPath = local.formatAsFile( metadata.getRepositoryPath() );
String realignedPath = metadataPath.replace( File.separatorChar, '/' ); metadataPath = metadataPath.replace( File.separatorChar, '/' );
if ( !realignedPath.startsWith( "/" ) ) return new File( local.getBasedir(), metadataPath );
{
realignedPath = "/" + realignedPath;
}
realignedPath = "/REPOSITORY-INF/" + remoteId + realignedPath;
return new File( local.getBasedir(), realignedPath );
} }
} }

View File

@ -5,6 +5,9 @@ import org.apache.maven.artifact.repository.ArtifactRepository;
public interface RepositoryMetadataManager public interface RepositoryMetadataManager
{ {
void resolveLocally( RepositoryMetadata repositoryMetadata, ArtifactRepository local )
throws RepositoryMetadataManagementException;
void resolve( RepositoryMetadata repositoryMetadata, ArtifactRepository remote, ArtifactRepository local ) void resolve( RepositoryMetadata repositoryMetadata, ArtifactRepository remote, ArtifactRepository local )
throws RepositoryMetadataManagementException; throws RepositoryMetadataManagementException;
@ -13,5 +16,8 @@ public interface RepositoryMetadataManager
void install( RepositoryMetadata repositoryMetadata, ArtifactRepository local, String remoteRepositoryId ) void install( RepositoryMetadata repositoryMetadata, ArtifactRepository local, String remoteRepositoryId )
throws RepositoryMetadataManagementException; throws RepositoryMetadataManagementException;
void purgeLocalCopy( RepositoryMetadata repositoryMetadata, ArtifactRepository local )
throws RepositoryMetadataManagementException;
} }

View File

@ -18,9 +18,11 @@ package org.apache.maven.artifact.transform;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.AbstractVersionArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.VersionArtifactMetadata; import org.apache.maven.artifact.metadata.VersionArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.io.IOException; import java.io.IOException;
@ -101,8 +103,19 @@ public abstract class AbstractVersionTransformation
getLogger().info( getLogger().info(
artifact.getArtifactId() + ": checking for updates from " + remoteRepository.getId() ); artifact.getArtifactId() + ": checking for updates from " + remoteRepository.getId() );
VersionArtifactMetadata remoteMetadata = retrieveFromRemoteRepository( artifact, remoteRepository, localMetadata ); VersionArtifactMetadata remoteMetadata;
try
{
remoteMetadata = retrieveFromRemoteRepository( artifact, remoteRepository, localMetadata );
}
catch ( ResourceDoesNotExistException e )
{
getLogger().debug( "Error resolving artifact version from metadata.", e );
continue;
}
int difference = remoteMetadata.compareTo( localMetadata ); int difference = remoteMetadata.compareTo( localMetadata );
if ( difference > 0 ) if ( difference > 0 )
{ {
@ -124,13 +137,13 @@ public abstract class AbstractVersionTransformation
} }
String version = localMetadata.constructVersion(); String version = localMetadata.constructVersion();
// TODO: if the POM and JAR are inconsistent, this might mean that different version of each are used // TODO: if the POM and JAR are inconsistent, this might mean that different version of each are used
if ( !artifact.getFile().exists() || localMetadata.newerThanFile( artifact.getFile() ) ) if ( !artifact.getFile().exists() || localMetadata.newerThanFile( artifact.getFile() ) )
{ {
if ( getLogger().isInfoEnabled() && !alreadyResolved ) if ( getLogger().isInfoEnabled() && !alreadyResolved )
{ {
if ( !version.equals( artifact.getBaseVersion() ) ) if ( version != null && !version.equals( artifact.getBaseVersion() ) )
{ {
String message = artifact.getArtifactId() + ": resolved to version " + version; String message = artifact.getArtifactId() + ": resolved to version " + version;
if ( artifact.getRepository() != null ) if ( artifact.getRepository() != null )
@ -158,14 +171,27 @@ public abstract class AbstractVersionTransformation
} }
} }
protected abstract VersionArtifactMetadata retrieveFromRemoteRepository( Artifact artifact, protected VersionArtifactMetadata retrieveFromRemoteRepository( Artifact artifact,
ArtifactRepository remoteRepository, ArtifactRepository remoteRepository,
VersionArtifactMetadata localMetadata ) VersionArtifactMetadata localMetadata )
throws ArtifactMetadataRetrievalException; throws ArtifactMetadataRetrievalException, ResourceDoesNotExistException
{
AbstractVersionArtifactMetadata metadata = createMetadata( artifact );
metadata.retrieveFromRemoteRepository( remoteRepository, wagonManager );
return metadata;
}
protected abstract AbstractVersionArtifactMetadata createMetadata( Artifact artifact );
protected abstract VersionArtifactMetadata readFromLocalRepository( Artifact artifact, private VersionArtifactMetadata readFromLocalRepository( Artifact artifact, ArtifactRepository localRepository )
ArtifactRepository localRepository ) throws IOException
throws IOException; {
AbstractVersionArtifactMetadata metadata = createMetadata( artifact );
metadata.readFromLocalRepository( localRepository );
return metadata;
}
private Date getMidnightBoundary() private Date getMidnightBoundary()
{ {

View File

@ -4,11 +4,8 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.AbstractVersionArtifactMetadata; import org.apache.maven.artifact.metadata.AbstractVersionArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.LatestArtifactMetadata; import org.apache.maven.artifact.metadata.LatestArtifactMetadata;
import org.apache.maven.artifact.metadata.VersionArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import java.io.IOException;
import java.util.List; import java.util.List;
public class LatestArtifactTransformation public class LatestArtifactTransformation
@ -22,7 +19,7 @@ public class LatestArtifactTransformation
if ( LATEST_VERSION.equals( artifact.getVersion() ) ) if ( LATEST_VERSION.equals( artifact.getVersion() ) )
{ {
String version = resolveVersion( artifact, localRepository, remoteRepositories ); String version = resolveVersion( artifact, localRepository, remoteRepositories );
if ( !version.equals( artifact.getVersion() ) ) if ( version != null && !version.equals( artifact.getVersion() ) )
{ {
artifact.setBaseVersion( version ); artifact.setBaseVersion( version );
artifact.updateVersion( version, localRepository ); artifact.updateVersion( version, localRepository );
@ -42,32 +39,9 @@ public class LatestArtifactTransformation
// metadata is added at deploy time // metadata is added at deploy time
} }
protected VersionArtifactMetadata retrieveFromRemoteRepository( Artifact artifact, protected AbstractVersionArtifactMetadata createMetadata( Artifact artifact )
ArtifactRepository remoteRepository,
VersionArtifactMetadata localMetadata )
throws ArtifactMetadataRetrievalException
{ {
AbstractVersionArtifactMetadata metadata = new LatestArtifactMetadata( artifact ); return new LatestArtifactMetadata( artifact );
try
{
metadata.retrieveFromRemoteRepository( remoteRepository, wagonManager );
}
catch ( ResourceDoesNotExistException e )
{
if ( localMetadata.constructVersion() == null )
{
throw new ArtifactMetadataRetrievalException( "Unable to find latest version for plugin artifact " + artifact, e );
}
// otherwise, ignore - use the local one
}
return metadata;
} }
protected VersionArtifactMetadata readFromLocalRepository( Artifact artifact, ArtifactRepository localRepository )
throws IOException
{
AbstractVersionArtifactMetadata metadata = new LatestArtifactMetadata( artifact );
metadata.readFromLocalRepository( localRepository );
return metadata;
}
} }

View File

@ -20,11 +20,8 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.AbstractVersionArtifactMetadata; import org.apache.maven.artifact.metadata.AbstractVersionArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ReleaseArtifactMetadata; import org.apache.maven.artifact.metadata.ReleaseArtifactMetadata;
import org.apache.maven.artifact.metadata.VersionArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import java.io.IOException;
import java.util.List; import java.util.List;
/** /**
@ -44,7 +41,8 @@ public class ReleaseArtifactTransformation
if ( RELEASE_VERSION.equals( artifact.getVersion() ) ) if ( RELEASE_VERSION.equals( artifact.getVersion() ) )
{ {
String version = resolveVersion( artifact, localRepository, remoteRepositories ); String version = resolveVersion( artifact, localRepository, remoteRepositories );
if ( !version.equals( artifact.getVersion() ) )
if ( version != null && !version.equals( artifact.getVersion() ) )
{ {
artifact.setBaseVersion( version ); artifact.setBaseVersion( version );
artifact.updateVersion( version, localRepository ); artifact.updateVersion( version, localRepository );
@ -64,32 +62,9 @@ public class ReleaseArtifactTransformation
// metadata is added at deploy time // metadata is added at deploy time
} }
protected VersionArtifactMetadata retrieveFromRemoteRepository( Artifact artifact, protected AbstractVersionArtifactMetadata createMetadata( Artifact artifact )
ArtifactRepository remoteRepository,
VersionArtifactMetadata localMetadata )
throws ArtifactMetadataRetrievalException
{ {
AbstractVersionArtifactMetadata metadata = new ReleaseArtifactMetadata( artifact ); return new ReleaseArtifactMetadata( artifact );
try
{
metadata.retrieveFromRemoteRepository( remoteRepository, wagonManager );
}
catch ( ResourceDoesNotExistException e )
{
if ( localMetadata.constructVersion() == null )
{
throw new ArtifactMetadataRetrievalException( "Unable to find release for artifact " + artifact, e );
}
// otherwise, ignore - use the local one
}
return metadata;
} }
protected VersionArtifactMetadata readFromLocalRepository( Artifact artifact, ArtifactRepository localRepository )
throws IOException
{
AbstractVersionArtifactMetadata metadata = new ReleaseArtifactMetadata( artifact );
metadata.readFromLocalRepository( localRepository );
return metadata;
}
} }

View File

@ -17,13 +17,12 @@ package org.apache.maven.artifact.transform;
*/ */
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.AbstractVersionArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.SnapshotArtifactMetadata; import org.apache.maven.artifact.metadata.SnapshotArtifactMetadata;
import org.apache.maven.artifact.metadata.VersionArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.wagon.ResourceDoesNotExistException; import org.apache.maven.wagon.ResourceDoesNotExistException;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -81,7 +80,16 @@ public class SnapshotTransformation
else if ( isSnapshot( artifact ) ) else if ( isSnapshot( artifact ) )
{ {
SnapshotArtifactMetadata metadata = null; SnapshotArtifactMetadata metadata = null;
metadata = (SnapshotArtifactMetadata) retrieveFromRemoteRepository( artifact, remoteRepository, null );
try
{
metadata = (SnapshotArtifactMetadata) retrieveFromRemoteRepository( artifact, remoteRepository, null );
}
catch ( ResourceDoesNotExistException e )
{
// ignore. We'll be creating this metadata if it doesn't exist...
}
metadata.update(); metadata.update();
artifact.setVersion( metadata.constructVersion() ); artifact.setVersion( metadata.constructVersion() );
@ -95,29 +103,9 @@ public class SnapshotTransformation
return artifact.getVersion().endsWith( SNAPSHOT_VERSION ); return artifact.getVersion().endsWith( SNAPSHOT_VERSION );
} }
protected VersionArtifactMetadata retrieveFromRemoteRepository( Artifact artifact, protected AbstractVersionArtifactMetadata createMetadata( Artifact artifact )
ArtifactRepository remoteRepository,
VersionArtifactMetadata localMetadata )
throws ArtifactMetadataRetrievalException
{ {
SnapshotArtifactMetadata metadata = new SnapshotArtifactMetadata( artifact ); return new SnapshotArtifactMetadata( artifact );
try
{
metadata.retrieveFromRemoteRepository( remoteRepository, wagonManager );
}
catch ( ResourceDoesNotExistException e )
{
// No problem...
// this just means that there is no snapshot version file, so we keep timestamp = null, build = 0
}
return metadata;
} }
protected VersionArtifactMetadata readFromLocalRepository( Artifact artifact, ArtifactRepository localRepository )
throws IOException
{
SnapshotArtifactMetadata metadata = new SnapshotArtifactMetadata( artifact );
metadata.readFromLocalRepository( localRepository );
return metadata;
}
} }

View File

@ -493,8 +493,8 @@ public class Verifier
cli.setExecutable( executable ); cli.setExecutable( executable );
// cli.createArgument().setValue( "-e" ); cli.createArgument().setValue( "-e" );
cli.createArgument().setValue( "-X" ); // cli.createArgument().setValue( "-X" );
cli.createArgument().setValue( "--no-plugin-registry" ); cli.createArgument().setValue( "--no-plugin-registry" );

View File

@ -570,7 +570,26 @@ public class DefaultLifecycleExecutor
// Steps for retrieving the plugin model instance: // Steps for retrieving the plugin model instance:
// 1. request directly from the plugin collector by prefix // 1. request directly from the plugin collector by prefix
pluginDescriptor = pluginManager.getPluginDescriptorForPrefix( prefix ); try
{
pluginDescriptor = pluginManager.getPluginDescriptorForPrefix( prefix );
}
catch ( PluginManagerException e )
{
throw new LifecycleExecutionException( "Cannot resolve plugin-prefix: \'" + prefix + "\' from plugin collector.", e );
}
if ( pluginDescriptor == null )
{
try
{
plugin = pluginManager.getPluginDefinitionForPrefix( prefix, session, project );
}
catch ( PluginManagerException e )
{
throw new LifecycleExecutionException( "Cannot resolve plugin-prefix: \'" + prefix + "\' from plugin mappings metadata.", e );
}
}
if ( pluginDescriptor != null ) if ( pluginDescriptor != null )
{ {
@ -581,15 +600,7 @@ public class DefaultLifecycleExecutor
plugin.setVersion( pluginDescriptor.getVersion() ); plugin.setVersion( pluginDescriptor.getVersion() );
} }
// 2. use the plugin resolver to resolve the prefix in the search groups // 2. default to o.a.m.plugins and maven-<prefix>-plugin
if ( plugin == null )
{
PluginMappingManager mappingManager = getPluginMappingManager( session, project );
plugin = mappingManager.getByPrefix( prefix );
}
// 3. default to o.a.m.plugins and maven-<prefix>-plugin
if ( plugin == null ) if ( plugin == null )
{ {
plugin = new Plugin(); plugin = new Plugin();

View File

@ -21,6 +21,7 @@ import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ResolutionGroup; import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManagementException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult; import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.artifact.resolver.ArtifactResolver;
@ -40,6 +41,9 @@ import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder; import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.mapping.MavenPluginMappingBuilder;
import org.apache.maven.plugin.mapping.PluginMappingManagementException;
import org.apache.maven.plugin.mapping.PluginMappingManager;
import org.apache.maven.plugin.version.PluginVersionManager; import org.apache.maven.plugin.version.PluginVersionManager;
import org.apache.maven.plugin.version.PluginVersionResolutionException; import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
@ -67,6 +71,7 @@ import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
@ -74,7 +79,6 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.lang.reflect.Field;
public class DefaultPluginManager public class DefaultPluginManager
extends AbstractLogEnabled extends AbstractLogEnabled
@ -100,6 +104,8 @@ public class DefaultPluginManager
protected ArtifactResolver artifactResolver; protected ArtifactResolver artifactResolver;
protected MavenProjectBuilder mavenProjectBuilder; protected MavenProjectBuilder mavenProjectBuilder;
protected MavenPluginMappingBuilder pluginMappingBuilder;
// END component requirements // END component requirements
public DefaultPluginManager() public DefaultPluginManager()
@ -112,9 +118,41 @@ public class DefaultPluginManager
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
public PluginDescriptor getPluginDescriptorForPrefix( String prefix ) public PluginDescriptor getPluginDescriptorForPrefix( String prefix )
throws PluginManagerException
{ {
return pluginCollector.getPluginDescriptorForPrefix( prefix ); return pluginCollector.getPluginDescriptorForPrefix( prefix );
} }
public Plugin getPluginDefinitionForPrefix( String prefix, MavenSession session, MavenProject project ) throws PluginManagerException
{
PluginMappingManager mappingManager = getPluginMappingManager( session, project );
Plugin plugin = mappingManager.getByPrefix( prefix );
if ( plugin == null && !mappingManager.isRefreshed() )
{
getLogger().info( "Refreshing plugin mapping metadata; looking for plugin with prefix: \'" + prefix + "\'." );
try
{
mappingManager = pluginMappingBuilder.refreshPluginMappingManager( session
.getPluginMappingManager(), project.getPluginArtifactRepositories(), session
.getLocalRepository() );
}
catch ( RepositoryMetadataManagementException e )
{
throw new PluginManagerException( "Error refreshing plugin mappings.", e );
}
catch ( PluginMappingManagementException e )
{
throw new PluginManagerException( "Error refreshing plugin mappings.", e );
}
plugin = mappingManager.getByPrefix( prefix );
}
return plugin;
}
public PluginDescriptor verifyPlugin( Plugin plugin, MavenProject project, Settings settings, public PluginDescriptor verifyPlugin( Plugin plugin, MavenProject project, Settings settings,
ArtifactRepository localRepository ) ArtifactRepository localRepository )
@ -929,4 +967,36 @@ public class DefaultPluginManager
return pluginContainer.lookup( role, roleHint ); return pluginContainer.lookup( role, roleHint );
} }
private PluginMappingManager getPluginMappingManager( MavenSession session, MavenProject project )
throws PluginManagerException
{
PluginMappingManager mappingManager = session.getPluginMappingManager();
// don't reassemble the plugin mappings if the session has already been configured with them.
if ( mappingManager == null )
{
try
{
List pluginGroupIds = session.getSettings().getPluginGroups();
List pluginRepositories = project.getPluginArtifactRepositories();
ArtifactRepository localRepository = session.getLocalRepository();
mappingManager = pluginMappingBuilder.loadPluginMappings( pluginGroupIds, pluginRepositories,
localRepository );
// lazily configure this on the session.
session.setPluginMappingManager( mappingManager );
}
catch ( RepositoryMetadataManagementException e )
{
throw new PluginManagerException( "Cannot load plugin mappings.", e );
}
catch ( PluginMappingManagementException e )
{
throw new PluginManagerException( "Cannot load plugin mappings.", e );
}
}
return mappingManager;
}
} }

View File

@ -41,8 +41,12 @@ public interface PluginManager
void executeMojo( MavenProject project, MojoExecution execution, MavenSession session ) void executeMojo( MavenProject project, MojoExecution execution, MavenSession session )
throws MojoExecutionException, PluginManagerException, ArtifactResolutionException; throws MojoExecutionException, PluginManagerException, ArtifactResolutionException;
PluginDescriptor getPluginDescriptorForPrefix( String prefix ); PluginDescriptor getPluginDescriptorForPrefix( String prefix )
throws PluginManagerException;
Plugin getPluginDefinitionForPrefix( String prefix, MavenSession session, MavenProject project )
throws PluginManagerException;
PluginDescriptor verifyPlugin( Plugin plugin, MavenProject project, Settings settings, PluginDescriptor verifyPlugin( Plugin plugin, MavenProject project, Settings settings,
ArtifactRepository localRepository ) ArtifactRepository localRepository )
throws ArtifactResolutionException, PluginManagerException, PluginVersionResolutionException; throws ArtifactResolutionException, PluginManagerException, PluginVersionResolutionException;

View File

@ -22,6 +22,9 @@
<requirement> <requirement>
<role>org.apache.maven.project.MavenProjectBuilder</role> <role>org.apache.maven.project.MavenProjectBuilder</role>
</requirement> </requirement>
<requirement>
<role>org.apache.maven.plugin.mapping.MavenPluginMappingBuilder</role>
</requirement>
</requirements> </requirements>
</component> </component>

View File

@ -29,14 +29,50 @@ public class DefaultPluginMappingBuilder
ArtifactRepository localRepository ) ArtifactRepository localRepository )
throws RepositoryMetadataManagementException, PluginMappingManagementException throws RepositoryMetadataManagementException, PluginMappingManagementException
{ {
List pluginGroupIds = new ArrayList( groupIds ); return loadPluginMappings( groupIds, pluginRepositories, localRepository, new PluginMappingManager() );
}
public PluginMappingManager refreshPluginMappingManager( PluginMappingManager mappingManager, List pluginRepositories,
ArtifactRepository localRepository )
throws RepositoryMetadataManagementException, PluginMappingManagementException
{
// prevent performance drag from abuse of this method.
if ( mappingManager.isRefreshed() )
{
throw new PluginMappingManagementException( "Plugin-mappings have already been refreshed. Cannot re-refresh." );
}
getLogger().info( "Refreshing plugin-mapping metadata..." );
List groupIds = new ArrayList();
for ( Iterator it = mappingManager.getPluginMaps().iterator(); it.hasNext(); )
{
PluginMap map = (PluginMap) it.next();
String groupId = map.getGroupId();
groupIds.add( groupId );
repositoryMetadataManager.purgeLocalCopy( new PluginMappingMetadata( groupId ), localRepository );
}
mappingManager.markRefreshed();
return loadPluginMappings(groupIds, pluginRepositories, localRepository, mappingManager);
}
private PluginMappingManager loadPluginMappings( List groupIds, List pluginRepositories,
ArtifactRepository localRepository,
PluginMappingManager mappingManager )
throws RepositoryMetadataManagementException, PluginMappingManagementException
{
List pluginGroupIds = new ArrayList( groupIds );
if ( !pluginGroupIds.contains( "org.apache.maven.plugins" ) ) if ( !pluginGroupIds.contains( "org.apache.maven.plugins" ) )
{ {
pluginGroupIds.add( "org.apache.maven.plugins" ); pluginGroupIds.add( "org.apache.maven.plugins" );
} }
PluginMappingManager mappingManager = new PluginMappingManager();
if ( pluginGroupIds != null ) if ( pluginGroupIds != null )
{ {
@ -57,8 +93,9 @@ public class DefaultPluginMappingBuilder
} }
catch ( RepositoryMetadataManagementException e ) catch ( RepositoryMetadataManagementException e )
{ {
getLogger().warn( "Cannot resolve plugin-mapping metadata for groupId: " + groupId + " - IGNORING." ); getLogger()
.warn( "Cannot resolve plugin-mapping metadata for groupId: " + groupId + " - IGNORING." );
getLogger().debug( "Error resolving plugin-mapping metadata for groupId: " + groupId + ".", e ); getLogger().debug( "Error resolving plugin-mapping metadata for groupId: " + groupId + ".", e );
} }
} }
@ -67,9 +104,10 @@ public class DefaultPluginMappingBuilder
return mappingManager; return mappingManager;
} }
private PluginMap readPluginMap( File mappingFile ) throws PluginMappingManagementException private PluginMap readPluginMap( File mappingFile )
throws PluginMappingManagementException
{ {
if( mappingFile.exists() ) if ( mappingFile.exists() )
{ {
Reader fileReader = null; Reader fileReader = null;
try try
@ -77,8 +115,8 @@ public class DefaultPluginMappingBuilder
fileReader = new FileReader( mappingFile ); fileReader = new FileReader( mappingFile );
PluginMappingXpp3Reader mappingReader = new PluginMappingXpp3Reader(); PluginMappingXpp3Reader mappingReader = new PluginMappingXpp3Reader();
return mappingReader.read(fileReader); return mappingReader.read( fileReader );
} }
catch ( IOException e ) catch ( IOException e )
{ {
@ -113,7 +151,7 @@ public class DefaultPluginMappingBuilder
try try
{ {
repositoryMetadataManager.resolve( metadata, repository, localRepository ); repositoryMetadataManager.resolve( metadata, repository, localRepository );
// reset this to keep it from getting in the way when we succeed but not on first repo... // reset this to keep it from getting in the way when we succeed but not on first repo...
repositoryException = null; repositoryException = null;

View File

@ -12,4 +12,8 @@ public interface MavenPluginMappingBuilder
ArtifactRepository localRepository ) ArtifactRepository localRepository )
throws RepositoryMetadataManagementException, PluginMappingManagementException; throws RepositoryMetadataManagementException, PluginMappingManagementException;
PluginMappingManager refreshPluginMappingManager( PluginMappingManager mappingManager, List pluginRepositories,
ArtifactRepository localRepository )
throws RepositoryMetadataManagementException, PluginMappingManagementException;
} }

View File

@ -12,6 +12,7 @@ public class PluginMappingManager
{ {
private List mappings = new ArrayList(); private List mappings = new ArrayList();
private boolean refreshed = false;
private Map pluginDefinitionsByPrefix = new HashMap(); private Map pluginDefinitionsByPrefix = new HashMap();
private Map pluginDefinitionsByPackaging = new HashMap(); private Map pluginDefinitionsByPackaging = new HashMap();
@ -20,9 +21,34 @@ public class PluginMappingManager
{ {
mappings.add( pluginMap ); mappings.add( pluginMap );
// flush the cache. clearCache();
pluginDefinitionsByPrefix = null; }
pluginDefinitionsByPackaging = null;
public void markRefreshed()
{
this.refreshed = true;
}
public boolean isRefreshed()
{
return refreshed;
}
public List getPluginMaps()
{
return mappings;
}
public void clear()
{
this.mappings = null;
clearCache();
}
private void clearCache()
{
this.pluginDefinitionsByPackaging = null;
this.pluginDefinitionsByPrefix = null;
} }
public Plugin getByPrefix( String pluginPrefix ) public Plugin getByPrefix( String pluginPrefix )

View File

@ -17,7 +17,7 @@ public class PluginMappingMetadata
{ {
this.groupId = groupId; this.groupId = groupId;
} }
public String getRepositoryPath() public String getRepositoryPath()
{ {
return groupId + "/" + PLUGIN_MAPPING_FILE; return groupId + "/" + PLUGIN_MAPPING_FILE;

View File

@ -58,8 +58,8 @@ public class DefaultPluginRegistryBuilder
globalRegistryFile = getFile( globalRegistryPath, "maven.home", MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION ); globalRegistryFile = getFile( globalRegistryPath, "maven.home", MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION );
getLogger().debug( "Building Maven global-level settings from: '" + globalRegistryFile.getAbsolutePath() + "'" ); getLogger().debug( "Building Maven global-level plugin registry from: '" + globalRegistryFile.getAbsolutePath() + "'" );
getLogger().debug( "Building Maven user-level settings from: '" + userRegistryFile.getAbsolutePath() + "'" ); getLogger().debug( "Building Maven user-level plugin registry from: '" + userRegistryFile.getAbsolutePath() + "'" );
} }
public PluginRegistry buildPluginRegistry() public PluginRegistry buildPluginRegistry()