1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
31  
32  @Mojo(name = "directory-of", defaultPhase = LifecyclePhase.INITIALIZE, requiresProject = true, threadSafe = true)
33  public class DirectoryOfGoal extends AbstractDirectoryGoal {
34  
35      
36      protected static final String DIR_OF_CONTEXT_KEY = "directories.directoryOf-";
37  
38      
39      @Parameter
40      private ProjectRef project;
41  
42      
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  }