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