[MNG-377] Resolving.

Added new mojos to the plugin-plugin that will update the plugins.xml mapping metadata in the plugin's group on the distribution repository.




git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@210153 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-07-11 16:13:33 +00:00
parent 81afd82296
commit 03a50c1462
19 changed files with 485 additions and 43 deletions

View File

@ -104,7 +104,7 @@ public class DefaultWagonManager
{
getLogger().info( "Uploading " + metadata );
putRemoteFile( repository, source, metadata.getRepositoryPath(), null );
putRemoteFile( repository, source, repository.formatAsFile( metadata.getRepositoryPath() ), null );
}
private void putRemoteFile( ArtifactRepository repository, File source, String remotePath,
@ -254,7 +254,7 @@ public class DefaultWagonManager
public void getRepositoryMetadata( RepositoryMetadata metadata, ArtifactRepository remoteRepository, File destination )
throws TransferFailedException, ResourceDoesNotExistException
{
String remotePath = metadata.getRepositoryPath();
String remotePath = remoteRepository.formatAsFile( metadata.getRepositoryPath() );
getLogger().info( "Retrieving " + metadata );

View File

@ -64,9 +64,14 @@ public class DefaultArtifactRepository
return layout.pathOfMetadata( artifactMetadata );
}
public String formatDirectory( String directory )
public String formatAsDirectory( String directory )
{
return layout.formatDirectory( directory );
return layout.formatAsDirectory( directory );
}
public String formatAsFile( String file )
{
return layout.formatAsFile( file );
}
public String getSnapshotPolicy()

View File

@ -4,49 +4,81 @@ import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.TransferFailedException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.IOException;
public class DefaultRepositoryMetadataManager
extends AbstractLogEnabled
implements RepositoryMetadataManager
{
// component requirement
private WagonManager wagonManager;
public void get( RepositoryMetadata metadata, ArtifactRepository remote, ArtifactRepository local )
public void resolve( RepositoryMetadata metadata, ArtifactRepository remote, ArtifactRepository local, String remoteId )
throws RepositoryMetadataManagementException
{
String realignedPath = local.formatDirectory( metadata.getRepositoryPath() );
String metadataPath = local.formatAsFile( metadata.getRepositoryPath() );
realignedPath = realignedPath.replace( File.separatorChar, '/' );
String realignedPath = metadataPath.replace( File.separatorChar, '/' );
if ( !realignedPath.startsWith( "/" ) )
{
realignedPath = "/" + realignedPath;
}
realignedPath = "/REPOSITORY-INF/" + remote.getId() + realignedPath;
realignedPath = "/REPOSITORY-INF/" + remoteId + realignedPath;
File metadataFile = new File( local.getBasedir(), realignedPath );
try
if ( remote == null )
{
wagonManager.getRepositoryMetadata( metadata, remote, metadataFile );
if ( metadataFile.exists() )
{
getLogger().warn( "Cannot retrieve repository metadata for: " + metadataPath + ". Using locally cached version instead." );
getLogger().debug( "Error retrieving repository metadata: " + metadataPath + ". Reason: repository is null." );
metadata.setFile( metadataFile );
}
else
{
throw new RepositoryMetadataManagementException( metadata, "Cannot retrieve repository metadata from null repository." );
}
}
else
{
try
{
wagonManager.getRepositoryMetadata( metadata, remote, metadataFile );
metadata.setFile( metadataFile );
}
catch ( TransferFailedException e )
{
throw new RepositoryMetadataManagementException( metadata, "Failed to download repository metadata.", e );
}
catch ( ResourceDoesNotExistException e )
{
throw new RepositoryMetadataManagementException( metadata, "Remote repository metadata not found.", e );
metadata.setFile( metadataFile );
}
catch ( TransferFailedException e )
{
throw new RepositoryMetadataManagementException( metadata, "Failed to download repository metadata.", e );
}
catch ( ResourceDoesNotExistException e )
{
if ( metadataFile.exists() )
{
getLogger().warn( "Cannot find repository metadata for: " + metadataPath + ". Using locally cached version instead." );
getLogger().debug( "Error retrieving repository metadata: " + metadataPath, e );
metadata.setFile( metadataFile );
}
else
{
throw new RepositoryMetadataManagementException( metadata, "Remote repository metadata not found.", e );
}
}
}
}
public void put( RepositoryMetadata metadata, ArtifactRepository remote )
public void deploy( RepositoryMetadata metadata, ArtifactRepository remote )
throws RepositoryMetadataManagementException
{
File metadataFile = metadata.getFile();
@ -64,4 +96,38 @@ public class DefaultRepositoryMetadataManager
}
public void install( RepositoryMetadata metadata, ArtifactRepository local, String remoteRepositoryId )
throws RepositoryMetadataManagementException
{
String realignedPath = local.formatAsFile( metadata.getRepositoryPath() );
realignedPath = realignedPath.replace( File.separatorChar, '/' );
if ( !realignedPath.startsWith( "/" ) )
{
realignedPath = "/" + realignedPath;
}
realignedPath = "/REPOSITORY-INF/" + remoteRepositoryId + realignedPath;
File metadataFile = new File( local.getBasedir(), realignedPath ).getAbsoluteFile();
try
{
File dir = metadataFile.getParentFile();
if ( !dir.exists() )
{
dir.mkdirs();
}
FileUtils.copyFile( metadata.getFile(), metadataFile );
}
catch ( IOException e )
{
throw new RepositoryMetadataManagementException( metadata, "Failed to install repository metadata.", e );
}
}
}

View File

@ -47,7 +47,9 @@ public interface ArtifactRepository
String pathOfMetadata( ArtifactMetadata artifactMetadata );
String formatDirectory( String directory );
String formatAsDirectory( String directory );
String formatAsFile( String file );
String getUrl();

View File

@ -31,5 +31,7 @@ public interface ArtifactRepositoryLayout
String pathOfMetadata( ArtifactMetadata metadata );
String formatDirectory( String directory );
String formatAsDirectory( String directory );
String formatAsFile( String file );
}

