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.locator;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.File;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.nio.file.Paths;
28  
29  /**
30   * Locates a POM file within a project base directory.
31   *
32   */
33  @Named
34  @Singleton
35  public class DefaultModelLocator implements ModelLocator {
36  
37      @Deprecated
38      @Override
39      public File locatePom(File projectDirectory) {
40          Path path = locatePom(projectDirectory != null ? projectDirectory.toPath() : null);
41          return path != null ? path.toFile() : null;
42      }
43  
44      @Override
45      public Path locatePom(Path projectDirectory) {
46          return projectDirectory != null ? projectDirectory : Paths.get(System.getProperty("user.dir"));
47      }
48  
49      @Deprecated
50      @Override
51      public File locateExistingPom(File project) {
52          Path path = locateExistingPom(project != null ? project.toPath() : null);
53          return path != null ? path.toFile() : null;
54      }
55  
56      @Override
57      public Path locateExistingPom(Path project) {
58          if (project == null || Files.isDirectory(project)) {
59              project = locatePom(project);
60          }
61          if (Files.isDirectory(project)) {
62              Path pom = project.resolve("pom.xml");
63              return Files.isRegularFile(pom) ? pom : null;
64          } else if (Files.isRegularFile(project)) {
65              return project;
66          } else {
67              return null;
68          }
69      }
70  }