Coverage Report - org.apache.maven.artifact.transform.SnapshotTransformation
 
Classes in this File Line Coverage Branch Coverage Complexity
SnapshotTransformation
15 %
8/53
19 %
5/26
0
 
 1  
 package org.apache.maven.artifact.transform;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.artifact.Artifact;
 23  
 import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
 24  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 25  
 import org.apache.maven.artifact.repository.metadata.Metadata;
 26  
 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
 27  
 import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
 28  
 import org.apache.maven.artifact.repository.metadata.Snapshot;
 29  
 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
 30  
 import org.apache.maven.artifact.repository.metadata.Versioning;
 31  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 32  
 import org.codehaus.plexus.util.StringUtils;
 33  
 
 34  
 import java.text.DateFormat;
 35  
 import java.text.SimpleDateFormat;
 36  
 import java.util.Date;
 37  
 import java.util.List;
 38  
 import java.util.TimeZone;
 39  
 
 40  
 /**
 41  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 42  
  * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
 43  
  * @version $Id: SnapshotTransformation.java,v 1.1 2005/03/03 15:37:25
 44  
  *          jvanzyl Exp $
 45  
  */
 46  13
 public class SnapshotTransformation
 47  
     extends AbstractVersionTransformation
 48  
 {
 49  
     private String deploymentTimestamp;
 50  
 
 51  1
     private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
 52  
 
 53  
     private static final String UTC_TIMESTAMP_PATTERN = "yyyyMMdd.HHmmss";
 54  
 
 55  
     public void transformForResolve( Artifact artifact, List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository )
 56  
         throws ArtifactResolutionException
 57  
     {
 58  
         // Only select snapshots that are unresolved (eg 1.0-SNAPSHOT, not 1.0-20050607.123456)
 59  16
         if ( artifact.isSnapshot() && artifact.getBaseVersion().equals( artifact.getVersion() ) )
 60  
         {
 61  
             try
 62  
             {
 63  0
                 String version = resolveVersion( artifact, localRepository, remoteRepositories );
 64  0
                 artifact.updateVersion( version, localRepository );
 65  
             }
 66  0
             catch ( RepositoryMetadataResolutionException e )
 67  
             {
 68  0
                 throw new ArtifactResolutionException( e.getMessage(), artifact, e );
 69  0
             }
 70  
         }
 71  16
     }
 72  
 
 73  
     public void transformForInstall( Artifact artifact, ArtifactRepository localRepository )
 74  
     {
 75  1
         if ( artifact.isSnapshot() )
 76  
         {
 77  0
             Snapshot snapshot = new Snapshot();
 78  0
             snapshot.setLocalCopy( true );
 79  0
             RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
 80  
 
 81  0
             artifact.addMetadata( metadata );
 82  
         }
 83  1
     }
 84  
 
 85  
     public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository,
 86  
                                         ArtifactRepository localRepository )
 87  
         throws ArtifactDeploymentException
 88  
     {
 89  1
         if ( artifact.isSnapshot() )
 90  
         {
 91  0
             Snapshot snapshot = new Snapshot();
 92  0
             if ( remoteRepository.isUniqueVersion() )
 93  
             {
 94  0
                 snapshot.setTimestamp( getDeploymentTimestamp() );
 95  
             }
 96  
 
 97  
             // we update the build number anyway so that it doesn't get lost. It requires the timestamp to take effect
 98  
             try
 99  
             {
 100  0
                 int buildNumber = resolveLatestSnapshotBuildNumber( artifact, localRepository, remoteRepository );
 101  
 
 102  0
                 snapshot.setBuildNumber( buildNumber + 1 );
 103  
             }
 104  0
             catch ( RepositoryMetadataResolutionException e )
 105  
             {
 106  0
                 throw new ArtifactDeploymentException( "Error retrieving previous build number for artifact '" +
 107  
                     artifact.getDependencyConflictId() + "': " + e.getMessage(), e );
 108  0
             }
 109  
 
 110  0
             RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
 111  
 
 112  0
             artifact.setResolvedVersion(
 113  
                 constructVersion( metadata.getMetadata().getVersioning(), artifact.getBaseVersion() ) );
 114  
 
 115  0
             artifact.addMetadata( metadata );
 116  
         }
 117  1
     }
 118  
 
 119  
     public String getDeploymentTimestamp()
 120  
     {
 121  0
         if ( deploymentTimestamp == null )
 122  
         {
 123  0
             deploymentTimestamp = getUtcDateFormatter().format( new Date() );
 124  
         }
 125  0
         return deploymentTimestamp;
 126  
     }
 127  
 
 128  
     protected String constructVersion( Versioning versioning, String baseVersion )
 129  
     {
 130  0
         String version = null;
 131  0
         Snapshot snapshot = versioning.getSnapshot();
 132  0
         if ( snapshot != null )
 133  
         {
 134  0
             if ( snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0 )
 135  
             {
 136  0
                 String newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber();
 137  0
                 version = StringUtils.replace( baseVersion, "SNAPSHOT", newVersion );
 138  0
             }
 139  
             else
 140  
             {
 141  0
                 version = baseVersion;
 142  
             }
 143  
         }
 144  0
         return version;
 145  
     }
 146  
 
 147  
     private int resolveLatestSnapshotBuildNumber( Artifact artifact, ArtifactRepository localRepository,
 148  
                                                   ArtifactRepository remoteRepository )
 149  
         throws RepositoryMetadataResolutionException
 150  
     {
 151  0
         RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
 152  
 
 153  0
         if ( !wagonManager.isOnline() )
 154  
         {
 155  
             // build number is a required feature for metadata consistency
 156  0
             throw new RepositoryMetadataResolutionException(
 157  
                 "System is offline. Cannot resolve metadata:\n" + metadata.extendedToString() + "\n\n" );
 158  
         }
 159  
 
 160  0
         getLogger().info( "Retrieving previous build number from " + remoteRepository.getId() );
 161  0
         repositoryMetadataManager.resolveAlways( metadata, localRepository, remoteRepository );
 162  
 
 163  0
         int buildNumber = 0;
 164  0
         Metadata repoMetadata = metadata.getMetadata();
 165  0
         if ( ( repoMetadata != null )
 166  
             && ( repoMetadata.getVersioning() != null && repoMetadata.getVersioning().getSnapshot() != null ) )
 167  
         {
 168  0
             buildNumber = repoMetadata.getVersioning().getSnapshot().getBuildNumber();
 169  
         }
 170  0
         return buildNumber;
 171  
     }
 172  
 
 173  
     public static DateFormat getUtcDateFormatter()
 174  
     {
 175  0
         DateFormat utcDateFormatter = new SimpleDateFormat( UTC_TIMESTAMP_PATTERN );
 176  0
         utcDateFormatter.setTimeZone( UTC_TIME_ZONE );
 177  0
         return utcDateFormatter;
 178  
     }
 179  
 }