View Javadoc

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      {
43          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          Map<String, String> versionsByProjectId = getVersionsByProjectId( projects );
56  
57          for ( MavenProject project : projects )
58          {
59              mediateProject( project, versionsByProjectId );
60          }
61      }
62  
63      // private methods --------------------------------------------------------
64  
65      private static void mediateProject( MavenProject project, Map<String, String> versionsByProjectId )
66      {
67          for ( Dependency dependency : project.getDependencies() )
68          {
69              mediateDependency( dependency, versionsByProjectId );
70          }
71      }
72  
73      private static void mediateDependency( Dependency dependency, Map<String, String> versionsByProjectId )
74      {
75          String projectId = versionlessKey( dependency );
76          String version = versionsByProjectId.get( projectId );
77  
78          if ( version != null )
79          {
80              dependency.setVersion( version );
81          }
82      }
83  
84      private static Map<String, String> getVersionsByProjectId( List<MavenProject> projects )
85      {
86          Map<String, String> versionsByProjectId = new HashMap<String, String>();
87  
88          for ( MavenProject project : projects )
89          {
90              String projectId = versionlessKey( project );
91              String version = project.getVersion();
92  
93              versionsByProjectId.put( projectId, version );
94          }
95  
96          return versionsByProjectId;
97      }
98  
99      private static String versionlessKey( MavenProject project )
100     {
101         return ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
102     }
103 
104     private static String versionlessKey( Dependency dependency )
105     {
106         return ArtifactUtils.versionlessKey( dependency.getGroupId(), dependency.getArtifactId() );
107     }
108 }