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;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.io.File;
25  import java.nio.file.Path;
26  import java.util.Map;
27  import java.util.Objects;
28  import java.util.Optional;
29  import java.util.concurrent.ConcurrentHashMap;
30  import java.util.stream.Stream;
31  
32  import org.apache.maven.api.Artifact;
33  import org.apache.maven.api.annotations.Nonnull;
34  import org.apache.maven.api.di.SessionScoped;
35  import org.apache.maven.api.services.ArtifactManager;
36  import org.apache.maven.project.MavenProject;
37  import org.eclipse.sisu.Typed;
38  
39  @Named
40  @Typed
41  @SessionScoped
42  public class DefaultArtifactManager implements ArtifactManager {
43  
44      @Nonnull
45      private final InternalSession session;
46  
47      private final Map<String, Path> paths = new ConcurrentHashMap<>();
48  
49      @Inject
50      public DefaultArtifactManager(@Nonnull InternalSession session) {
51          this.session = session;
52      }
53  
54      @Nonnull
55      @Override
56      public Optional<Path> getPath(@Nonnull Artifact artifact) {
57          String id = id(artifact);
58          if (session.getMavenSession().getAllProjects() != null) {
59              for (MavenProject project : session.getMavenSession().getAllProjects()) {
60                  if (id.equals(id(project.getArtifact()))
61                          && project.getArtifact().getFile() != null) {
62                      return Optional.of(project.getArtifact().getFile().toPath());
63                  }
64              }
65          }
66          Path path = paths.get(id);
67          if (path == null && artifact instanceof DefaultArtifact) {
68              File file = ((DefaultArtifact) artifact).getArtifact().getFile();
69              if (file != null) {
70                  path = file.toPath();
71              }
72          }
73          return Optional.ofNullable(path);
74      }
75  
76      @Override
77      public void setPath(@Nonnull Artifact artifact, Path path) {
78          String id = id(artifact);
79          if (session.getMavenSession().getAllProjects() != null) {
80              session.getMavenSession().getAllProjects().stream()
81                      .flatMap(this::getProjectArtifacts)
82                      .filter(a -> Objects.equals(id, id(a)))
83                      .forEach(a -> a.setFile(path != null ? path.toFile() : null));
84          }
85          if (path == null) {
86              paths.remove(id);
87          } else {
88              paths.put(id, path);
89          }
90      }
91  
92      /**
93       * Retrieve a stream of the project's artifacts.
94       * Do not include the POM artifact as the file can't be set anyway.
95       */
96      private Stream<org.apache.maven.artifact.Artifact> getProjectArtifacts(MavenProject project) {
97          return Stream.concat(Stream.of(project.getArtifact()), project.getAttachedArtifacts().stream());
98      }
99  
100     private String id(org.apache.maven.artifact.Artifact artifact) {
101         return artifact.getGroupId()
102                 + ":" + artifact.getArtifactId()
103                 + ":" + artifact.getArtifactHandler().getExtension()
104                 + (artifact.getClassifier() == null || artifact.getClassifier().isEmpty()
105                         ? ""
106                         : ":" + artifact.getClassifier())
107                 + ":" + artifact.getVersion();
108     }
109 
110     private String id(Artifact artifact) {
111         return artifact.key();
112     }
113 }