Coverage Report - org.apache.maven.plugin.assembly.utils.ProjectUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectUtils
90%
46/51
86%
26/30
5
 
 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.artifact.Artifact;
 23  
 import org.apache.maven.project.MavenProject;
 24  
 import org.codehaus.plexus.logging.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  
  * @version $Id: ProjectUtils.java 1401695 2012-10-24 14:01:38Z dennisl $
 36  
  */
 37  
 public final class ProjectUtils
 38  
 {
 39  
 
 40  
     private ProjectUtils()
 41  0
     {
 42  0
     }
 43  
 
 44  
     public static String getClassifier( Artifact artifact )
 45  
     {
 46  141
         String classifier = artifact.getClassifier();
 47  141
         if ( classifier != null && classifier.length() == 0 )
 48  
         {
 49  3
             classifier = null;
 50  
         }
 51  141
         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  39
         final Set<MavenProject> singleParentSet = Collections.singleton( project );
 60  
 
 61  39
         final Set<MavenProject> moduleCandidates = new LinkedHashSet<MavenProject>( reactorProjects );
 62  
 
 63  39
         final Set<MavenProject> modules = new LinkedHashSet<MavenProject>();
 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  39
         modules.add( project );
 72  
 
 73  39
         int changed = 0;
 74  
 
 75  
         do
 76  
         {
 77  66
             changed = 0;
 78  
 
 79  66
             for ( final Iterator<MavenProject> candidateIterator = moduleCandidates.iterator(); candidateIterator.hasNext(); )
 80  
             {
 81  141
                 final MavenProject moduleCandidate = candidateIterator.next();
 82  
 
 83  141
                 if ( moduleCandidate.getFile() == null )
 84  
                 {
 85  3
                     logger.warn( "Cannot compute whether " + moduleCandidate.getId() + " is a module of: "
 86  
                                     + project.getId()
 87  
                                     + "; it does not have an associated POM file on the local filesystem." );
 88  3
                     continue;
 89  
                 }
 90  
 
 91  
                 Set<MavenProject> currentPotentialParents;
 92  138
                 if ( includeSubModules )
 93  
                 {
 94  93
                     currentPotentialParents = new LinkedHashSet<MavenProject>( modules );
 95  
                 }
 96  
                 else
 97  
                 {
 98  45
                     currentPotentialParents = singleParentSet;
 99  
                 }
 100  
 
 101  138
                 for ( final Iterator<MavenProject> parentIterator = currentPotentialParents.iterator(); parentIterator.hasNext(); )
 102  
                 {
 103  216
                     final MavenProject potentialParent = parentIterator.next();
 104  
 
 105  216
                     if ( potentialParent.getFile() == null )
 106  
                     {
 107  0
                         logger.warn( "Cannot use: " + moduleCandidate.getId()
 108  
                                         + " as a potential module-parent while computing the module set for: "
 109  
                                         + project.getId()
 110  
                                         + "; it does not have an associated POM file on the local filesystem." );
 111  0
                         continue;
 112  
                     }
 113  
 
 114  
                     // if this parent has an entry for the module candidate in
 115  
                     // the path adjustments map, it's a direct
 116  
                     // module of that parent.
 117  216
                     if ( projectContainsModule( potentialParent, moduleCandidate ) )
 118  
                     {
 119  
                         // add the candidate to the list of modules (and
 120  
                         // potential parents)
 121  51
                         modules.add( moduleCandidate );
 122  
 
 123  
                         // remove the candidate from the candidate pool, because
 124  
                         // it's been verified.
 125  51
                         candidateIterator.remove();
 126  
 
 127  
                         // increment the change counter, to show that we
 128  
                         // verified a new module on this pass.
 129  51
                         changed++;
 130  
                     }
 131  216
                 }
 132  138
             }
 133  
         }
 134  66
         while ( changed != 0 );
 135  
 
 136  
         // remove the master project from the modules set, now that we're done
 137  
         // using it as a set of potential module
 138  
         // parents...
 139  39
         modules.remove( project );
 140  
 
 141  39
         return modules;
 142  
     }
 143  
 
 144  
     private static boolean projectContainsModule( final MavenProject mainProject, final MavenProject moduleProject )
 145  
         throws IOException
 146  
     {
 147  
         @SuppressWarnings( "unchecked" )
 148  216
         final List<String> modules = mainProject.getModules();
 149  216
         final File basedir = mainProject.getBasedir();
 150  
 
 151  216
         final File moduleFile = moduleProject.getFile()
 152  
                                              .getCanonicalFile();
 153  
 
 154  216
         File moduleBasedir = moduleProject.getBasedir();
 155  
 
 156  216
         if ( moduleBasedir == null )
 157  
         {
 158  9
             if ( moduleFile != null )
 159  
             {
 160  9
                 moduleBasedir = moduleFile.getParentFile();
 161  
             }
 162  
 
 163  9
             if ( moduleBasedir == null )
 164  
             {
 165  0
                 moduleBasedir = new File( "." );
 166  
             }
 167  
         }
 168  
 
 169  216
         moduleBasedir = moduleBasedir.getCanonicalFile();
 170  
 
 171  216
         for ( final Iterator<String> it = modules.iterator(); it.hasNext(); )
 172  
         {
 173  219
             final String moduleSubpath = it.next();
 174  
 
 175  219
             final File moduleDir = new File( basedir, moduleSubpath ).getCanonicalFile();
 176  
 
 177  219
             if ( moduleDir.equals( moduleFile ) || moduleDir.equals( moduleBasedir ) )
 178  
             {
 179  51
                 return true;
 180  
             }
 181  168
         }
 182  
 
 183  165
         return false;
 184  
     }
 185  
 
 186  
 }