View File

@ -33,7 +33,7 @@ public class DefaultRepositoryLayout
StringBuffer path = new StringBuffer();
path.append( formatDirectory( artifact.getGroupId() ) ).append( '/' );
path.append( formatAsDirectory( artifact.getGroupId() ) ).append( '/' );
path.append( artifact.getArtifactId() ).append( '/' );
path.append( artifact.getBaseVersion() ).append( '/' );
path.append( artifact.getArtifactId() ).append( '-' ).append( artifact.getVersion() );
@ -55,7 +55,7 @@ public class DefaultRepositoryLayout
{
StringBuffer path = new StringBuffer();
path.append( formatDirectory( metadata.getGroupId() ) ).append( '/' );
path.append( formatAsDirectory( metadata.getGroupId() ) ).append( '/' );
path.append( metadata.getArtifactId() ).append( '/' );
if ( !metadata.getBaseVersion().equals( "RELEASE" ) )
{
@ -67,8 +67,26 @@ public class DefaultRepositoryLayout
return path.toString();
}
public String formatDirectory( String directory )
public String formatAsDirectory( String directory )
{
return directory.replace( '.', '/' );
}
public String formatAsFile( String file )
{
int lastSlash = file.lastIndexOf('/');
if( lastSlash > -1 )
{
String filePart = file.substring( lastSlash );
String dirPart = file.substring( 0, lastSlash );
return dirPart.replace('.', '/') + filePart;
}
else
{
return file;
}
}
}

View File

@ -59,9 +59,13 @@ public class LegacyRepositoryLayout
return path.toString();
}
public String formatDirectory( String directory )
public String formatAsDirectory( String directory )
{
return directory;
}
public String formatAsFile( String file )
{
return file;
}
}

View File

@ -7,8 +7,8 @@ public interface RepositoryMetadata
String getRepositoryPath();
void setFile( File metadataFile );
File getFile();
void setFile( File file );
}

View File

