View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository.internal;
20  
21  import java.io.File;
22  import java.text.DateFormat;
23  import java.text.SimpleDateFormat;
24  import java.util.ArrayList;
25  import java.util.Date;
26  import java.util.GregorianCalendar;
27  import java.util.LinkedHashMap;
28  import java.util.Map;
29  import java.util.TimeZone;
30  
31  import org.apache.maven.artifact.repository.metadata.Metadata;
32  import org.apache.maven.artifact.repository.metadata.Snapshot;
33  import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
34  import org.apache.maven.artifact.repository.metadata.Versioning;
35  import org.eclipse.aether.artifact.Artifact;
36  
37  /**
38   * Maven remote GAV level metadata.
39   */
40  final class RemoteSnapshotMetadata extends MavenSnapshotMetadata {
41      public static final String DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT = "yyyyMMdd.HHmmss";
42  
43      public static final TimeZone DEFAULT_SNAPSHOT_TIME_ZONE = TimeZone.getTimeZone("Etc/UTC");
44  
45      private final Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
46  
47      private final Integer buildNumber;
48  
49      RemoteSnapshotMetadata(Artifact artifact, boolean legacyFormat, Date timestamp, Integer buildNumber) {
50          super(createRepositoryMetadata(artifact, legacyFormat), null, legacyFormat, timestamp);
51          this.buildNumber = buildNumber;
52      }
53  
54      private RemoteSnapshotMetadata(
55              Metadata metadata, File file, boolean legacyFormat, Date timestamp, Integer buildNumber) {
56          super(metadata, file, legacyFormat, timestamp);
57          this.buildNumber = buildNumber;
58      }
59  
60      @Override
61      public MavenMetadata setFile(File file) {
62          return new RemoteSnapshotMetadata(metadata, file, legacyFormat, timestamp, buildNumber);
63      }
64  
65      public String getExpandedVersion(Artifact artifact) {
66          String key = getKey(artifact.getClassifier(), artifact.getExtension());
67          return versions.get(key).getVersion();
68      }
69  
70      @Override
71      protected void merge(Metadata recessive) {
72          Snapshot snapshot;
73          String lastUpdated;
74  
75          if (metadata.getVersioning() == null) {
76              DateFormat utcDateFormatter = new SimpleDateFormat(DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT);
77              utcDateFormatter.setCalendar(new GregorianCalendar());
78              utcDateFormatter.setTimeZone(DEFAULT_SNAPSHOT_TIME_ZONE);
79  
80              snapshot = new Snapshot();
81              snapshot.setBuildNumber(buildNumber != null ? buildNumber : getBuildNumber(recessive) + 1);
82              snapshot.setTimestamp(utcDateFormatter.format(timestamp));
83  
84              Versioning versioning = new Versioning();
85              versioning.setSnapshot(snapshot);
86              versioning.setLastUpdatedTimestamp(timestamp);
87              lastUpdated = versioning.getLastUpdated();
88  
89              metadata.setVersioning(versioning);
90          } else {
91              snapshot = metadata.getVersioning().getSnapshot();
92              lastUpdated = metadata.getVersioning().getLastUpdated();
93          }
94  
95          for (Artifact artifact : artifacts) {
96              String version = artifact.getVersion();
97  
98              if (version.endsWith(SNAPSHOT)) {
99                  String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
100                 version = version.substring(0, version.length() - SNAPSHOT.length()) + qualifier;
101             }
102 
103             SnapshotVersion sv = new SnapshotVersion();
104             sv.setClassifier(artifact.getClassifier());
105             sv.setExtension(artifact.getExtension());
106             sv.setVersion(version);
107             sv.setUpdated(lastUpdated);
108 
109             versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
110         }
111 
112         artifacts.clear();
113 
114         Versioning versioning = recessive.getVersioning();
115         if (versioning != null) {
116             for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
117                 String key = getKey(sv.getClassifier(), sv.getExtension());
118                 if (!versions.containsKey(key)) {
119                     versions.put(key, sv);
120                 }
121             }
122         }
123 
124         if (!legacyFormat) {
125             metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
126         }
127         // just carry-on as-is
128         if (!recessive.getPlugins().isEmpty()) {
129             metadata.setPlugins(new ArrayList<>(recessive.getPlugins()));
130         }
131     }
132 
133     private static int getBuildNumber(Metadata metadata) {
134         int number = 0;
135 
136         Versioning versioning = metadata.getVersioning();
137         if (versioning != null) {
138             Snapshot snapshot = versioning.getSnapshot();
139             if (snapshot != null && snapshot.getBuildNumber() > 0) {
140                 number = snapshot.getBuildNumber();
141             }
142         }
143 
144         return number;
145     }
146 }