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.model;
20
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Optional;
30
31 import org.apache.maven.api.di.Inject;
32 import org.apache.maven.api.di.Named;
33 import org.apache.maven.api.di.Singleton;
34 import org.apache.maven.api.model.Model;
35 import org.apache.maven.api.services.model.ModelProcessor;
36 import org.apache.maven.api.services.xml.ModelXmlFactory;
37 import org.apache.maven.api.services.xml.XmlReaderRequest;
38 import org.apache.maven.api.spi.ModelParser;
39 import org.apache.maven.api.spi.ModelParserException;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @Named
67 @Singleton
68 public class DefaultModelProcessor implements ModelProcessor {
69
70 private final ModelXmlFactory modelXmlFactory;
71 private final List<ModelParser> modelParsers;
72
73 @Inject
74 public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, List<ModelParser> modelParsers) {
75 this.modelXmlFactory = modelXmlFactory;
76 this.modelParsers = modelParsers;
77 }
78
79 @Override
80 public Path locateExistingPom(Path projectDirectory) {
81
82
83 Path pom = modelParsers.stream()
84 .map(m -> m.locate(projectDirectory)
85 .map(org.apache.maven.api.services.Source::getPath)
86 .orElse(null))
87 .filter(Objects::nonNull)
88 .findFirst()
89 .orElseGet(() -> doLocateExistingPom(projectDirectory));
90 if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
91 throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
92 }
93 return pom;
94 }
95
96 @Override
97 public Model read(XmlReaderRequest request) throws IOException {
98 Objects.requireNonNull(request, "source cannot be null");
99 Path pomFile = request.getPath();
100 if (pomFile != null) {
101 Path projectDirectory = pomFile.getParent();
102 List<ModelParserException> exceptions = new ArrayList<>();
103 for (ModelParser parser : modelParsers) {
104 try {
105 Optional<Model> model =
106 parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
107 if (model.isPresent()) {
108 return model.get().withPomFile(pomFile);
109 }
110 } catch (ModelParserException e) {
111 exceptions.add(e);
112 }
113 }
114 try {
115 return doRead(request);
116 } catch (IOException e) {
117 exceptions.forEach(e::addSuppressed);
118 throw e;
119 }
120 } else {
121 return doRead(request);
122 }
123 }
124
125 private Path doLocateExistingPom(Path project) {
126 if (project == null) {
127 project = Paths.get(System.getProperty("user.dir"));
128 }
129 if (Files.isDirectory(project)) {
130 Path pom = project.resolve("pom.xml");
131 return Files.isRegularFile(pom) ? pom : null;
132 } else if (Files.isRegularFile(project)) {
133 return project;
134 } else {
135 return null;
136 }
137 }
138
139 private Model doRead(XmlReaderRequest request) throws IOException {
140 return modelXmlFactory.read(request);
141 }
142 }