/
DependencyUpdatesXmlRenderer.java
286 lines (246 loc) · 11.6 KB
/
DependencyUpdatesXmlRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package org.codehaus.mojo.versions;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.model.Dependency;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.UpdateScope;
import org.codehaus.mojo.versions.utils.DependencyComparator;
/**
* XML renderer for DependencyUpdatesReport creates an xml file in target directory and writes report about available
* dependency/dependency management updates.
*
* @author Illia Dubinin
* @since 2.4
*/
public class DependencyUpdatesXmlRenderer
{
private static final String GROUP_ID = "groupId";
private static final String ARTIFACT_ID = "artifactId";
private static final String SCOPE = "scope";
private static final String CLASSIFIER = "classifier";
private static final String TYPE = "type";
private static final String CURRENT_VERSION = "currentVersion";
private static final String NEXT_VERSION = "nextVersion";
private static final String STATUS = "status";
private static final String OPEN_TAG = "<";
private static final String CLOSE_TAG = ">";
private static final String OPEN_CLOSING_TAG = "</";
private static final String NL = "\n";
private static final String TAB = "\t";
private Map<Dependency, ArtifactVersions> dependencyUpdates;
private Map<Dependency, ArtifactVersions> dependencyManagementUpdates;
private String outputFileName;
public DependencyUpdatesXmlRenderer( Map<Dependency, ArtifactVersions> dependencyUpdates,
Map<Dependency, ArtifactVersions> dependencyManagementUpdates,
String outputFileName )
{
this.dependencyUpdates = dependencyUpdates;
this.dependencyManagementUpdates = dependencyManagementUpdates;
this.outputFileName = outputFileName;
}
/**
* Makes report file with given name in target directory.
*
* @throws MavenReportException if something went wrong
*/
public void render()
throws MavenReportException
{
StringBuilder sb = new StringBuilder();
sb.append( "<DependencyUpdatesReport>" ).append( NL );
Map<Dependency, ArtifactVersions> allUpdates = new TreeMap<>( new DependencyComparator() );
allUpdates.putAll( dependencyManagementUpdates );
allUpdates.putAll( dependencyUpdates );
sb.append( getSummaryBlock( allUpdates.values() ) );
sb.append( getDependencyInfoBlock( dependencyManagementUpdates, "dependencyManagements",
"dependencyManagement" ) );
sb.append( getDependencyInfoBlock( dependencyUpdates, "dependencies", "dependency" ) );
sb.append( "</DependencyUpdatesReport>" ).append( NL );
PrintWriter pw;
try
{
pw = new PrintWriter( outputFileName, "UTF8" );
pw.print( sb );
pw.close();
}
catch ( IOException e )
{
throw new MavenReportException( "Cannot create xml report.", e );
}
}
/**
* Method wraps value in xml tag. In ex: to wrap foo in tag bar you have to pass foo as value and bar as tag. As a
* result you will get: <bar>foo</bar>
*
* @param value - string to wrap
* @param tag - name of tag
* @return value wrapped in xml tag
*/
public static String wrapElement( String value, String tag )
{
return OPEN_TAG + tag + CLOSE_TAG + value + OPEN_CLOSING_TAG + tag + CLOSE_TAG;
}
/**
* Returns summary of dependency analysis result in xml format: current version, next available, next incremental,
* next minor and next major versions.
*
* @param allUpdates all dependencies versions
* @return summary in xml format
*/
public static String getSummaryBlock( Collection<ArtifactVersions> allUpdates )
{
int numInc = 0;
int numMin = 0;
int numMaj = 0;
int numAny = 0;
int numCur = 0;
for ( ArtifactVersions details : allUpdates )
{
if ( details.getOldestUpdate( UpdateScope.SUBINCREMENTAL ) != null )
{
numAny++;
}
else if ( details.getOldestUpdate( UpdateScope.INCREMENTAL ) != null )
{
numInc++;
}
else if ( details.getOldestUpdate( UpdateScope.MINOR ) != null )
{
numMin++;
}
else if ( details.getOldestUpdate( UpdateScope.MAJOR ) != null )
{
numMaj++;
}
else
{
numCur++;
}
}
String result = "\t<summary>%n" + "\t\t<usingLastVersion>%d</usingLastVersion>%n"
+ "\t\t<nextVersionAvailable>%d</nextVersionAvailable>%n"
+ "\t\t<nextIncremetalAvailable>%d</nextIncremetalAvailable>%n"
+ "\t\t<nextMinorAvailable>%d</nextMinorAvailable>%n" + "\t\t<nextMajorAvailable>%d</nextMajorAvailable>%n"
+ "\t</summary>%n";
return String.format( result, numCur, numAny, numInc, numMin, numMaj );
}
/**
* Returns xml report for current dependency state with following info: current version, next available version,
* next incremental/minor/major if available and status ('incremental available', 'minor available', 'major
* available' or 'no new available')
*
* @param versions version info for dependency
* @return xml reports about current possible updates.
*/
public static String getVersionsBlocks( ArtifactVersions versions )
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( versions.isCurrentVersionDefined()
? versions.getCurrentVersion().toString() : versions.getArtifact().getVersionRange().toString(),
CURRENT_VERSION ) ).append( NL );
ArtifactVersion nextVersion = versions.getOldestUpdate( UpdateScope.ANY );
if ( nextVersion != null )
{
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( nextVersion.toString(),
NEXT_VERSION ) ).append( NL );
String incrementalsBlock = getVersionsInScopeBlock( versions, UpdateScope.INCREMENTAL );
sBuilder.append( incrementalsBlock );
String minorsBlock = getVersionsInScopeBlock( versions, UpdateScope.MINOR );
sBuilder.append( minorsBlock );
String majorsBlock = getVersionsInScopeBlock( versions, UpdateScope.MAJOR );
sBuilder.append( majorsBlock );
String status = null;
if ( incrementalsBlock.length() > 0 )
{
status = "incremental available";
}
else if ( minorsBlock.length() > 0 )
{
status = "minor available";
}
else if ( majorsBlock.length() > 0 )
{
status = "major available";
}
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( status, STATUS ) ).append( NL );
}
else
{
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( "no new available",
STATUS ) ).append( NL );
}
return sBuilder.toString();
}
private static String getDependencyInfoBlock( Map<Dependency, ArtifactVersions> dependencyUpdates, String blockName,
String subblockName )
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append( TAB ).append( OPEN_TAG ).append( blockName ).append( CLOSE_TAG ).append( NL );
for ( Entry<Dependency, ArtifactVersions> entry : dependencyUpdates.entrySet() )
{
sBuilder.append( TAB ).append( TAB ).append( OPEN_TAG ).append( subblockName ).append( CLOSE_TAG )
.append( NL );
Dependency dep = entry.getKey();
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getGroupId(),
GROUP_ID ) ).append( NL );
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getArtifactId(),
ARTIFACT_ID ) ).append( NL );
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getScope(),
SCOPE ) ).append( NL );
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getClassifier(),
CLASSIFIER ) ).append( NL );
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getType(),
TYPE ) ).append( NL );
sBuilder.append( getVersionsBlocks( entry.getValue() ) );
sBuilder.append( TAB ).append( TAB ).append( OPEN_CLOSING_TAG ).append( subblockName ).append( CLOSE_TAG )
.append( NL );
}
sBuilder.append( TAB ).append( OPEN_CLOSING_TAG ).append( blockName ).append( CLOSE_TAG ).append( NL );
return sBuilder.toString();
}
private static String getVersionsInScopeBlock( ArtifactVersions av, UpdateScope scope )
{
String versionsTag = scope.toString().toLowerCase() + "s";
StringBuilder sBuilder = new StringBuilder();
ArtifactVersion nextVersion = av.getOldestUpdate( scope );
if ( nextVersion != null )
{
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( OPEN_TAG ).append( versionsTag )
.append( CLOSE_TAG ).append( NL );
ArtifactVersion[] versions = av.getAllUpdates( scope );
for ( ArtifactVersion version : versions )
{
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( TAB )
.append( wrapElement( version.toString(),
scope.toString().toLowerCase() ) ).append( NL );
}
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( OPEN_CLOSING_TAG ).append( versionsTag )
.append( CLOSE_TAG ).append( NL );
}
return sBuilder.toString();
}
}