View Javadoc
1   /*
2    *    Copyright 2011-2024 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.commonjava.maven.plugins.execroot;
17  
18  import java.io.File;
19  import java.util.ArrayDeque;
20  import java.util.Deque;
21  import java.util.List;
22  
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.project.MavenProject;
28  
29  /**
30   * The Class DirectoryOfGoal.
31   */
32  @Mojo(name = "directory-of", defaultPhase = LifecyclePhase.INITIALIZE, requiresProject = true, threadSafe = true)
33  public class DirectoryOfGoal extends AbstractDirectoryGoal {
34  
35      /** The Constant DIR_OF_CONTEXT_KEY. */
36      protected static final String DIR_OF_CONTEXT_KEY = "directories.directoryOf-";
37  
38      /** The project. */
39      @Parameter
40      private ProjectRef project;
41  
42      /** The projects. */
43      @Parameter(defaultValue = "${reactorProjects}", readonly = true)
44      protected List<MavenProject> projects;
45  
46      @Override
47      protected File findDirectory() throws MojoExecutionException {
48          File dir = null;
49  
50          final Deque<MavenProject> toCheck = new ArrayDeque<>(projects);
51          while (!toCheck.isEmpty()) {
52              final MavenProject mavenProject = toCheck.pop();
53              if (project.matches(mavenProject)) {
54                  dir = mavenProject.getBasedir();
55                  break;
56              }
57  
58              if (mavenProject.getParent() != null) {
59                  toCheck.add(mavenProject.getParent());
60              }
61          }
62  
63          if (dir == null) {
64              throw new MojoExecutionException("Cannot find directory for project: " + project);
65          }
66  
67          return dir;
68      }
69  
70      @Override
71      protected String getContextKey() {
72          return DIR_OF_CONTEXT_KEY + project;
73      }
74  
75      @Override
76      protected String getLogLabel() {
77          return "Directory of " + project;
78      }
79  
80  }