forked from mojohaus/versions
/
PluginUpdatesXmlRenderer.java
138 lines (115 loc) · 5.41 KB
/
PluginUpdatesXmlRenderer.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
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 static org.codehaus.mojo.versions.DependencyUpdatesXmlRenderer.getVersionsBlocks;
import static org.codehaus.mojo.versions.DependencyUpdatesXmlRenderer.wrapElement;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.apache.maven.model.Plugin;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.utils.PluginComparator;
/**
* XML renderer for PluginUpdatesReport creates an xml file in target directory and writes report about available
* plugin/plugin management updates.
*
* @author Illia Dubinin
* @since 2.4
*/
public class PluginUpdatesXmlRenderer
{
private static final String GROUP_ID = "groupId";
private static final String ARTIFACT_ID = "artifactId";
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<Plugin, PluginUpdatesDetails> pluginUpdates;
private Map<Plugin, PluginUpdatesDetails> pluginManagementUpdates;
private String outputFileName;
public PluginUpdatesXmlRenderer( Map<Plugin, PluginUpdatesDetails> pluginUpdates,
Map<Plugin, PluginUpdatesDetails> pluginManagementUpdates, String outputFileName )
{
this.pluginUpdates = pluginUpdates;
this.pluginManagementUpdates = pluginManagementUpdates;
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( "<PluginUpdatesReport>" ).append( NL );
Map<Plugin, PluginUpdatesDetails> allUpdates = new TreeMap<>( new PluginComparator() );
allUpdates.putAll( pluginManagementUpdates );
allUpdates.putAll( pluginUpdates );
sb.append( getSummaryBlock( allUpdates ) );
sb.append( getPluginsInfoBlock( pluginManagementUpdates, "pluginManagements", "pluginManagement" ) );
sb.append( getPluginsInfoBlock( pluginUpdates, "plugins", "plugin" ) );
sb.append( "</PluginUpdatesReport>" ).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 );
}
}
private static String getSummaryBlock( Map<Plugin, PluginUpdatesDetails> allUpdates )
{
Collection<ArtifactVersions> allVersions = allUpdates.values().stream()
.map( PluginUpdatesDetails::getArtifactVersions )
.collect( Collectors.toList() );
return DependencyUpdatesXmlRenderer.getSummaryBlock( allVersions );
}
private static String getPluginsInfoBlock( Map<Plugin, PluginUpdatesDetails> pluginUpdates, String blockName,
String subblockName )
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append( TAB ).append( OPEN_TAG ).append( blockName ).append( CLOSE_TAG ).append( NL );
for ( Entry<Plugin, PluginUpdatesDetails> entry : pluginUpdates.entrySet() )
{
sBuilder.append( TAB ).append( TAB ).append( OPEN_TAG ).append( subblockName ).append( CLOSE_TAG ).append( NL );
Plugin plugin = entry.getKey();
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( plugin.getGroupId(),
GROUP_ID ) ).append( NL );
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( plugin.getArtifactId(),
ARTIFACT_ID ) ).append( NL );
sBuilder.append( getVersionsBlocks( entry.getValue().getArtifactVersions() ) );
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();
}
}