View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.root;
20  
21  import javax.inject.Named;
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.xml.XmlService;
31  
32  /**
33   * @deprecated use {@code org.apache.maven.api.services.model.RootLocator} instead
34   */
35  @Named
36  @Deprecated(since = "4.0.0")
37  public class DefaultRootLocator implements RootLocator {
38  
39      @Override
40      public boolean isRootDirectory(Path dir) {
41          if (Files.isDirectory(dir.resolve(".mvn"))) {
42              return true;
43          }
44          // we're too early to use the modelProcessor ...
45          Path pom = dir.resolve("pom.xml");
46          try (InputStream is = Files.newInputStream(pom)) {
47              XMLStreamReader parser = XmlService.newXMLInputFactory().createXMLStreamReader(is);
48              if (parser.nextTag() == XMLStreamReader.START_ELEMENT
49                      && parser.getLocalName().equals("project")) {
50                  for (int i = 0; i < parser.getAttributeCount(); i++) {
51                      if ("root".equals(parser.getAttributeLocalName(i))) {
52                          return Boolean.parseBoolean(parser.getAttributeValue(i));
53                      }
54                  }
55              }
56          } catch (IOException | XMLStreamException e) {
57              // The root locator can be used very early during the setup of Maven,
58              // even before the arguments from the command line are parsed.  Any exception
59              // that would happen here should cause the build to fail at a later stage
60              // (when actually parsing the POM) and will lead to a better exception being
61              // displayed to the user, so just bail out and return false.
62          }
63          return false;
64      }
65  }