PR: MNG-847

merge metadata if it is duplicated

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@280086 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-09-11 07:08:55 +00:00
parent c585793b40
commit 285d97d93c
12 changed files with 302 additions and 403 deletions

View File

@ -110,4 +110,14 @@ public abstract class AbstractVersionArtifactMetadata
{
return new Date( lastModified );
}
public Object getKey()
{
return "legacy " + artifact.getGroupId() + ":" + artifact.getArtifactId();
}
public void merge( ArtifactMetadata metadata )
{
throw new IllegalStateException( "Cannot add two pieces of metadata for: " + getKey() );
}
}

View File

@ -16,11 +16,22 @@ package org.apache.maven.artifact.repository.metadata;
* limitations under the License.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* Shared methods of the repository metadata handling.
@ -31,6 +42,13 @@ import java.io.IOException;
public abstract class AbstractRepositoryMetadata
implements ArtifactMetadata
{
private Metadata metadata;
protected AbstractRepositoryMetadata( Metadata metadata )
{
this.metadata = metadata;
}
public String getRemoteFilename()
{
return "maven-metadata.xml";
@ -54,7 +72,114 @@ public abstract class AbstractRepositoryMetadata
}
}
protected abstract void updateRepositoryMetadata( ArtifactRepository localRepository,
ArtifactRepository remoteRepository )
throws IOException;
protected void updateRepositoryMetadata( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
throws IOException
{
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
Metadata metadata = null;
File metadataFile = new File( localRepository.getBasedir(),
localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) );
if ( metadataFile.exists() )
{
Reader reader = null;
try
{
reader = new FileReader( metadataFile );
metadata = mappingReader.read( reader );
}
catch ( FileNotFoundException e )
{
// TODO: Log a warning
}
catch ( IOException e )
{
// TODO: Log a warning
}
catch ( XmlPullParserException e )
{
// TODO: Log a warning
}
finally
{
IOUtil.close( reader );
}
}
boolean changed = false;
// If file could not be found or was not valid, start from scratch
if ( metadata == null )
{
metadata = new Metadata();
metadata.setGroupId( getGroupId() );
metadata.setArtifactId( getArtifactId() );
metadata.setVersion( getBaseVersion() );
changed = true;
}
changed |= metadata.merge( this.metadata );
if ( changed )
{
Writer writer = null;
try
{
metadataFile.getParentFile().mkdirs();
writer = new FileWriter( metadataFile );
MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
mappingWriter.write( writer, metadata );
}
finally
{
IOUtil.close( writer );
}
}
else
{
metadataFile.setLastModified( System.currentTimeMillis() );
}
}
public String toString()
{
return "repository metadata for: \'" + getKey() + "\'";
}
protected static Metadata createMetadata( Artifact artifact, Versioning versioning )
{
Metadata metadata = new Metadata();
metadata.setGroupId( artifact.getGroupId() );
metadata.setArtifactId( artifact.getArtifactId() );
metadata.setVersion( artifact.getVersion() );
metadata.setVersioning( versioning );
return metadata;
}
protected static Versioning createVersioning( Snapshot snapshot )
{
Versioning versioning = new Versioning();
versioning.setSnapshot( snapshot );
return versioning;
}
protected Metadata getMetadata()
{
return metadata;
}
public void merge( ArtifactMetadata metadata )
{
// TODO: not sure that it should assume this, maybe the calls to addMetadata should pre-merge, then artifact replaces?
AbstractRepositoryMetadata repoMetadata = (AbstractRepositoryMetadata) metadata;
this.metadata.merge( repoMetadata.getMetadata() );
}
}

View File

