View Javadoc
1   package org.apache.maven.plugins.assembly.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.project.MavenProject;
24  import org.slf4j.Logger;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.LinkedHashSet;
31  import java.util.List;
32  import java.util.Set;
33  
34  /**
35   *
36   */
37  public final class ProjectUtils
38  {
39  
40      private ProjectUtils()
41      {
42      }
43  
44      public static String getClassifier( Artifact artifact )
45      {
46          String classifier = artifact.getClassifier();
47          if ( classifier != null && classifier.length() == 0 )
48          {
49              classifier = null;
50          }
51          return classifier;
52      }
53  
54      public static Set<MavenProject> getProjectModules( final MavenProject project,
55                                                         final List<MavenProject> reactorProjects,
56                                                         final boolean includeSubModules, final Logger logger )
57          throws IOException
58      {
59          final Set<MavenProject> singleParentSet = Collections.singleton( project );
60  
61          final Set<MavenProject> moduleCandidates = new LinkedHashSet<>( reactorProjects );
62  
63          final Set<MavenProject> modules = new LinkedHashSet<>();
64  
65          // we temporarily add the master project to the modules set, since this
66          // set is pulling double duty as a set of
67          // potential module parents in the tree rooted at the master
68          // project...this allows us to use the same looping
69          // algorithm below to discover both direct modules of the master project
70          // AND modules of those direct modules.
71          modules.add( project );
72  
73          int changed;
74  
75          do
76          {
77              changed = 0;
78  
79              for ( final Iterator<MavenProject> candidateIterator = moduleCandidates.iterator();
80                    candidateIterator.hasNext(); )
81              {
82                  final MavenProject moduleCandidate = candidateIterator.next();
83  
84                  if ( moduleCandidate.getFile() == null )
85                  {
86                      logger.warn(
87                          "Cannot compute whether " + moduleCandidate.getId() + " is a module of: " + project.getId()
88                              + "; it does not have an associated POM file on the local filesystem." );
89                      continue;
90                  }
91  
92                  Set<MavenProject> currentPotentialParents;
93                  if ( includeSubModules )
94                  {
95                      currentPotentialParents = new LinkedHashSet<>( modules );
96                  }
97                  else
98                  {
99                      currentPotentialParents = singleParentSet;
100                 }
101 
102                 for ( final MavenProject potentialParent : currentPotentialParents )
103                 {
104                     if ( potentialParent.getFile() == null )
105                     {
106                         logger.warn( "Cannot use: " + moduleCandidate.getId()
107                                          + " as a potential module-parent while computing the module set for: "
108                                          + project.getId()
109                                          + "; it does not have an associated POM file on the local filesystem." );
110                         continue;
111                     }
112 
113                     // if this parent has an entry for the module candidate in
114                     // the path adjustments map, it's a direct
115                     // module of that parent.
116                     if ( projectContainsModule( potentialParent, moduleCandidate ) )
117                     {
118                         // add the candidate to the list of modules (and
119                         // potential parents)
120                         modules.add( moduleCandidate );
121 
122                         // remove the candidate from the candidate pool, because
123                         // it's been verified.
124                         candidateIterator.remove();
125 
126                         // increment the change counter, to show that we
127                         // verified a new module on this pass.
128                         changed++;
129                         
130                         // We need to move on to the next candidate since this one was just verified
131                         break;
132                     }
133                 }
134             }
135         }
136         while ( changed != 0 );
137 
138         // remove the master project from the modules set, now that we're done
139         // using it as a set of potential module
140         // parents...
141         modules.remove( project );
142 
143         return modules;
144     }
145 
146     private static boolean projectContainsModule( final MavenProject mainProject,
147                                                   final MavenProject moduleProject )
148         throws IOException
149     {
150         final List<String> modules = mainProject.getModules();
151         final File basedir = mainProject.getBasedir();
152 
153         final File moduleFile = moduleProject.getFile().getCanonicalFile();
154 
155         File moduleBasedir = moduleProject.getBasedir();
156 
157         if ( moduleBasedir == null )
158         {
159             moduleBasedir = moduleFile.getParentFile();
160 
161             if ( moduleBasedir == null )
162             {
163                 moduleBasedir = new File( "." );
164             }
165         }
166 
167         moduleBasedir = moduleBasedir.getCanonicalFile();
168 
169         for ( final String moduleSubpath : modules )
170         {
171             final File moduleDir = new File( basedir, moduleSubpath ).getCanonicalFile();
172 
173             if ( moduleDir.equals( moduleFile ) || moduleDir.equals( moduleBasedir ) )
174             {
175                 return true;
176             }
177         }
178 
179         return false;
180     }
181 
182 }