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 javax.xml.stream.XMLInputFactory;
22 import javax.xml.stream.XMLStreamException;
23 import javax.xml.stream.XMLStreamReader;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29
30 import org.apache.maven.api.di.Named;
31 import org.apache.maven.api.services.model.RootLocator;
32
33 @Named
34 public class DefaultRootLocator implements RootLocator {
35
36 public boolean isRootDirectory(Path dir) {
37 if (Files.isDirectory(dir.resolve(".mvn"))) {
38 return true;
39 }
40
41 Path pom = dir.resolve("pom.xml");
42 try (InputStream is = Files.newInputStream(pom)) {
43 XMLStreamReader parser = XMLInputFactory.newFactory().createXMLStreamReader(is);
44 if (parser.nextTag() == XMLStreamReader.START_ELEMENT
45 && parser.getLocalName().equals("project")) {
46 for (int i = 0; i < parser.getAttributeCount(); i++) {
47 if ("root".equals(parser.getAttributeLocalName(i))) {
48 return Boolean.parseBoolean(parser.getAttributeValue(i));
49 }
50 }
51 }
52 } catch (IOException | XMLStreamException e) {
53
54
55
56
57
58 }
59 return false;
60 }
61 }