|
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. |
@@ -77,67 +80,69 @@ public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<Mod |
77 | 80 | this.modelParsers = modelParsers; |
78 | 81 | } |
79 | 82 |
|
| 83 | + /** |
| 84 | + * @implNote The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path! |
| 85 | + */ |
80 | 86 | @Override |
81 | 87 | public Path locateExistingPom(Path projectDirectory) { |
82 | | - // Note that the ModelProcessor#locatePom never returns null |
83 | | - // while the ModelParser#locatePom needs to return an existing path! |
84 | | - Path pom = modelParsers.stream() |
85 | | - .map(m -> m.locate(projectDirectory) |
86 | | - .map(org.apache.maven.api.services.Source::getPath) |
87 | | - .orElse(null)) |
88 | | - .filter(Objects::nonNull) |
89 | | - .findFirst() |
90 | | - .orElseGet(() -> doLocateExistingPom(projectDirectory)); |
| 88 | + return throwIfWrongProjectDirLocation( |
| 89 | + projectDirectory, |
| 90 | + modelParsers.stream() |
| 91 | + .map(m -> m.locate(projectDirectory) |
| 92 | + .map(org.apache.maven.api.services.Source::getPath) |
| 93 | + .orElse(null)) |
| 94 | + .filter(Objects::nonNull) |
| 95 | + .findFirst() |
| 96 | + .orElseGet(() -> locateExistingPomWithFallback(projectDirectory))); |
| 97 | + } |
| 98 | + |
| 99 | + private static Path throwIfWrongProjectDirLocation(Path projectDirectory, Path pom) { |
91 | 100 | if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) { |
92 | 101 | throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom); |
93 | 102 | } |
94 | 103 | return pom; |
95 | 104 | } |
96 | 105 |
|
| 106 | + private Path locateExistingPomWithFallback(Path project) { |
| 107 | + if (project != null) { |
| 108 | + if (Files.isDirectory(project)) { |
| 109 | + Path pom = project.resolve("pom.xml"); |
| 110 | + return Files.isRegularFile(pom) ? pom : null; |
| 111 | + } |
| 112 | + return Files.isRegularFile(project) ? project : null; |
| 113 | + } |
| 114 | + return locateExistingPomWithFallback(Paths.get(System.getProperty("user.dir"))); // check in user dir |
| 115 | + } |
| 116 | + |
97 | 117 | @Override |
98 | 118 | public Model read(XmlReaderRequest request) throws IOException { |
99 | | - Objects.requireNonNull(request, "source cannot be null"); |
100 | | - Path pomFile = request.getPath(); |
| 119 | + Path pomFile = requireNonNull(request, "source cannot be null").getPath(); |
101 | 120 | if (pomFile != null) { |
102 | | - Path projectDirectory = pomFile.getParent(); |
103 | 121 | List<ModelParserException> exceptions = new ArrayList<>(); |
104 | 122 | for (ModelParser parser : modelParsers) { |
105 | 123 | 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); |
| 124 | + Optional<Model> parent = parent(request, parser, pomFile.getParent()); |
| 125 | + if (parent.isPresent()) { |
| 126 | + return parent.get().withPomFile(pomFile); |
110 | 127 | } |
111 | 128 | } catch (ModelParserException e) { |
112 | 129 | exceptions.add(e); |
113 | 130 | } |
114 | 131 | } |
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); |
| 132 | + throwErrorsSuppressed(exceptions); |
123 | 133 | } |
| 134 | + return modelXmlFactory.read(request); |
124 | 135 | } |
125 | 136 |
|
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 | | - } |
| 137 | + private static Optional<Model> parent(XmlReaderRequest request, ModelParser parser, Path projectDirectory) { |
| 138 | + return parser.locateAndParse(projectDirectory, Map.of(STRICT, request.isStrict())); |
138 | 139 | } |
139 | 140 |
|
140 | | - private Model doRead(XmlReaderRequest request) throws IOException { |
141 | | - return modelXmlFactory.read(request); |
| 141 | + private static void throwErrorsSuppressed(List<ModelParserException> exceptions) throws IOException { |
| 142 | + if (!exceptions.isEmpty()) { |
| 143 | + IOException ex = new IOException(); |
| 144 | + exceptions.forEach(ex::addSuppressed); |
| 145 | + throw ex; |
| 146 | + } |
142 | 147 | } |
143 | 148 | } |
0 commit comments