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