1 | |
package org.apache.maven.archiva.dependency.graph.tasks; |
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.Map; |
24 | |
|
25 | |
import org.apache.commons.lang.StringUtils; |
26 | |
import org.apache.maven.archiva.dependency.graph.DependencyGraph; |
27 | |
import org.apache.maven.archiva.dependency.graph.DependencyGraphBuilder; |
28 | |
import org.apache.maven.archiva.dependency.graph.DependencyGraphEdge; |
29 | |
import org.apache.maven.archiva.dependency.graph.DependencyGraphNode; |
30 | |
import org.apache.maven.archiva.dependency.graph.DependencyGraphUtils; |
31 | |
import org.apache.maven.archiva.dependency.graph.tasks.DependencyManagementStack.Rules; |
32 | |
import org.apache.maven.archiva.dependency.graph.walk.BaseVisitor; |
33 | |
import org.apache.maven.archiva.dependency.graph.walk.DependencyGraphVisitor; |
34 | |
import org.apache.maven.archiva.model.ArtifactReference; |
35 | |
import org.apache.maven.archiva.model.VersionedReference; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class DependencyManagementApplier |
44 | |
extends BaseVisitor |
45 | |
implements DependencyGraphVisitor |
46 | |
{ |
47 | 0 | private DependencyManagementStack depStack = new DependencyManagementStack(); |
48 | |
|
49 | |
private DependencyGraphBuilder builder; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | private Map<ArtifactReference, String> nodeVersionChanges = new HashMap<ArtifactReference, String>(); |
60 | |
|
61 | 0 | private int nodesAdded = 0; |
62 | |
|
63 | |
public void discoverGraph( DependencyGraph graph ) |
64 | |
{ |
65 | 0 | super.discoverGraph( graph ); |
66 | 0 | nodeVersionChanges.clear(); |
67 | 0 | depStack.reset(); |
68 | 0 | nodesAdded = 0; |
69 | 0 | } |
70 | |
|
71 | |
public void discoverNode( DependencyGraphNode node ) |
72 | |
{ |
73 | 0 | super.discoverNode( node ); |
74 | |
|
75 | 0 | depStack.push( node ); |
76 | |
|
77 | 0 | for ( DependencyGraphEdge edge : graph.getEdgesFrom( node ) ) |
78 | |
{ |
79 | 0 | Rules rules = depStack.getRules( edge ); |
80 | |
|
81 | 0 | if ( rules == null ) |
82 | |
{ |
83 | |
|
84 | 0 | continue; |
85 | |
} |
86 | |
|
87 | 0 | DependencyGraphNode subnode = graph.getNode( edge.getNodeTo() ); |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 0 | node.getExcludes().addAll( rules.exclusions ); |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 0 | String specifiedVersion = rules.artifact.getVersion(); |
98 | |
|
99 | |
|
100 | 0 | String trackedVersion = (String) nodeVersionChanges.get( edge.getNodeTo() ); |
101 | |
|
102 | |
|
103 | 0 | String nodeVersion = subnode.getArtifact().getVersion(); |
104 | |
|
105 | |
|
106 | 0 | String actualVersion = StringUtils.defaultString( trackedVersion, nodeVersion ); |
107 | |
|
108 | |
|
109 | 0 | if ( !StringUtils.equals( specifiedVersion, actualVersion ) ) |
110 | |
{ |
111 | |
|
112 | 0 | nodeVersionChanges.put( edge.getNodeTo(), specifiedVersion ); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | 0 | if ( StringUtils.isNotBlank( rules.scope ) ) |
118 | |
{ |
119 | 0 | edge.setScope( rules.scope ); |
120 | |
} |
121 | 0 | } |
122 | 0 | } |
123 | |
|
124 | |
public void finishNode( DependencyGraphNode node ) |
125 | |
{ |
126 | 0 | super.finishNode( node ); |
127 | |
|
128 | 0 | depStack.pop(); |
129 | 0 | } |
130 | |
|
131 | |
public void finishGraph( DependencyGraph graph ) |
132 | |
{ |
133 | 0 | super.finishGraph( graph ); |
134 | |
|
135 | 0 | for ( ArtifactReference ref : this.nodeVersionChanges.keySet() ) |
136 | |
{ |
137 | 0 | String toVersion = this.nodeVersionChanges.get( ref ); |
138 | |
|
139 | 0 | collapseVersions( graph, ref, ref.getVersion(), toVersion ); |
140 | 0 | } |
141 | 0 | } |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private void collapseVersions( DependencyGraph graph, ArtifactReference fromRef, String fromVersion, |
158 | |
String toVersion ) |
159 | |
{ |
160 | 0 | if ( StringUtils.equals( fromVersion, toVersion ) ) |
161 | |
{ |
162 | |
|
163 | 0 | return; |
164 | |
} |
165 | |
|
166 | 0 | ArtifactReference toRef = new ArtifactReference(); |
167 | 0 | toRef.setGroupId( fromRef.getGroupId() ); |
168 | 0 | toRef.setArtifactId( fromRef.getArtifactId() ); |
169 | 0 | toRef.setVersion( toVersion ); |
170 | 0 | toRef.setClassifier( fromRef.getClassifier() ); |
171 | 0 | toRef.setType( fromRef.getType() ); |
172 | |
|
173 | 0 | DependencyGraphNode nodeFROM = graph.getNode( fromRef ); |
174 | 0 | DependencyGraphNode nodeTO = graph.getNode( toRef ); |
175 | |
|
176 | 0 | if ( nodeTO == null ) |
177 | |
{ |
178 | |
|
179 | 0 | nodeTO = new DependencyGraphNode( toRef ); |
180 | 0 | nodeTO.setResolved( false ); |
181 | |
|
182 | 0 | graph.addNode( nodeTO ); |
183 | |
|
184 | 0 | VersionedReference projectRef = new VersionedReference(); |
185 | 0 | projectRef.setGroupId( toRef.getGroupId() ); |
186 | 0 | projectRef.setArtifactId( toRef.getArtifactId() ); |
187 | 0 | projectRef.setVersion( toRef.getVersion() ); |
188 | |
|
189 | 0 | builder.resolveNode( graph, nodeTO, projectRef ); |
190 | 0 | nodesAdded++; |
191 | |
} |
192 | |
|
193 | 0 | DependencyGraphUtils.collapseNodes( graph, nodeFROM, nodeTO ); |
194 | 0 | } |
195 | |
|
196 | |
public DependencyGraphBuilder getBuilder() |
197 | |
{ |
198 | 0 | return builder; |
199 | |
} |
200 | |
|
201 | |
public void setBuilder( DependencyGraphBuilder builder ) |
202 | |
{ |
203 | 0 | this.builder = builder; |
204 | 0 | } |
205 | |
|
206 | |
public boolean hasCreatedNodes() |
207 | |
{ |
208 | 0 | return ( nodesAdded > 0 ); |
209 | |
} |
210 | |
} |