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.codehaus.plexus.logging.Logger;
25  
26  import javax.annotation.Nonnull;
27  import javax.annotation.Nullable;
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.Collections;
31  import java.util.Iterator;
32  import java.util.LinkedHashSet;
33  import java.util.List;
34  import java.util.Set;
35  
36  /**
37   * @version $Id: ProjectUtils.html 1016737 2017-08-13 12:01:54Z khmarbaise $
38   */
39  public final class ProjectUtils
40  {
41  
42      private ProjectUtils()
43      {
44      }
45  
46      @Nullable
47      public static String getClassifier( @Nonnull Artifact artifact )
48      {
49          String classifier = artifact.getClassifier();
50          if ( classifier != null && classifier.length() == 0 )
51          {
52              classifier = null;
53          }
54          return classifier;
55      }
56  
57      @Nonnull
58      public static Set<MavenProject> getProjectModules( @Nonnull final MavenProject project,
59                                                         @Nonnull final List<MavenProject> reactorProjects,
60                                                         final boolean includeSubModules, @Nonnull final Logger logger )
61          throws IOException
62      {
63          final Set<MavenProject> singleParentSet = Collections.singleton( project );
64  
65          final Set<MavenProject> moduleCandidates = new LinkedHashSet<MavenProject>( reactorProjects );
66  
67          final Set<MavenProject> modules = new LinkedHashSet<MavenProject>();
68  
69          // we temporarily add the master project to the modules set, since this
70          // set is pulling double duty as a set of
71          // potential module parents in the tree rooted at the master
72          // project...this allows us to use the same looping
73          // algorithm below to discover both direct modules of the master project
74          // AND modules of those direct modules.
75          modules.add( project );
76  
77          int changed;
78  
79          do
80          {
81              changed = 0;
82  
83              for ( final Iterator<MavenProject> candidateIterator = moduleCandidates.iterator();
84                    candidateIterator.hasNext(); )
85              {
86                  final MavenProject moduleCandidate = candidateIterator.next();
87  
88                  if ( moduleCandidate.getFile() == null )
89                  {
90                      logger.warn(
91                          "Cannot compute whether " + moduleCandidate.getId() + " is a module of: " + project.getId()
92                              + "; it does not have an associated POM file on the local filesystem." );
93                      continue;
94                  }
95  
96                  Set<MavenProject> currentPotentialParents;
97                  if ( includeSubModules )
98                  {
99                      currentPotentialParents = new LinkedHashSet<MavenProject>( modules );
100                 }
101                 else
102                 {
103                     currentPotentialParents = singleParentSet;
104                 }
105 
106                 for ( final MavenProject potentialParent : currentPotentialParents )
107                 {
108                     if ( potentialParent.getFile() == null )
109                     {
110                         logger.warn( "Cannot use: " + moduleCandidate.getId()
111                                          + " as a potential module-parent while computing the module set for: "
112                                          + project.getId()
113                                          + "; it does not have an associated POM file on the local filesystem." );
114                         continue;
115                     }
116 
117                     // if this parent has an entry for the module candidate in
118                     // the path adjustments map, it's a direct
119                     // module of that parent.
120                     if ( projectContainsModule( potentialParent, moduleCandidate ) )
121                     {
122                         // add the candidate to the list of modules (and
123                         // potential parents)
124                         modules.add( moduleCandidate );
125 
126                         // remove the candidate from the candidate pool, because
127                         // it's been verified.
128                         candidateIterator.remove();
129 
130                         // increment the change counter, to show that we
131                         // verified a new module on this pass.
132                         changed++;
133                         
134                         // We need to move on to the next candidate since this one was just verified
135                         break;
136                     }
137                 }
138             }
139         }
140         while ( changed != 0 );
141 
142         // remove the master project from the modules set, now that we're done
143         // using it as a set of potential module
144         // parents...
145         modules.remove( project );
146 
147         return modules;
148     }
149 
150     private static boolean projectContainsModule( @Nonnull final MavenProject mainProject,
151                                                   @Nonnull final MavenProject moduleProject )
152         throws IOException
153     {
154         final List<String> modules = mainProject.getModules();
155         final File basedir = mainProject.getBasedir();
156 
157         final File moduleFile = moduleProject.getFile().getCanonicalFile();
158 
159         File moduleBasedir = moduleProject.getBasedir();
160 
161         if ( moduleBasedir == null )
162         {
163             moduleBasedir = moduleFile.getParentFile();
164 
165             if ( moduleBasedir == null )
166             {
167                 moduleBasedir = new File( "." );
168             }
169         }
170 
171         moduleBasedir = moduleBasedir.getCanonicalFile();
172 
173         for ( final String moduleSubpath : modules )
174         {
175             final File moduleDir = new File( basedir, moduleSubpath ).getCanonicalFile();
176 
177             if ( moduleDir.equals( moduleFile ) || moduleDir.equals( moduleBasedir ) )
178             {
179                 return true;
180             }
181         }
182 
183         return false;
184     }
185 
186 }