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