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