1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.internal.ant;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.concurrent.ConcurrentHashMap;
26
27 import org.apache.maven.model.Model;
28 import org.apache.maven.resolver.internal.ant.types.Pom;
29 import org.eclipse.aether.artifact.Artifact;
30 import org.eclipse.aether.artifact.DefaultArtifact;
31 import org.eclipse.aether.repository.WorkspaceReader;
32 import org.eclipse.aether.repository.WorkspaceRepository;
33 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
34
35
36
37
38
39
40
41
42 public class ProjectWorkspaceReader implements WorkspaceReader {
43
44 private static volatile ProjectWorkspaceReader instance;
45
46 private static final Object LOCK = new Object();
47
48 private Map<String, Artifact> artifacts = new ConcurrentHashMap<>();
49
50 public void addPom(Pom pom) {
51 if (pom.getFile() != null) {
52 Model model = pom.getModel(pom);
53 Artifact aetherArtifact =
54 new DefaultArtifact(model.getGroupId(), model.getArtifactId(), null, "pom", model.getVersion());
55 aetherArtifact = aetherArtifact.setFile(pom.getFile());
56 String coords = coords(aetherArtifact);
57 artifacts.put(coords, aetherArtifact);
58 }
59 }
60
61 public void addArtifact(org.apache.maven.resolver.internal.ant.types.Artifact artifact) {
62 if (artifact.getPom() != null) {
63 Pom pom = artifact.getPom();
64 Artifact aetherArtifact;
65 if (pom.getFile() != null) {
66 Model model = pom.getModel(pom);
67 aetherArtifact = new DefaultArtifact(
68 model.getGroupId(),
69 model.getArtifactId(),
70 artifact.getClassifier(),
71 artifact.getType(),
72 model.getVersion());
73 } else {
74 aetherArtifact = new DefaultArtifact(
75 pom.getGroupId(),
76 pom.getArtifactId(),
77 artifact.getClassifier(),
78 artifact.getType(),
79 pom.getVersion());
80 }
81 aetherArtifact = aetherArtifact.setFile(artifact.getFile());
82
83 String coords = coords(aetherArtifact);
84 artifacts.put(coords, aetherArtifact);
85 }
86 }
87
88 private String coords(Artifact artifact) {
89 return ArtifactIdUtils.toId(artifact);
90 }
91
92 public WorkspaceRepository getRepository() {
93 return new WorkspaceRepository("ant");
94 }
95
96 public File findArtifact(Artifact artifact) {
97 artifact = artifacts.get(coords(artifact));
98 return (artifact != null) ? artifact.getFile() : null;
99 }
100
101 public List<String> findVersions(Artifact artifact) {
102 List<String> versions = new ArrayList<>();
103 for (Artifact art : artifacts.values()) {
104 if (ArtifactIdUtils.equalsVersionlessId(artifact, art)) {
105 versions.add(art.getVersion());
106 }
107 }
108 return versions;
109 }
110
111 ProjectWorkspaceReader() {}
112
113 public static ProjectWorkspaceReader getInstance() {
114 if (instance == null) {
115 synchronized (LOCK) {
116 if (instance == null) {
117 instance = new ProjectWorkspaceReader();
118 }
119 }
120 }
121 return instance;
122 }
123
124 static void dropInstance() {
125 instance = null;
126 }
127 }