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.impl.resolver;
20  
21  import java.time.Instant;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.LinkedHashMap;
26  import java.util.Map;
27  
28  import org.apache.maven.api.Constants;
29  import org.apache.maven.api.MonotonicClock;
30  import org.eclipse.aether.RepositorySystemSession;
31  import org.eclipse.aether.artifact.Artifact;
32  import org.eclipse.aether.deployment.DeployRequest;
33  import org.eclipse.aether.impl.MetadataGenerator;
34  import org.eclipse.aether.installation.InstallRequest;
35  import org.eclipse.aether.metadata.Metadata;
36  import org.eclipse.aether.util.ConfigUtils;
37  
38  /**
39   * Maven GA level metadata generator.
40   *
41   * Version metadata contains list of existing baseVersions within this GA.
42   */
43  class VersionsMetadataGenerator implements MetadataGenerator {
44  
45      private final Map<Object, VersionsMetadata> versions;
46  
47      private final Map<Object, VersionsMetadata> processedVersions;
48  
49      private final Instant timestamp;
50  
51      VersionsMetadataGenerator(RepositorySystemSession session, InstallRequest request) {
52          this(session, request.getMetadata());
53      }
54  
55      VersionsMetadataGenerator(RepositorySystemSession session, DeployRequest request) {
56          this(session, request.getMetadata());
57      }
58  
59      private VersionsMetadataGenerator(RepositorySystemSession session, Collection<? extends Metadata> metadatas) {
60          versions = new LinkedHashMap<>();
61          processedVersions = new LinkedHashMap<>();
62          timestamp = (Instant) ConfigUtils.getObject(session, MonotonicClock.now(), Constants.MAVEN_START_INSTANT);
63  
64          /*
65           * NOTE: This should be considered a quirk to support interop with Maven's legacy ArtifactDeployer which
66           * processes one artifact at a time and hence cannot associate the artifacts from the same project to use the
67           * same version index. Allowing the caller to pass in metadata from a previous deployment allows to re-establish
68           * the association between the artifacts of the same project.
69           */
70          for (Iterator<? extends Metadata> it = metadatas.iterator(); it.hasNext(); ) {
71              Metadata metadata = it.next();
72              if (metadata instanceof VersionsMetadata versionsMetadata) {
73                  it.remove();
74                  processedVersions.put(versionsMetadata.getKey(), versionsMetadata);
75              }
76          }
77      }
78  
79      @Override
80      public Collection<? extends Metadata> prepare(Collection<? extends Artifact> artifacts) {
81          return Collections.emptyList();
82      }
83  
84      @Override
85      public Artifact transformArtifact(Artifact artifact) {
86          return artifact;
87      }
88  
89      @Override
90      public Collection<? extends Metadata> finish(Collection<? extends Artifact> artifacts) {
91          for (Artifact artifact : artifacts) {
92              Object key = VersionsMetadata.getKey(artifact);
93              if (processedVersions.get(key) == null) {
94                  VersionsMetadata versionsMetadata = versions.get(key);
95                  if (versionsMetadata == null) {
96                      versionsMetadata = new VersionsMetadata(artifact, timestamp);
97                      versions.put(key, versionsMetadata);
98                  }
99              }
100         }
101 
102         return versions.values();
103     }
104 }