@ -6,13 +6,6 @@ public class RepositoryMetadataManagementException
private final RepositoryMetadata metadata;
public RepositoryMetadataManagementException( RepositoryMetadata metadata )
{
super( "Failed to resolve repository metadata: " + metadata + ".");
this.metadata = metadata;
}
public RepositoryMetadataManagementException( RepositoryMetadata metadata, String message, Throwable cause )
{
super( "Failed to resolve repository metadata: " + metadata + ". Error was: " + cause.getMessage(), cause );
@ -22,11 +15,11 @@ public class RepositoryMetadataManagementException
public RepositoryMetadataManagementException( RepositoryMetadata metadata, String message )
{
super( "Failed to resolve repository metadata: " + metadata + ".");
super( "Failed to resolve repository metadata: " + metadata + "." );
this.metadata = metadata;
}
public RepositoryMetadata getMetadata()
{
return metadata;

View File

@ -5,10 +5,13 @@ import org.apache.maven.artifact.repository.ArtifactRepository;
public interface RepositoryMetadataManager
{
void get( RepositoryMetadata repositoryMetadata, ArtifactRepository remote, ArtifactRepository local )
void resolve( RepositoryMetadata repositoryMetadata, ArtifactRepository remote, ArtifactRepository local, String remoteRepositoryId )
throws RepositoryMetadataManagementException;
void put( RepositoryMetadata repositoryMetadata, ArtifactRepository remote )
void deploy( RepositoryMetadata repositoryMetadata, ArtifactRepository remote )
throws RepositoryMetadataManagementException;
void install( RepositoryMetadata repositoryMetadata, ArtifactRepository local, String remoteRepositoryId )
throws RepositoryMetadataManagementException;
}

View File

@ -193,8 +193,8 @@
<test-compile>compiler:testCompile</test-compile>
<test>surefire:test</test>
<package>jar:jar</package>
<install>install:install</install>
<deploy>deploy:deploy</deploy>
<install>install:install,plugin:install-mapping</install>
<deploy>deploy:deploy,plugin:deploy-mapping</deploy>
</phases>
<!-- END SNIPPET: maven-plugin-lifecycle -->
</configuration>

View File

@ -93,7 +93,7 @@ public class DefaultPluginMappingBuilder
try
{
repositoryMetadataManager.get( metadata, repository, localRepository );
repositoryMetadataManager.resolve( metadata, repository, localRepository, repository.getId() );
break;
}

View File

@ -32,5 +32,10 @@ public class PluginMappingMetadata
{
return metadataFile;
}
public String toString()
{
return PLUGIN_MAPPING_FILE + " (plugin mappings) for group: \'" + groupId + "\'";
}
}

View File

@ -34,5 +34,25 @@
<version>2.0-beta-1-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0-beta-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0-beta-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact-manager</artifactId>
<version>2.0-beta-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-mapping</artifactId>
<version>2.0-beta-1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -50,6 +50,8 @@ public abstract class AbstractGeneratorMojo
/**
* The goal prefix that will appear before the ":".
*
* @parameter
*/
protected String goalPrefix;

View File

@ -0,0 +1,159 @@
package org.apache.maven.plugin.plugin.metadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManagementException;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import java.io.File;
public abstract class AbstractMetadataPublisherMojo
extends AbstractMojo
{
/**
* @parameter
*/
private String goalPrefix;
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
/**
* @parameter expression="${localRepository}"
* @required
* @readonly
*/
private ArtifactRepository localRepository;
/**
* @parameter expression="${project.build.directory}/repository-metadata"
* @required
* @readonly
*/
private String outputDirectory;
/**
* @parameter expression="${component.org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager}"
* @required
* @readonly
*/
private RepositoryMetadataManager repositoryMetadataManager;
protected String getGroupId()
{
return project.getGroupId();
}
protected String getArtifactId()
{
return project.getArtifactId();
}
protected String getGoalPrefix()
{
if ( goalPrefix == null )
{
goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( getArtifactId() );
}
return goalPrefix;
}
protected void publish( boolean doDeploy )
throws MojoExecutionException
{
ArtifactRepository distributionRepository = project.getDistributionManagementArtifactRepository();
String remoteRepositoryId = "local";
if( distributionRepository != null )
{
remoteRepositoryId = distributionRepository.getId();
}
else if ( doDeploy )
{
throw new MojoExecutionException( "You must provide a distribution repository for your plugin." );
}
RepositoryMetadata metadata = createMetadataInstance();
try
{
repositoryMetadataManager.resolve( metadata, distributionRepository, localRepository, remoteRepositoryId );
}
catch ( RepositoryMetadataManagementException e )
{
Throwable cause = e.getCause();
if ( cause == null || ( cause instanceof ResourceDoesNotExistException ) )
{
getLog().debug( "Cannot find remote repository metadata for: " + metadata.getRepositoryPath() + "; creating new metadata resource..." );
metadata.setFile( null );
}
else
{
throw new MojoExecutionException( "Cannot retrieve plugin-mapping metadata for: "
+ metadata.getRepositoryPath(), e );
}
}
updateMetadata( metadata );
if ( distributionRepository != null )
{
try
{
repositoryMetadataManager.install( metadata, localRepository, remoteRepositoryId );
}
catch ( RepositoryMetadataManagementException e )
{
throw new MojoExecutionException( "Error installing plugin-mapping metadata to local repository.", e );
}
if ( doDeploy )
{
try
{
repositoryMetadataManager.deploy( metadata, distributionRepository );
}
catch ( RepositoryMetadataManagementException e )
{
throw new MojoExecutionException(
"Error deploying plugin-mapping metadata to distribution repository: "
+ distributionRepository.getId(), e );
}
}
}
}
protected File getMetadataFile( String path )
{
File metadataFile = new File( outputDirectory, path ).getAbsoluteFile();
File dir = metadataFile.getParentFile();
if ( !dir.exists() )
{
dir.mkdirs();
}
return metadataFile;
}
protected abstract void updateMetadata( RepositoryMetadata metadata )
throws MojoExecutionException;
protected abstract RepositoryMetadata createMetadataInstance();
}

View File

@ -0,0 +1,125 @@
package org.apache.maven.plugin.plugin.metadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.mapping.MappedPlugin;
import org.apache.maven.plugin.mapping.PluginMap;
import org.apache.maven.plugin.mapping.io.xpp3.PluginMappingXpp3Reader;
import org.apache.maven.plugin.mapping.io.xpp3.PluginMappingXpp3Writer;
import org.apache.maven.plugin.mapping.metadata.PluginMappingMetadata;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Iterator;
public abstract class AbstractPluginMappingPublisherMojo
extends AbstractMetadataPublisherMojo
{
protected RepositoryMetadata createMetadataInstance()
{
return new PluginMappingMetadata( getGroupId() );
}
protected void updateMetadata( RepositoryMetadata metadata ) throws MojoExecutionException
{
PluginMappingXpp3Reader mappingReader = new PluginMappingXpp3Reader();
PluginMap pluginMap = null;
Reader reader = null;
File metadataFile = metadata.getFile();
if ( metadataFile != null )
{
try
{
reader = new FileReader( metadataFile );
pluginMap = mappingReader.read( reader );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Cannot read plugin-mapping metadata from file: " + metadataFile, e );
}
catch ( XmlPullParserException e )
{
throw new MojoExecutionException( "Cannot parse plugin-mapping metadata from file: " + metadataFile, e );
}
finally
{
IOUtil.close( reader );
}
}
else
{
pluginMap = new PluginMap();
pluginMap.setGroupId( getGroupId() );
}
MappedPlugin mappedPlugin = new MappedPlugin();
mappedPlugin.setArtifactId( getArtifactId() );
mappedPlugin.setPrefix( getGoalPrefix() );
boolean prefixAlreadyMapped = false;
for ( Iterator it = pluginMap.getPlugins().iterator(); it.hasNext(); )
{
MappedPlugin preExisting = (MappedPlugin) it.next();
if ( preExisting.getPrefix().equals( getGoalPrefix() ) )
{
prefixAlreadyMapped = true;
if ( !preExisting.getArtifactId().equals( getArtifactId() ) )
{
// TODO: In this case, should we rather just replace the existing plugin mapping??
throw new MojoExecutionException( "Cannot map plugin to it's prefix in plugins.xml metadata; the prefix: \'" + getGoalPrefix() + "\' is already mapped to: " + preExisting.getArtifactId() );
}
else
{
getLog().info( "NOT updating plugins.xml metadata; this plugin is already mapped." );
}
}
}
if ( !prefixAlreadyMapped )
{
pluginMap.addPlugin( mappedPlugin );
Writer writer = null;
try
{
metadataFile = getMetadataFile( metadata.getRepositoryPath() );
writer = new FileWriter( metadataFile );
PluginMappingXpp3Writer mappingWriter = new PluginMappingXpp3Writer();
mappingWriter.write( writer, pluginMap );
metadata.setFile( metadataFile );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error writing repository metadata to build directory.", e );
}
finally
{
IOUtil.close( writer );
}
}
}
}

View File

@ -0,0 +1,19 @@
package org.apache.maven.plugin.plugin.metadata;
import org.apache.maven.plugin.MojoExecutionException;
/**
* @goal deploy-mapping
* @phase deploy
*/
public class PluginMappingDeployMojo
extends AbstractPluginMappingPublisherMojo
{
public void execute()
throws MojoExecutionException
{
publish( true );
}
}

View File

@ -0,0 +1,19 @@
package org.apache.maven.plugin.plugin.metadata;
import org.apache.maven.plugin.MojoExecutionException;
/**
* @goal install-mapping
* @phase install
*/
public class PluginMappingInstallMojo
extends AbstractPluginMappingPublisherMojo
{
public void execute()
throws MojoExecutionException
{
publish( false );
}
}