@ -17,20 +17,6 @@ package org.apache.maven.artifact.repository.metadata;
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Iterator;
/**
* Metadata for the artifact directory of the repository.
@ -42,26 +28,19 @@ import java.util.Iterator;
public class ArtifactRepositoryMetadata
extends AbstractRepositoryMetadata
{
private Versioning versioning;
private Artifact artifact;
public ArtifactRepositoryMetadata( Artifact artifact )
{
this.artifact = artifact;
this( artifact, null );
}
public ArtifactRepositoryMetadata( Artifact artifact, Versioning versioning )
{
this.versioning = versioning;
super( createMetadata( artifact, versioning ) );
this.artifact = artifact;
}
public String toString()
{
return "repository metadata for: \'" + getKey() + "\'";
}
public boolean storedInGroupDirectory()
{
return false;
@ -84,127 +63,19 @@ public class ArtifactRepositoryMetadata
public String getBaseVersion()
{
// Don't want the artifact's version in here, as this is stored in the directory above that
return null;
}
protected void updateRepositoryMetadata( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
throws IOException
{
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
Metadata metadata = null;
File metadataFile = new File( localRepository.getBasedir(),
localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) );
if ( metadataFile.exists() )
{
Reader reader = null;
try
{
reader = new FileReader( metadataFile );
metadata = mappingReader.read( reader );
}
catch ( FileNotFoundException e )
{
// TODO: Log a warning
}
catch ( IOException e )
{
// TODO: Log a warning
}
catch ( XmlPullParserException e )
{
// TODO: Log a warning
}
finally
{
IOUtil.close( reader );
}
}
boolean changed = false;
// If file could not be found or was not valid, start from scratch
if ( metadata == null )
{
metadata = new Metadata();
metadata.setGroupId( artifact.getGroupId() );
metadata.setArtifactId( artifact.getArtifactId() );
changed = true;
}
if ( versioning != null )
{
Versioning v = metadata.getVersioning();
if ( v != null )
{
if ( versioning.getRelease() != null )
{
changed = true;
v.setRelease( versioning.getRelease() );
}
if ( versioning.getLatest() != null )
{
changed = true;
v.setLatest( versioning.getLatest() );
}
for ( Iterator i = versioning.getVersions().iterator(); i.hasNext(); )
{
String version = (String) i.next();
if ( !v.getVersions().contains( version ) )
{
changed = true;
v.getVersions().add( version );
}
}
}
else
{
metadata.setVersioning( versioning );
changed = true;
}
}
if ( changed )
{
Writer writer = null;
try
{
metadataFile.getParentFile().mkdirs();
writer = new FileWriter( metadataFile );
MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
mappingWriter.write( writer, metadata );
}
finally
{
IOUtil.close( writer );
}
}
else
{
metadataFile.setLastModified( System.currentTimeMillis() );
}
}
public Object getKey()
{
return artifact.getGroupId() + ":" + artifact.getArtifactId();
return "artifact " + artifact.getGroupId() + ":" + artifact.getArtifactId();
}
public boolean isSnapshot()
{
return artifact.isSnapshot();
}
public Snapshot getSnapshot()
{
return null;
// Don't consider the artifact's version in here, as this is stored in the directory above that
return false;
}
}

View File

@ -16,22 +16,8 @@ package org.apache.maven.artifact.repository.metadata;
* limitations under the License.
*/
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
/**
* Metadata for the group directory of the repository.
@ -44,18 +30,12 @@ public class GroupRepositoryMetadata
{
private final String groupId;
private Map pluginMappings = new HashMap();
public GroupRepositoryMetadata( String groupId )
{
super( new Metadata() );
this.groupId = groupId;
}
public String toString()
{
return "repository metadata for group: \'" + groupId + "\'";
}
public boolean storedInGroupDirectory()
{
return true;
@ -83,110 +63,23 @@ public class GroupRepositoryMetadata
public void addPluginMapping( String goalPrefix, String artifactId )
{
pluginMappings.put( goalPrefix, artifactId );
}
protected void updateRepositoryMetadata( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
throws IOException
{
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
Metadata pluginMap = null;
File metadataFile = new File( localRepository.getBasedir(),
localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) );
if ( metadataFile.exists() )
List plugins = getMetadata().getPlugins();
boolean found = false;
for ( Iterator i = plugins.iterator(); i.hasNext() && !found; )
{
Reader reader = null;
try
Plugin plugin = (Plugin) i.next();
if ( plugin.getPrefix().equals( goalPrefix ) )
{
reader = new FileReader( metadataFile );
pluginMap = mappingReader.read( reader );
}
catch ( FileNotFoundException e )
{
// TODO: Log a warning
}
catch ( IOException e )
{
// TODO: Log a warning
}
catch ( XmlPullParserException e )
{
// TODO: Log a warning
}
finally
{
IOUtil.close( reader );
found = true;
}
}
boolean changed = false;
// If file could not be found or was not valid, start from scratch
if ( pluginMap == null )
if ( !found )
{
pluginMap = new Metadata();
Plugin plugin = new Plugin();
plugin.setPrefix( goalPrefix );
plugin.setArtifactId( artifactId );
pluginMap.setGroupId( groupId );
changed = true;
}
for ( Iterator i = pluginMappings.keySet().iterator(); i.hasNext(); )
{
String prefix = (String) i.next();
boolean found = false;
for ( Iterator it = pluginMap.getPlugins().iterator(); it.hasNext() && !found; )
{
Plugin preExisting = (Plugin) it.next();
if ( preExisting.getPrefix().equals( prefix ) )
{
// TODO: log
// getLog().info( "Plugin-mapping metadata for prefix: " + prefix + " already exists. Skipping." );
found = true;
}
}
if ( !found )
{
Plugin mappedPlugin = new Plugin();
mappedPlugin.setArtifactId( (String) pluginMappings.get( prefix ) );
mappedPlugin.setPrefix( prefix );
pluginMap.addPlugin( mappedPlugin );
changed = true;
}
}
if ( changed )
{
Writer writer = null;
try
{
writer = new FileWriter( metadataFile );
MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
mappingWriter.write( writer, pluginMap );
}
finally
{
IOUtil.close( writer );
}
}
else
{
metadataFile.setLastModified( System.currentTimeMillis() );
getMetadata().addPlugin( plugin );
}
}

View File

@ -17,19 +17,6 @@ package org.apache.maven.artifact.repository.metadata;
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* Metadata for the artifact version directory of the repository.
@ -41,27 +28,19 @@ import java.io.Writer;
public class SnapshotArtifactRepositoryMetadata
extends AbstractRepositoryMetadata
{
private Snapshot snapshot;
private Artifact artifact;
public SnapshotArtifactRepositoryMetadata( Artifact artifact )
{
this.artifact = artifact;
this.snapshot = new Snapshot();
this( artifact, new Snapshot() );
}
public SnapshotArtifactRepositoryMetadata( Artifact artifact, Snapshot snapshot )
{
this.snapshot = snapshot;
super( createMetadata( artifact, createVersioning( snapshot ) ) );
this.artifact = artifact;
}
public String toString()
{
return "repository metadata for: \'" + getKey() + "\'";
}
public boolean storedInGroupDirectory()
{
return false;
@ -87,117 +66,13 @@ public class SnapshotArtifactRepositoryMetadata
return artifact.getBaseVersion();
}
protected void updateRepositoryMetadata( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
throws IOException
{
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
Metadata metadata = null;
File metadataFile = new File( localRepository.getBasedir(),
localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) );
if ( metadataFile.exists() )
{
Reader reader = null;
try
{
reader = new FileReader( metadataFile );
metadata = mappingReader.read( reader );
}
catch ( FileNotFoundException e )
{
// TODO: Log a warning
}
catch ( IOException e )
{
// TODO: Log a warning
}
catch ( XmlPullParserException e )
{
// TODO: Log a warning
}
finally
{
IOUtil.close( reader );
}
}
boolean changed = false;
// If file could not be found or was not valid, start from scratch
if ( metadata == null )
{
metadata = new Metadata();
metadata.setGroupId( artifact.getGroupId() );
metadata.setArtifactId( artifact.getArtifactId() );
changed = true;
}
if ( snapshot != null )
{
Versioning v = metadata.getVersioning();
if ( v == null )
{
v = new Versioning();
metadata.setVersioning( v );
}
Snapshot s = v.getSnapshot();
if ( s == null )
{
v.setSnapshot( snapshot );
changed = true;
}
else
{
if ( s.getTimestamp() != null && !s.getTimestamp().equals( snapshot.getTimestamp() ) )
{
s.setTimestamp( snapshot.getTimestamp() );
changed = true;
}
if ( s.getBuildNumber() != snapshot.getBuildNumber() )
{
s.setBuildNumber( snapshot.getBuildNumber() );
changed = true;
}
}
}
if ( changed )
{
Writer writer = null;
try
{
metadataFile.getParentFile().mkdirs();
writer = new FileWriter( metadataFile );
MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
mappingWriter.write( writer, metadata );
}
finally
{
IOUtil.close( writer );
}
}
else
{
metadataFile.setLastModified( System.currentTimeMillis() );
}
}
public Object getKey()
{
return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getBaseVersion();
return "snapshot " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getBaseVersion();
}
public boolean isSnapshot()
{
return artifact.isSnapshot();
}
}

View File

@ -23,6 +23,7 @@ import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.versioning.VersionRange;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
@ -95,7 +96,7 @@ public interface Artifact
void addMetadata( ArtifactMetadata metadata );
List getMetadataList();
Collection getMetadataList();
void setRepository( ArtifactRepository remoteRepository );

View File

@ -24,9 +24,11 @@ import org.apache.maven.artifact.versioning.VersionRange;
import org.codehaus.plexus.util.StringUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
/**
@ -54,8 +56,6 @@ public class DefaultArtifact
private String scope;
private List metadataList;
private File file;
private ArtifactRepository repository;
@ -74,10 +74,12 @@ public class DefaultArtifact
private boolean resolved;
private boolean release = false;
private boolean release;
private List availableVersions;
private Map metadataMap;
public DefaultArtifact( String groupId, String artifactId, VersionRange versionRange, String scope, String type,
String classifier, ArtifactHandler artifactHandler )
{
@ -213,16 +215,25 @@ public class DefaultArtifact
public void addMetadata( ArtifactMetadata metadata )
{
if ( metadataList == null )
if ( metadataMap == null )
{
metadataList = new ArrayList();
metadataMap = new HashMap();
}
ArtifactMetadata m = (ArtifactMetadata) metadataMap.get( metadata.getKey() );
if ( m != null )
{
m.merge( metadata );
}
else
{
metadataMap.put( metadata.getKey(), metadata );
}
metadataList.add( metadata );
}
public List getMetadataList()
public Collection getMetadataList()
{
return metadataList == null ? Collections.EMPTY_LIST : metadataList;
return metadataMap == null ? Collections.EMPTY_LIST : metadataMap.values();
}
// ----------------------------------------------------------------------

View File

@ -53,10 +53,4 @@ public abstract class AbstractArtifactMetadata
{
return artifact.getVersion();
}
public Object getKey()
{
return artifact.getGroupId() + ":" + artifact.getArtifactId();
}
}

View File

@ -73,4 +73,10 @@ public interface ArtifactMetadata
*/
String getRemoteFilename();
/**
* Merge a new metadata set into this piece of metadata.
*
* @param metadata the new metadata
*/
void merge( ArtifactMetadata metadata );
}

