add metadata for new artifact

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@290084 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-09-19 07:52:26 +00:00
parent 386cc730ed
commit 52d376bf7f
4 changed files with 130 additions and 63 deletions

View File

@ -73,8 +73,7 @@ public class RepositoryCleaner
File targetRepositoryBase = normalizeTargetRepositoryBase( configuration.getTargetRepositoryPath() ); File targetRepositoryBase = normalizeTargetRepositoryBase( configuration.getTargetRepositoryPath() );
// do not proceed if we cannot produce reports, or if the repository is // do not proceed if we cannot produce reports, or if the repository is invalid.
// invalid.
if ( reportsBase != null && sourceRepositoryBase != null && targetRepositoryBase != null ) if ( reportsBase != null && sourceRepositoryBase != null && targetRepositoryBase != null )
{ {
Logger logger = getLogger(); Logger logger = getLogger();
@ -96,15 +95,13 @@ public class RepositoryCleaner
try try
{ {
sourceLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, sourceLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE,
configuration configuration.getSourceRepositoryLayout() );
.getSourceRepositoryLayout() );
ArtifactRepository sourceRepo = new DefaultArtifactRepository( "source", "file://" + ArtifactRepository sourceRepo = new DefaultArtifactRepository( "source", "file://" +
sourceRepositoryBase.getAbsolutePath(), sourceLayout ); sourceRepositoryBase.getAbsolutePath(), sourceLayout );
targetLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, targetLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE,
configuration configuration.getTargetRepositoryLayout() );
.getTargetRepositoryLayout() );
ArtifactRepository targetRepo = new DefaultArtifactRepository( "target", "file://" + ArtifactRepository targetRepo = new DefaultArtifactRepository( "target", "file://" +
targetRepositoryBase.getAbsolutePath(), targetLayout ); targetRepositoryBase.getAbsolutePath(), targetLayout );
@ -114,8 +111,6 @@ public class RepositoryCleaner
logger.debug( "Rewriting POMs and artifact files." ); logger.debug( "Rewriting POMs and artifact files." );
} }
// List originalArtifacts = new ArrayList( artifacts );
List rewritten = rewritePhase.execute( artifacts, sourceRepo, targetRepo, configuration, List rewritten = rewritePhase.execute( artifacts, sourceRepo, targetRepo, configuration,
reportsBase, repoReporter ); reportsBase, repoReporter );

View File

