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