1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.io.File;
22 import java.nio.file.Path;
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.concurrent.ConcurrentHashMap;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import org.apache.maven.SessionScoped;
29 import org.apache.maven.api.Artifact;
30 import org.apache.maven.api.annotations.Nonnull;
31 import org.apache.maven.api.services.ArtifactManager;
32 import org.apache.maven.project.MavenProject;
33
34 @Named
35 @SessionScoped
36 public class DefaultArtifactManager implements ArtifactManager {
37
38 @Nonnull
39 private final DefaultSession session;
40
41 private final Map<String, Path> paths = new ConcurrentHashMap<>();
42
43 @Inject
44 public DefaultArtifactManager(@Nonnull DefaultSession session) {
45 this.session = session;
46 }
47
48 @Nonnull
49 @Override
50 public Optional<Path> getPath(@Nonnull Artifact artifact) {
51 String id = id(artifact);
52 if (session.getMavenSession().getAllProjects() != null) {
53 for (MavenProject project : session.getMavenSession().getAllProjects()) {
54 if (id.equals(id(project.getArtifact()))
55 && project.getArtifact().getFile() != null) {
56 return Optional.of(project.getArtifact().getFile().toPath());
57 }
58 }
59 }
60 Path path = paths.get(id);
61 if (path == null && artifact instanceof DefaultArtifact) {
62 File file = ((DefaultArtifact) artifact).getArtifact().getFile();
63 if (file != null) {
64 path = file.toPath();
65 }
66 }
67 return Optional.ofNullable(path);
68 }
69
70 @Override
71 public void setPath(@Nonnull Artifact artifact, Path path) {
72 String id = id(artifact);
73 if (session.getMavenSession().getAllProjects() != null) {
74 for (MavenProject project : session.getMavenSession().getAllProjects()) {
75 if (id.equals(id(project.getArtifact()))) {
76 project.getArtifact().setFile(path != null ? path.toFile() : null);
77 break;
78 }
79 }
80 }
81 if (path == null) {
82 paths.remove(id);
83 } else {
84 paths.put(id, path);
85 }
86 }
87
88 private String id(org.apache.maven.artifact.Artifact artifact) {
89 return artifact.getGroupId()
90 + ":" + artifact.getArtifactId()
91 + ":" + artifact.getType()
92 + (artifact.getClassifier() == null || artifact.getClassifier().isEmpty()
93 ? ""
94 : ":" + artifact.getClassifier())
95 + ":" + artifact.getVersion();
96 }
97
98 private String id(Artifact artifact) {
99 return artifact.key();
100 }
101 }