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.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  class VersionsMetadataGenerator implements MetadataGenerator {
42  
43      private final Map<Object, VersionsMetadata> versions;
44  
45      private final Map<Object, VersionsMetadata> processedVersions;
46  
47      private final Date timestamp;
48  
49      VersionsMetadataGenerator(RepositorySystemSession session, InstallRequest request) {
50          this(session, request.getMetadata());
51      }
52  
53      VersionsMetadataGenerator(RepositorySystemSession session, DeployRequest request) {
54          this(session, request.getMetadata());
55      }
56  
57      private VersionsMetadataGenerator(RepositorySystemSession session, Collection<? extends Metadata> metadatas) {
58          versions = new LinkedHashMap<>();
59          processedVersions = new LinkedHashMap<>();
60          timestamp = (Date) ConfigUtils.getObject(session, new Date(), "maven.startTime");
61  
62          /*
63           * NOTE: This should be considered a quirk to support interop with Maven's legacy ArtifactDeployer which
64           * processes one artifact at a time and hence cannot associate the artifacts from the same project to use the
65           * same version index. Allowing the caller to pass in metadata from a previous deployment allows to re-establish
66           * the association between the artifacts of the same project.
67           */
68          for (Iterator<? extends Metadata> it = metadatas.iterator(); it.hasNext(); ) {
69              Metadata metadata = it.next();
70              if (metadata instanceof VersionsMetadata) {
71                  it.remove();
72                  VersionsMetadata versionsMetadata = (VersionsMetadata) metadata;
73                  processedVersions.put(versionsMetadata.getKey(), versionsMetadata);
74              }
75          }
76      }
77  
78      @Override
79      public Collection<? extends Metadata> prepare(Collection<? extends Artifact> artifacts) {
80          return Collections.emptyList();
81      }
82  
83      @Override
84      public Artifact transformArtifact(Artifact artifact) {
85          return artifact;
86      }
87  
88      @Override
89      public Collection<? extends Metadata> finish(Collection<? extends Artifact> artifacts) {
90          for (Artifact artifact : artifacts) {
91              Object key = VersionsMetadata.getKey(artifact);
92              if (processedVersions.get(key) == null) {
93                  VersionsMetadata versionsMetadata = versions.get(key);
94                  if (versionsMetadata == null) {
95                      versionsMetadata = new VersionsMetadata(artifact, timestamp);
96                      versions.put(key, versionsMetadata);
97                  }
98              }
99          }
100 
101         return versions.values();
102     }
103 }