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