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.LinkedHashSet;
26  
27  import org.apache.maven.artifact.repository.metadata.Metadata;
28  import org.apache.maven.artifact.repository.metadata.Versioning;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.artifact.ArtifactProperties;
31  
32  /**
33   * Maven GA level metadata.
34   */
35  final class VersionsMetadata extends MavenMetadata {
36  
37      private final Artifact artifact;
38  
39      VersionsMetadata(Artifact artifact, Date timestamp) {
40          super(createRepositoryMetadata(artifact), null, timestamp);
41          this.artifact = artifact;
42      }
43  
44      VersionsMetadata(Artifact artifact, File file, Date timestamp) {
45          super(createRepositoryMetadata(artifact), file, timestamp);
46          this.artifact = artifact;
47      }
48  
49      private static Metadata createRepositoryMetadata(Artifact artifact) {
50          Metadata metadata = new Metadata();
51          metadata.setGroupId(artifact.getGroupId());
52          metadata.setArtifactId(artifact.getArtifactId());
53  
54          Versioning versioning = new Versioning();
55          versioning.addVersion(artifact.getBaseVersion());
56          if (!artifact.isSnapshot()) {
57              versioning.setRelease(artifact.getBaseVersion());
58          }
59          if ("maven-plugin".equals(artifact.getProperty(ArtifactProperties.TYPE, ""))) {
60              versioning.setLatest(artifact.getBaseVersion());
61          }
62  
63          metadata.setVersioning(versioning);
64  
65          return metadata;
66      }
67  
68      @Override
69      protected void merge(Metadata recessive) {
70          Versioning versioning = metadata.getVersioning();
71          versioning.setLastUpdatedTimestamp(timestamp);
72  
73          if (recessive.getVersioning() != null) {
74              if (versioning.getLatest() == null) {
75                  versioning.setLatest(recessive.getVersioning().getLatest());
76              }
77              if (versioning.getRelease() == null) {
78                  versioning.setRelease(recessive.getVersioning().getRelease());
79              }
80  
81              Collection<String> versions =
82                      new LinkedHashSet<>(recessive.getVersioning().getVersions());
83              versions.addAll(versioning.getVersions());
84              versioning.setVersions(new ArrayList<>(versions));
85          }
86      }
87  
88      public Object getKey() {
89          return getGroupId() + ':' + getArtifactId();
90      }
91  
92      public static Object getKey(Artifact artifact) {
93          return artifact.getGroupId() + ':' + artifact.getArtifactId();
94      }
95  
96      @Override
97      public MavenMetadata setFile(File file) {
98          return new VersionsMetadata(artifact, file, timestamp);
99      }
100 
101     @Override
102     public String getGroupId() {
103         return artifact.getGroupId();
104     }
105 
106     @Override
107     public String getArtifactId() {
108         return artifact.getArtifactId();
109     }
110 
111     @Override
112     public String getVersion() {
113         return "";
114     }
115 
116     @Override
117     public Nature getNature() {
118         return artifact.isSnapshot() ? Nature.RELEASE_OR_SNAPSHOT : Nature.RELEASE;
119     }
120 }