Coverage Report - org.apache.maven.shared.runtime.MavenProjectUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MavenProjectUtils
92%
23/25
87%
7/8
1,714
 
 1  
 package org.apache.maven.shared.runtime;
 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 java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.maven.artifact.ArtifactUtils;
 27  
 import org.apache.maven.model.Dependency;
 28  
 import org.apache.maven.project.MavenProject;
 29  
 
 30  
 /**
 31  
  * Utility methods for working with Maven projects.
 32  
  * 
 33  
  * @author <a href="mailto:markh@apache.org">Mark Hobson</a>
 34  
  * @version $Id: MavenProjectUtils.java 1341416 2012-05-22 11:38:54Z markh $
 35  
  * @see MavenProject
 36  
  */
 37  
 final class MavenProjectUtils
 38  
 {
 39  
     // constructors -----------------------------------------------------------
 40  
 
 41  
     private MavenProjectUtils()
 42  0
     {
 43  0
         throw new AssertionError();
 44  
     }
 45  
 
 46  
     // public methods ---------------------------------------------------------
 47  
 
 48  
     /**
 49  
      * Aligns dependency versions to their corresponding project version for the specified projects.
 50  
      * 
 51  
      * @param projects the projects whose dependency versions to align
 52  
      */
 53  
     public static void mediateDependencyVersions( List<MavenProject> projects )
 54  
     {
 55  32
         Map<String, String> versionsByProjectId = getVersionsByProjectId( projects );
 56  
 
 57  32
         for ( MavenProject project : projects )
 58  
         {
 59  56
             mediateProject( project, versionsByProjectId );
 60  56
         }
 61  32
     }
 62  
 
 63  
     // private methods --------------------------------------------------------
 64  
 
 65  
     private static void mediateProject( MavenProject project, Map<String, String> versionsByProjectId )
 66  
     {
 67  56
         for ( Dependency dependency : project.getDependencies() )
 68  
         {
 69  16
             mediateDependency( dependency, versionsByProjectId );
 70  16
         }
 71  56
     }
 72  
 
 73  
     private static void mediateDependency( Dependency dependency, Map<String, String> versionsByProjectId )
 74  
     {
 75  16
         String projectId = versionlessKey( dependency );
 76  16
         String version = versionsByProjectId.get( projectId );
 77  
 
 78  16
         if ( version != null )
 79  
         {
 80  16
             dependency.setVersion( version );
 81  
         }
 82  16
     }
 83  
 
 84  
     private static Map<String, String> getVersionsByProjectId( List<MavenProject> projects )
 85  
     {
 86  32
         Map<String, String> versionsByProjectId = new HashMap<String, String>();
 87  
 
 88  32
         for ( MavenProject project : projects )
 89  
         {
 90  56
             String projectId = versionlessKey( project );
 91  56
             String version = project.getVersion();
 92  
 
 93  56
             versionsByProjectId.put( projectId, version );
 94  56
         }
 95  
 
 96  32
         return versionsByProjectId;
 97  
     }
 98  
 
 99  
     private static String versionlessKey( MavenProject project )
 100  
     {
 101  56
         return ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
 102  
     }
 103  
 
 104  
     private static String versionlessKey( Dependency dependency )
 105  
     {
 106  16
         return ArtifactUtils.versionlessKey( dependency.getGroupId(), dependency.getArtifactId() );
 107  
     }
 108  
 }