View Javadoc

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  public class SnapshotTransformation
47      extends AbstractVersionTransformation
48  {
49      private String deploymentTimestamp;
50  
51      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          if ( artifact.isSnapshot() && artifact.getBaseVersion().equals( artifact.getVersion() ) )
60          {
61              try
62              {
63                  String version = resolveVersion( artifact, localRepository, remoteRepositories );
64                  artifact.updateVersion( version, localRepository );
65              }
66              catch ( RepositoryMetadataResolutionException e )
67              {
68                  throw new ArtifactResolutionException( e.getMessage(), artifact, e );
69              }
70          }
71      }
72  
73      public void transformForInstall( Artifact artifact, ArtifactRepository localRepository )
74      {
75          if ( artifact.isSnapshot() )
76          {
77              Snapshot snapshot = new Snapshot();
78              snapshot.setLocalCopy( true );
79              RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
80  
81              artifact.addMetadata( metadata );
82          }
83      }
84  
85      public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository,
86                                          ArtifactRepository localRepository )
87          throws ArtifactDeploymentException
88      {
89          if ( artifact.isSnapshot() )
90          {
91              Snapshot snapshot = new Snapshot();
92              if ( remoteRepository.isUniqueVersion() )
93              {
94                  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                 int buildNumber = resolveLatestSnapshotBuildNumber( artifact, localRepository, remoteRepository );
101 
102                 snapshot.setBuildNumber( buildNumber + 1 );
103             }
104             catch ( RepositoryMetadataResolutionException e )
105             {
106                 throw new ArtifactDeploymentException( "Error retrieving previous build number for artifact '" +
107                     artifact.getDependencyConflictId() + "': " + e.getMessage(), e );
108             }
109 
110             RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
111 
112             artifact.setResolvedVersion(
113                 constructVersion( metadata.getMetadata().getVersioning(), artifact.getBaseVersion() ) );
114 
115             artifact.addMetadata( metadata );
116         }
117     }
118 
119     public String getDeploymentTimestamp()
120     {
121         if ( deploymentTimestamp == null )
122         {
123             deploymentTimestamp = getUtcDateFormatter().format( new Date() );
124         }
125         return deploymentTimestamp;
126     }
127 
128     protected String constructVersion( Versioning versioning, String baseVersion )
129     {
130         String version = null;
131         Snapshot snapshot = versioning.getSnapshot();
132         if ( snapshot != null )
133         {
134             if ( snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0 )
135             {
136                 String newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber();
137                 version = StringUtils.replace( baseVersion, "SNAPSHOT", newVersion );
138             }
139             else
140             {
141                 version = baseVersion;
142             }
143         }
144         return version;
145     }
146 
147     private int resolveLatestSnapshotBuildNumber( Artifact artifact, ArtifactRepository localRepository,
148                                                   ArtifactRepository remoteRepository )
149         throws RepositoryMetadataResolutionException
150     {
151         RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
152 
153         if ( !wagonManager.isOnline() )
154         {
155             // build number is a required feature for metadata consistency
156             throw new RepositoryMetadataResolutionException(
157                 "System is offline. Cannot resolve metadata:\n" + metadata.extendedToString() + "\n\n" );
158         }
159 
160         getLogger().info( "Retrieving previous build number from " + remoteRepository.getId() );
161         repositoryMetadataManager.resolveAlways( metadata, localRepository, remoteRepository );
162 
163         int buildNumber = 0;
164         Metadata repoMetadata = metadata.getMetadata();
165         if ( ( repoMetadata != null )
166             && ( repoMetadata.getVersioning() != null && repoMetadata.getVersioning().getSnapshot() != null ) )
167         {
168             buildNumber = repoMetadata.getVersioning().getSnapshot().getBuildNumber();
169         }
170         return buildNumber;
171     }
172 
173     public static DateFormat getUtcDateFormatter()
174     {
175         DateFormat utcDateFormatter = new SimpleDateFormat( UTC_TIMESTAMP_PATTERN );
176         utcDateFormatter.setTimeZone( UTC_TIME_ZONE );
177         return utcDateFormatter;
178     }
179 }