1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.internal;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import org.apache.maven.artifact.repository.metadata.Metadata;
28 import org.apache.maven.artifact.repository.metadata.Snapshot;
29 import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
30 import org.apache.maven.artifact.repository.metadata.Versioning;
31 import org.eclipse.aether.artifact.Artifact;
32
33
34
35
36 final class LocalSnapshotMetadata extends MavenMetadata {
37
38 private final Collection<Artifact> artifacts = new ArrayList<>();
39
40 private final boolean legacyFormat;
41
42 LocalSnapshotMetadata(Artifact artifact, boolean legacyFormat, Date timestamp) {
43 super(createMetadata(artifact, legacyFormat), null, timestamp);
44 this.legacyFormat = legacyFormat;
45 }
46
47 LocalSnapshotMetadata(Metadata metadata, File file, boolean legacyFormat, Date timestamp) {
48 super(metadata, file, timestamp);
49 this.legacyFormat = legacyFormat;
50 }
51
52 private static Metadata createMetadata(Artifact artifact, boolean legacyFormat) {
53 Snapshot snapshot = new Snapshot();
54 snapshot.setLocalCopy(true);
55 Versioning versioning = new Versioning();
56 versioning.setSnapshot(snapshot);
57
58 Metadata metadata = new Metadata();
59 metadata.setVersioning(versioning);
60 metadata.setGroupId(artifact.getGroupId());
61 metadata.setArtifactId(artifact.getArtifactId());
62 metadata.setVersion(artifact.getBaseVersion());
63
64 if (!legacyFormat) {
65 metadata.setModelVersion("1.1.0");
66 }
67
68 return metadata;
69 }
70
71 public void bind(Artifact artifact) {
72 artifacts.add(artifact);
73 }
74
75 public MavenMetadata setFile(File file) {
76 return new LocalSnapshotMetadata(metadata, file, legacyFormat, timestamp);
77 }
78
79 public Object getKey() {
80 return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
81 }
82
83 public static Object getKey(Artifact artifact) {
84 return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
85 }
86
87 @Override
88 protected void merge(Metadata recessive) {
89 metadata.getVersioning().setLastUpdatedTimestamp(timestamp);
90
91 if (!legacyFormat) {
92 String lastUpdated = metadata.getVersioning().getLastUpdated();
93
94 Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
95
96 for (Artifact artifact : artifacts) {
97 SnapshotVersion sv = new SnapshotVersion();
98 sv.setClassifier(artifact.getClassifier());
99 sv.setExtension(artifact.getExtension());
100 sv.setVersion(getVersion());
101 sv.setUpdated(lastUpdated);
102 versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
103 }
104
105 Versioning versioning = recessive.getVersioning();
106 if (versioning != null) {
107 for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
108 String key = getKey(sv.getClassifier(), sv.getExtension());
109 if (!versions.containsKey(key)) {
110 versions.put(key, sv);
111 }
112 }
113 }
114
115 metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
116 }
117
118 artifacts.clear();
119 }
120
121 private String getKey(String classifier, String extension) {
122 return classifier + ':' + extension;
123 }
124
125 public String getGroupId() {
126 return metadata.getGroupId();
127 }
128
129 public String getArtifactId() {
130 return metadata.getArtifactId();
131 }
132
133 public String getVersion() {
134 return metadata.getVersion();
135 }
136
137 public Nature getNature() {
138 return Nature.SNAPSHOT;
139 }
140 }