1 | |
package org.apache.maven.shared.runtime; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
final class MavenProjectUtils |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
private MavenProjectUtils() |
42 | 0 | { |
43 | 0 | throw new AssertionError(); |
44 | |
} |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static void mediateDependencyVersions( List<MavenProject> projects ) |
54 | |
{ |
55 | 24 | Map<String, String> versionsByProjectId = getVersionsByProjectId( projects ); |
56 | |
|
57 | 24 | for ( MavenProject project : projects ) |
58 | |
{ |
59 | 42 | mediateProject( project, versionsByProjectId ); |
60 | |
} |
61 | 24 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
private static void mediateProject( MavenProject project, Map<String, String> versionsByProjectId ) |
66 | |
{ |
67 | 42 | for ( Dependency dependency : project.getDependencies() ) |
68 | |
{ |
69 | 12 | mediateDependency( dependency, versionsByProjectId ); |
70 | |
} |
71 | 42 | } |
72 | |
|
73 | |
private static void mediateDependency( Dependency dependency, Map<String, String> versionsByProjectId ) |
74 | |
{ |
75 | 12 | String projectId = versionlessKey( dependency ); |
76 | 12 | String version = versionsByProjectId.get( projectId ); |
77 | |
|
78 | 12 | if ( version != null ) |
79 | |
{ |
80 | 12 | dependency.setVersion( version ); |
81 | |
} |
82 | 12 | } |
83 | |
|
84 | |
private static Map<String, String> getVersionsByProjectId( List<MavenProject> projects ) |
85 | |
{ |
86 | 24 | Map<String, String> versionsByProjectId = new HashMap<String, String>(); |
87 | |
|
88 | 24 | for ( MavenProject project : projects ) |
89 | |
{ |
90 | 42 | String projectId = versionlessKey( project ); |
91 | 42 | String version = project.getVersion(); |
92 | |
|
93 | 42 | versionsByProjectId.put( projectId, version ); |
94 | 42 | } |
95 | |
|
96 | 24 | return versionsByProjectId; |
97 | |
} |
98 | |
|
99 | |
private static String versionlessKey( MavenProject project ) |
100 | |
{ |
101 | 42 | return ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() ); |
102 | |
} |
103 | |
|
104 | |
private static String versionlessKey( Dependency dependency ) |
105 | |
{ |
106 | 12 | return ArtifactUtils.versionlessKey( dependency.getGroupId(), dependency.getArtifactId() ); |
107 | |
} |
108 | |
} |