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