diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java index 934756b5be..924d28ca6b 100644 --- a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java @@ -34,9 +34,11 @@ import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; public class DefaultRepositoryMetadataManager @@ -100,6 +102,8 @@ public class DefaultRepositoryMetadataManager // TODO: this needs to be repeated here so the merging doesn't interfere with the written metadata // - we'd be much better having a pristine input, and an ongoing metadata for merging instead + Map previousMetadata = new HashMap(); + ArtifactRepository selected = null; for ( Iterator i = remoteRepositories.iterator(); i.hasNext(); ) { ArtifactRepository repository = (ArtifactRepository) i.next(); @@ -109,16 +113,68 @@ public class DefaultRepositoryMetadataManager if ( policy.isEnabled() ) { - loadMetadata( metadata, repository, localRepository, true ); + if ( loadMetadata( metadata, repository, localRepository, previousMetadata ) ) + { + metadata.setRepository( repository ); + selected = repository; + } } } - loadMetadata( metadata, localRepository, localRepository, false ); + if ( loadMetadata( metadata, localRepository, localRepository, previousMetadata ) ) + { + selected = localRepository; + } + + // TODO: this could be a lot nicer... should really be in the snapshot transformation? + if ( metadata.isSnapshot() ) + { + Metadata prevMetadata = metadata.getMetadata(); + + for ( Iterator i = previousMetadata.keySet().iterator(); i.hasNext(); ) + { + ArtifactRepository repository = (ArtifactRepository) i.next(); + Metadata m = (Metadata) previousMetadata.get( repository ); + if ( repository.equals( selected ) ) + { + if ( m.getVersioning() == null ) + { + m.setVersioning( new Versioning() ); + } + + if ( m.getVersioning().getSnapshot() == null ) + { + m.getVersioning().setSnapshot( new Snapshot() ); + } + + if ( !m.getVersioning().getSnapshot().isLocalCopy() ) + { + m.getVersioning().getSnapshot().setLocalCopy( true ); + metadata.setMetadata( m ); + metadata.storeInLocalRepository( localRepository, repository ); + } + } + else + { + if ( m.getVersioning() != null && m.getVersioning().getSnapshot() != null && + m.getVersioning().getSnapshot().isLocalCopy() ) + { + m.getVersioning().getSnapshot().setLocalCopy( false ); + metadata.setMetadata( m ); + metadata.storeInLocalRepository( localRepository, repository ); + } + } + } + + metadata.setMetadata( prevMetadata ); + } } - private void loadMetadata( RepositoryMetadata repoMetadata, ArtifactRepository remoteRepository, - ArtifactRepository localRepository, boolean setRepository ) + private boolean loadMetadata( RepositoryMetadata repoMetadata, ArtifactRepository remoteRepository, + ArtifactRepository localRepository, Map previousMetadata ) throws ArtifactMetadataRetrievalException { + boolean setRepository = false; + File metadataFile = new File( localRepository.getBasedir(), localRepository.pathOfLocalRepositoryMetadata( repoMetadata, remoteRepository ) ); @@ -126,25 +182,22 @@ public class DefaultRepositoryMetadataManager { Metadata metadata = readMetadata( metadataFile ); + if ( repoMetadata.isSnapshot() && previousMetadata != null ) + { + previousMetadata.put( remoteRepository, metadata ); + } + if ( repoMetadata.getMetadata() != null ) { - if ( repoMetadata.getMetadata().merge( metadata ) ) - { - if ( setRepository ) - { - repoMetadata.setRepository( remoteRepository ); - } - } + setRepository = repoMetadata.getMetadata().merge( metadata ); } else { repoMetadata.setMetadata( metadata ); - if ( setRepository ) - { - repoMetadata.setRepository( remoteRepository ); - } + setRepository = true; } } + return setRepository; } /** diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java index ceb2f03720..1217afc141 100644 --- a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java @@ -27,8 +27,10 @@ import org.apache.maven.artifact.transform.ArtifactTransformationManager; 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; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -116,10 +118,10 @@ public class DefaultArtifactResolver if ( !wagonManager.isOnline() ) { getLogger().debug( "System is offline. Cannot resolve artifact: " + artifact.getId() + "." ); - + return; } - + try { if ( artifact.getRepository() != null ) @@ -147,6 +149,23 @@ public class DefaultArtifactResolver { throw new ArtifactResolutionException( e.getMessage(), artifact, remoteRepositories, e ); } + + if ( artifact.isSnapshot() && !artifact.getBaseVersion().equals( artifact.getVersion() ) ) + { + String version = artifact.getVersion(); + artifact.selectVersion( artifact.getBaseVersion() ); + File copy = new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) ); + try + { + FileUtils.copyFile( destination, copy ); + } + catch ( IOException e ) + { + throw new ArtifactResolutionException( "Unable to copy resolved artifact for local use", + artifact, remoteRepositories, e ); + } + artifact.selectVersion( version ); + } } else if ( destination.exists() ) { diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/SnapshotTransformation.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/SnapshotTransformation.java index 58842093d5..f151b08288 100644 --- a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/SnapshotTransformation.java +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/SnapshotTransformation.java @@ -107,15 +107,15 @@ public class SnapshotTransformation Snapshot snapshot = versioning.getSnapshot(); if ( snapshot != null ) { - if ( snapshot.isLocalCopy() ) - { - version = baseVersion; - } - else if ( snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0 ) + if ( snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0 ) { String newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber(); version = StringUtils.replace( baseVersion, "SNAPSHOT", newVersion ); } + else + { + version = baseVersion; + } } return version; } diff --git a/maven-repository-metadata/src/main/mdo/metadata.mdo b/maven-repository-metadata/src/main/mdo/metadata.mdo index dd62ab62ef..d552b2a7d9 100644 --- a/maven-repository-metadata/src/main/mdo/metadata.mdo +++ b/maven-repository-metadata/src/main/mdo/metadata.mdo @@ -79,9 +79,9 @@ mappedPlugin.setArtifactId( plugin.getArtifactId() ); mappedPlugin.setPrefix( plugin.getPrefix() ); - + mappedPlugin.setName( plugin.getName() ); - + addPlugin( mappedPlugin ); changed = true; @@ -142,30 +142,22 @@ changed = true; } - if ( snapshot.isLocalCopy() ) + // overwrite + if ( s.getTimestamp() == null ? snapshot.getTimestamp() != null + : !s.getTimestamp().equals( snapshot.getTimestamp() ) ) { - s.setLocalCopy( true ); - s.setTimestamp( null ); - s.setBuildNumber( 0 ); + s.setTimestamp( snapshot.getTimestamp() ); changed = true; } - else + if ( s.getBuildNumber() != snapshot.getBuildNumber() ) { - if ( snapshot.getTimestamp() != null && !snapshot.getTimestamp().equals( s.getTimestamp() ) ) - { - s.setTimestamp( snapshot.getTimestamp() ); - changed = true; - } - if ( snapshot.getBuildNumber() > 0 && s.getBuildNumber() != snapshot.getBuildNumber() ) - { - s.setBuildNumber( snapshot.getBuildNumber() ); - changed = true; - } - if ( s.isLocalCopy() ) - { - s.setLocalCopy( false ); - changed = true; - } + s.setBuildNumber( snapshot.getBuildNumber() ); + changed = true; + } + if ( s.isLocalCopy() != snapshot.isLocalCopy() ) + { + s.setLocalCopy( snapshot.isLocalCopy() ); + changed = true; } } }