View File

@ -25,6 +25,7 @@ import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.project.MavenProject;
import java.io.File;
import java.util.Collection;
import java.util.List;
/**
@ -131,7 +132,7 @@ public class ActiveProjectArtifact
artifact.addMetadata( metadata );
}
public List getMetadataList()
public Collection getMetadataList()
{
return artifact.getMetadataList();
}

View File

@ -19,6 +19,7 @@ package org.apache.maven.project.artifact;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactStatus;
import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.DistributionManagement;
@ -146,4 +147,17 @@ public class ProjectArtifactMetadata
return artifact.isSnapshot();
}
public Object getKey()
{
return "project " + artifact.getGroupId() + ":" + artifact.getArtifactId();
}
public void merge( ArtifactMetadata metadata )
{
ProjectArtifactMetadata m = (ProjectArtifactMetadata) metadata;
if ( !m.file.equals( file ) )
{
throw new IllegalStateException( "Cannot add two different pieces of metadata for: " + getKey() );
}
}
}

View File

@ -49,6 +49,104 @@
</association>
</field>
</fields>
<codeSegments>
<codeSegment>
<version>1.0.0</version>
<code><![CDATA[
public boolean merge( Metadata sourceMetadata )
{
boolean changed = false;
for ( Iterator i = sourceMetadata.getPlugins().iterator(); i.hasNext(); )
{
Plugin plugin = (Plugin) i.next();
boolean found = false;
for ( Iterator it = getPlugins().iterator(); it.hasNext() && !found; )
{
Plugin preExisting = (Plugin) it.next();
if ( preExisting.getPrefix().equals( plugin.getPrefix() ) )
{
found = true;
}
}
if ( !found )
{
Plugin mappedPlugin = new Plugin();
mappedPlugin.setArtifactId( plugin.getArtifactId() );
mappedPlugin.setPrefix( plugin.getPrefix() );
addPlugin( mappedPlugin );
changed = true;
}
}
Versioning versioning = sourceMetadata.getVersioning();
if ( versioning != null )
{
Versioning v = getVersioning();
if ( v != null )
{
if ( versioning.getRelease() != null )
{
changed = true;
v.setRelease( versioning.getRelease() );
}
if ( versioning.getLatest() != null )
{
changed = true;
v.setLatest( versioning.getLatest() );
}
for ( Iterator i = versioning.getVersions().iterator(); i.hasNext(); )
{
String version = (String) i.next();
if ( !v.getVersions().contains( version ) )
{
changed = true;
v.getVersions().add( version );
}
}
Snapshot s = v.getSnapshot();
Snapshot snapshot = versioning.getSnapshot();
if ( snapshot != null )
{
if ( s == null )
{
v.setSnapshot( snapshot );
changed = true;
}
else
{
if ( snapshot.getTimestamp() != null && !snapshot.getTimestamp().equals( s.getTimestamp() ) )
{
s.setTimestamp( snapshot.getTimestamp() );
changed = true;
}
if ( s.getBuildNumber() != snapshot.getBuildNumber() )
{
s.setBuildNumber( snapshot.getBuildNumber() );
changed = true;
}
}
}
}
else
{
setVersioning( versioning );
changed = true;
}
}
return changed;
}
]]></code>
</codeSegment>
</codeSegments>
</class>
<class>
<name>Versioning</name>