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