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