@ -33,7 +33,6 @@ import java.security.NoSuchAlgorithmException;
*/ */
public class Digestor public class Digestor
{ {
public static final String ROLE = Digestor.class.getName(); public static final String ROLE = Digestor.class.getName();
public static final String MD5 = "MD5"; public static final String MD5 = "MD5";
@ -60,6 +59,25 @@ public class Digestor
} }
} }
public File getDigestFile( File artifactFile, String algorithm )
throws NoSuchAlgorithmException
{
String extension;
if ( SHA.equals( algorithm ) )
{
extension = "sha1";
}
else if ( MD5.equals( algorithm ) )
{
extension = "md5";
}
else
{
throw new NoSuchAlgorithmException( "Unknown algorithm " + algorithm );
}
return new File( artifactFile.getParentFile(), artifactFile.getName() + "." + extension );
}
public boolean verifyArtifactDigest( File artifactFile, File digestFile, String algorithm ) public boolean verifyArtifactDigest( File artifactFile, File digestFile, String algorithm )
throws DigestException throws DigestException
{ {

View File

@ -22,13 +22,16 @@ import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Metadata; import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.Snapshot;
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Versioning;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer; import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
import org.apache.maven.project.artifact.ProjectArtifactMetadata; import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.tools.repoclean.RepositoryCleanerConfiguration; import org.apache.maven.tools.repoclean.RepositoryCleanerConfiguration;
import org.apache.maven.tools.repoclean.digest.DigestException; import org.apache.maven.tools.repoclean.digest.DigestException;
import org.apache.maven.tools.repoclean.digest.DigestVerifier; import org.apache.maven.tools.repoclean.digest.DigestVerifier;
import org.apache.maven.tools.repoclean.digest.Digestor;
import org.apache.maven.tools.repoclean.report.ReportWriteException; import org.apache.maven.tools.repoclean.report.ReportWriteException;
import org.apache.maven.tools.repoclean.report.Reporter; import org.apache.maven.tools.repoclean.report.Reporter;
import org.apache.maven.tools.repoclean.rewrite.ArtifactPomRewriter; import org.apache.maven.tools.repoclean.rewrite.ArtifactPomRewriter;
@ -60,6 +63,7 @@ import java.io.StringReader;
import java.io.Writer; import java.io.Writer;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -70,6 +74,8 @@ public class RewritePhase
{ {
private DigestVerifier digestVerifier; private DigestVerifier digestVerifier;
private Digestor artifactDigestor;
private ArtifactRepositoryLayout bridgingLayout; private ArtifactRepositoryLayout bridgingLayout;
private PlexusContainer container; private PlexusContainer container;
@ -219,14 +225,43 @@ public class RewritePhase
File metadataSource = new File( sourceBase, sourceRepo.pathOfRemoteRepositoryMetadata( metadata ) ); File metadataSource = new File( sourceBase, sourceRepo.pathOfRemoteRepositoryMetadata( metadata ) );
File metadataTarget = new File( targetBase, targetRepo.pathOfRemoteRepositoryMetadata( metadata ) ); File metadataTarget = new File( targetBase, targetRepo.pathOfRemoteRepositoryMetadata( metadata ) );
mergeMetadata( metadataSource, metadataTarget, transaction, artifactReporter, reportOnly ); Metadata sourceMetadata = readMetadata( metadataSource, artifact );
if ( sourceMetadata.getVersioning() == null )
{
sourceMetadata.setVersioning( new Versioning() );
}
if ( !sourceMetadata.getVersioning().getVersions().contains( artifact.getBaseVersion() ) )
{
sourceMetadata.getVersioning().addVersion( artifact.getBaseVersion() );
}
mergeMetadata( sourceMetadata, metadataTarget, reportOnly );
metadata = new SnapshotArtifactRepositoryMetadata( artifact ); metadata = new SnapshotArtifactRepositoryMetadata( artifact );
metadataSource = new File( sourceBase, sourceRepo.pathOfRemoteRepositoryMetadata( metadata ) ); metadataSource = new File( sourceBase, sourceRepo.pathOfRemoteRepositoryMetadata( metadata ) );
metadataTarget = new File( targetBase, targetRepo.pathOfRemoteRepositoryMetadata( metadata ) ); metadataTarget = new File( targetBase, targetRepo.pathOfRemoteRepositoryMetadata( metadata ) );
mergeMetadata( metadataSource, metadataTarget, transaction, artifactReporter, reportOnly ); sourceMetadata = readMetadata( metadataSource, artifact );
if ( artifact.isSnapshot() )
{
if ( sourceMetadata.getVersioning() == null )
{
sourceMetadata.setVersioning( new Versioning() );
}
if ( sourceMetadata.getVersioning().getSnapshot() == null )
{
sourceMetadata.getVersioning().setSnapshot( new Snapshot() );
}
int i = artifact.getVersion().indexOf( '-' );
if ( i >= 0 )
{
Snapshot snapshot = sourceMetadata.getVersioning().getSnapshot();
snapshot.setTimestamp( artifact.getVersion().substring( 0, i ) );
snapshot.setBuildNumber( Integer.valueOf( artifact.getVersion().substring( i + 1 ) ).intValue() );
}
}
mergeMetadata( sourceMetadata, metadataTarget, reportOnly );
// The rest is for POM metadata - translation and bridging of locations in the target repo may be required. // The rest is for POM metadata - translation and bridging of locations in the target repo may be required.
ArtifactMetadata pom = new ProjectArtifactMetadata( artifact, null ); ArtifactMetadata pom = new ProjectArtifactMetadata( artifact, null );
@ -326,76 +361,92 @@ public class RewritePhase
} }
} }
private void mergeMetadata( File source, File target, RewriteTransaction transaction, Reporter artifactReporter, private void mergeMetadata( Metadata sourceMetadata, File target, boolean reportOnly )
boolean reportOnly ) throws IOException, DigestException, XmlPullParserException, NoSuchAlgorithmException
throws IOException, DigestException, ReportWriteException, XmlPullParserException
{ {
if ( source.exists() ) if ( target.exists() )
{ {
if ( !target.exists() ) Metadata targetMetadata = null;
{
copyMetadata( source, target, transaction, artifactReporter, reportOnly ); Reader reader = null;
}
else try
{ {
reader = new FileReader( target );
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader(); MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
Metadata sourceMetadata = null; targetMetadata = mappingReader.read( reader );
}
finally
{
IOUtil.close( reader );
}
Reader reader = null; boolean changed = targetMetadata.merge( sourceMetadata );
if ( changed )
{
Writer writer = null;
try try
{ {
reader = new FileReader( source ); target.getParentFile().mkdirs();
writer = new FileWriter( target );
sourceMetadata = mappingReader.read( reader ); MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
mappingWriter.write( writer, targetMetadata );
if ( !reportOnly )
{
File digestFile = artifactDigestor.getDigestFile( target, Digestor.MD5 );
artifactDigestor.createArtifactDigest( target, digestFile, Digestor.MD5 );
digestFile = artifactDigestor.getDigestFile( target, Digestor.SHA );
artifactDigestor.createArtifactDigest( target, digestFile, Digestor.SHA );
}
} }
finally finally
{ {
IOUtil.close( reader ); IOUtil.close( writer );
reader = null;
} }
Metadata targetMetadata = null;
try
{
reader = new FileReader( target );
targetMetadata = mappingReader.read( reader );
}
finally
{
IOUtil.close( reader );
}
boolean changed = false;
changed |= targetMetadata.merge( sourceMetadata );
if ( changed )
{
Writer writer = null;
try
{
target.getParentFile().mkdirs();
writer = new FileWriter( target );
MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
mappingWriter.write( writer, targetMetadata );
}
finally
{
IOUtil.close( writer );
}
}
digestVerifier.verifyDigest( source, target, transaction, artifactReporter, reportOnly );
} }
} }
} }
private Metadata readMetadata( File source, Artifact artifact )
throws IOException, XmlPullParserException
{
Metadata sourceMetadata = null;
if ( source.exists() )
{
Reader reader = null;
try
{
reader = new FileReader( source );
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
sourceMetadata = mappingReader.read( reader );
}
finally
{
IOUtil.close( reader );
}
}
if ( sourceMetadata == null )
{
sourceMetadata = new Metadata();
sourceMetadata.setGroupId( artifact.getGroupId() );
sourceMetadata.setArtifactId( artifact.getArtifactId() );
sourceMetadata.setVersion( artifact.getBaseVersion() );
}
return sourceMetadata;
}
private void copyMetadata( File source, File target, RewriteTransaction transaction, Reporter artifactReporter, private void copyMetadata( File source, File target, RewriteTransaction transaction, Reporter artifactReporter,
boolean reportOnly ) boolean reportOnly )
throws IOException, DigestException, ReportWriteException throws IOException, DigestException, ReportWriteException

View File

@ -44,6 +44,9 @@
<requirement> <requirement>
<role>org.apache.maven.tools.repoclean.digest.DigestVerifier</role> <role>org.apache.maven.tools.repoclean.digest.DigestVerifier</role>
</requirement> </requirement>
<requirement>
<role>org.apache.maven.tools.repoclean.digest.Digestor</role>
</requirement>
</requirements> </requirements>
</component> </component>
<!-- <!--