|
22 | 22 | import java.nio.file.Files; |
23 | 23 | import java.nio.file.Path; |
24 | 24 | import java.nio.file.Paths; |
25 | | -import java.util.ArrayList; |
26 | 25 | import java.util.List; |
27 | 26 | import java.util.Map; |
28 | 27 | import java.util.Objects; |
|
37 | 36 | import org.apache.maven.api.services.xml.ModelXmlFactory; |
38 | 37 | import org.apache.maven.api.services.xml.XmlReaderRequest; |
39 | 38 | import org.apache.maven.api.spi.ModelParser; |
40 | | -import org.apache.maven.api.spi.ModelParserException; |
41 | 39 |
|
42 | 40 | /** |
43 | 41 | * |
@@ -97,30 +95,20 @@ public Path locateExistingPom(Path projectDirectory) { |
97 | 95 | @Override |
98 | 96 | public Model read(XmlReaderRequest request) throws IOException { |
99 | 97 | Objects.requireNonNull(request, "source cannot be null"); |
100 | | - Path pomFile = request.getPath(); |
101 | | - if (pomFile != null) { |
102 | | - Path projectDirectory = pomFile.getParent(); |
103 | | - List<ModelParserException> exceptions = new ArrayList<>(); |
104 | | - for (ModelParser parser : modelParsers) { |
105 | | - try { |
106 | | - Optional<Model> model = |
107 | | - parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict())); |
108 | | - if (model.isPresent()) { |
109 | | - return model.get().withPomFile(pomFile); |
110 | | - } |
111 | | - } catch (ModelParserException e) { |
112 | | - exceptions.add(e); |
113 | | - } |
114 | | - } |
115 | | - try { |
116 | | - return doRead(request); |
117 | | - } catch (IOException e) { |
118 | | - exceptions.forEach(e::addSuppressed); |
119 | | - throw e; |
120 | | - } |
121 | | - } else { |
122 | | - return doRead(request); |
123 | | - } |
| 98 | + return read(request, request.getPath()); |
| 99 | + } |
| 100 | + |
| 101 | + private Model read(XmlReaderRequest request, Path pomFile) throws IOException { |
| 102 | + return pomFile == null |
| 103 | + ? doRead(request) |
| 104 | + : modelParsers.stream() |
| 105 | + .map(parser -> parser.locateAndParse( |
| 106 | + pomFile.getParent(), Map.of(ModelParser.STRICT, request.isStrict()))) |
| 107 | + .filter(Optional::isPresent) |
| 108 | + .map(Optional::get) |
| 109 | + .map(model -> model.withPomFile(pomFile)) |
| 110 | + .findFirst() |
| 111 | + .orElse(doRead(request)); |
124 | 112 | } |
125 | 113 |
|
126 | 114 | private Path doLocateExistingPom(Path project) { |
|
0 commit comments