1 | |
package org.apache.maven.artifact.transform; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 11 | 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 remoteRepositories, ArtifactRepository localRepository ) |
56 | |
throws ArtifactResolutionException |
57 | |
{ |
58 | |
|
59 | 13 | 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 | 13 | } |
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 | |
|
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 | |
} |
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 | |
|
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 | |
} |