|
39 | 39 | import org.apache.maven.api.spi.ModelParser; |
40 | 40 | import org.apache.maven.api.spi.ModelParserException; |
41 | 41 |
|
| 42 | +import static java.util.Objects.requireNonNull; |
| 43 | +import static org.apache.maven.api.spi.ModelParser.STRICT; |
| 44 | + |
42 | 45 | /** |
43 | 46 | * |
44 | 47 | * Note: uses @Typed to limit the types it is available for injection to just ModelProcessor. |
@@ -94,50 +97,45 @@ public Path locateExistingPom(Path projectDirectory) { |
94 | 97 | return pom; |
95 | 98 | } |
96 | 99 |
|
| 100 | + private Path doLocateExistingPom(Path project) { |
| 101 | + if (project == null) { |
| 102 | + project = Paths.get(System.getProperty("user.dir")); |
| 103 | + } else if (Files.isDirectory(project)) { |
| 104 | + Path pom = project.resolve("pom.xml"); |
| 105 | + return Files.isRegularFile(pom) ? pom : null; |
| 106 | + } |
| 107 | + return project; |
| 108 | + } |
| 109 | + |
97 | 110 | @Override |
98 | 111 | public Model read(XmlReaderRequest request) throws IOException { |
99 | | - Objects.requireNonNull(request, "source cannot be null"); |
100 | | - Path pomFile = request.getPath(); |
| 112 | + Path pomFile = requireNonNull(request, "source cannot be null").getPath(); |
101 | 113 | if (pomFile != null) { |
102 | | - Path projectDirectory = pomFile.getParent(); |
103 | 114 | List<ModelParserException> exceptions = new ArrayList<>(); |
104 | 115 | for (ModelParser parser : modelParsers) { |
105 | 116 | 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); |
| 117 | + Optional<Model> parent = parent(request, parser, pomFile.getParent()); |
| 118 | + if (parent.isPresent()) { |
| 119 | + return parent.get().withPomFile(pomFile); |
110 | 120 | } |
111 | 121 | } catch (ModelParserException e) { |
112 | 122 | exceptions.add(e); |
113 | 123 | } |
114 | 124 | } |
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); |
| 125 | + throwErrorsSuppressed(exceptions); |
123 | 126 | } |
| 127 | + return modelXmlFactory.read(request); |
124 | 128 | } |
125 | 129 |
|
126 | | - private Path doLocateExistingPom(Path project) { |
127 | | - if (project == null) { |
128 | | - project = Paths.get(System.getProperty("user.dir")); |
129 | | - } |
130 | | - if (Files.isDirectory(project)) { |
131 | | - Path pom = project.resolve("pom.xml"); |
132 | | - return Files.isRegularFile(pom) ? pom : null; |
133 | | - } else if (Files.isRegularFile(project)) { |
134 | | - return project; |
135 | | - } else { |
136 | | - return null; |
137 | | - } |
| 130 | + private static Optional<Model> parent(XmlReaderRequest request, ModelParser parser, Path projectDirectory) { |
| 131 | + return parser.locateAndParse(projectDirectory, Map.of(STRICT, request.isStrict())); |
138 | 132 | } |
139 | 133 |
|
140 | | - private Model doRead(XmlReaderRequest request) throws IOException { |
141 | | - return modelXmlFactory.read(request); |
| 134 | + private static void throwErrorsSuppressed(List<ModelParserException> exceptions) throws IOException { |
| 135 | + if (!exceptions.isEmpty()) { |
| 136 | + IOException ex = new IOException(); |
| 137 | + exceptions.forEach(ex::addSuppressed); |
| 138 | + throw ex; |
| 139 | + } |
142 | 140 | } |
143 | 141 | } |
0 commit comments