1 | |
package org.apache.maven.report.projectinfo; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
24 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
25 | |
import org.apache.maven.artifact.versioning.VersionRange; |
26 | |
import org.apache.maven.doxia.sink.Sink; |
27 | |
import org.apache.maven.model.Plugin; |
28 | |
import org.apache.maven.plugin.logging.Log; |
29 | |
import org.apache.maven.plugins.annotations.Component; |
30 | |
import org.apache.maven.plugins.annotations.Mojo; |
31 | |
import org.apache.maven.plugins.annotations.ResolutionScope; |
32 | |
import org.apache.maven.project.MavenProject; |
33 | |
import org.apache.maven.project.MavenProjectBuilder; |
34 | |
import org.apache.maven.project.ProjectBuildingException; |
35 | |
import org.codehaus.plexus.i18n.I18N; |
36 | |
import org.codehaus.plexus.util.StringUtils; |
37 | |
|
38 | |
import java.util.ArrayList; |
39 | |
import java.util.Collections; |
40 | |
import java.util.Comparator; |
41 | |
import java.util.List; |
42 | |
import java.util.Locale; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
@Mojo( name = "plugin-management", requiresDependencyResolution = ResolutionScope.TEST ) |
52 | 1 | public class PluginManagementReport |
53 | |
extends AbstractProjectInfoReport |
54 | |
{ |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
@Component |
63 | |
private MavenProjectBuilder mavenProjectBuilder; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@Component |
69 | |
private ArtifactFactory artifactFactory; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
@Override |
76 | |
public void executeReport( Locale locale ) |
77 | |
{ |
78 | 1 | PluginManagementRenderer r = |
79 | |
new PluginManagementRenderer( getLog(), getSink(), locale, getI18N( locale ), |
80 | |
project.getPluginManagement().getPlugins(), project, mavenProjectBuilder, |
81 | |
artifactFactory, localRepository ); |
82 | 1 | r.render(); |
83 | 1 | } |
84 | |
|
85 | |
|
86 | |
public String getOutputName() |
87 | |
{ |
88 | 2 | return "plugin-management"; |
89 | |
} |
90 | |
|
91 | |
@Override |
92 | |
protected String getI18Nsection() |
93 | |
{ |
94 | 1 | return "pluginManagement"; |
95 | |
} |
96 | |
|
97 | |
@Override |
98 | |
public boolean canGenerateReport() |
99 | |
{ |
100 | 2 | return project.getPluginManagement() != null && project.getPluginManagement().getPlugins() != null |
101 | |
&& !project.getPluginManagement().getPlugins().isEmpty(); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | 1 | protected static class PluginManagementRenderer |
114 | |
extends AbstractProjectInfoRenderer |
115 | |
{ |
116 | |
private final Log log; |
117 | |
|
118 | |
private final List<Plugin> pluginManagement; |
119 | |
|
120 | |
private final MavenProject project; |
121 | |
|
122 | |
private final MavenProjectBuilder mavenProjectBuilder; |
123 | |
|
124 | |
private final ArtifactFactory artifactFactory; |
125 | |
|
126 | |
private final ArtifactRepository localRepository; |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public PluginManagementRenderer( Log log, Sink sink, Locale locale, I18N i18n, List<Plugin> plugins, |
140 | |
MavenProject project, MavenProjectBuilder mavenProjectBuilder, |
141 | |
ArtifactFactory artifactFactory, ArtifactRepository localRepository ) |
142 | |
{ |
143 | 1 | super( sink, i18n, locale ); |
144 | |
|
145 | 1 | this.log = log; |
146 | |
|
147 | 1 | this.pluginManagement = plugins; |
148 | |
|
149 | 1 | this.project = project; |
150 | |
|
151 | 1 | this.mavenProjectBuilder = mavenProjectBuilder; |
152 | |
|
153 | 1 | this.artifactFactory = artifactFactory; |
154 | |
|
155 | 1 | this.localRepository = localRepository; |
156 | 1 | } |
157 | |
|
158 | |
@Override |
159 | |
protected String getI18Nsection() |
160 | |
{ |
161 | 2 | return "pluginManagement"; |
162 | |
} |
163 | |
|
164 | |
@Override |
165 | |
public void renderBody() |
166 | |
{ |
167 | |
|
168 | 1 | renderSectionPluginManagement(); |
169 | 1 | } |
170 | |
|
171 | |
private void renderSectionPluginManagement() |
172 | |
{ |
173 | 1 | String[] tableHeader = getPluginTableHeader(); |
174 | |
|
175 | 1 | startSection( getTitle() ); |
176 | |
|
177 | |
|
178 | 1 | Collections.sort( pluginManagement, getPluginComparator() ); |
179 | |
|
180 | 1 | startTable(); |
181 | 1 | tableHeader( tableHeader ); |
182 | |
|
183 | 1 | for ( Plugin plugin : pluginManagement ) |
184 | |
{ |
185 | |
VersionRange versionRange; |
186 | 2 | if ( StringUtils.isEmpty( plugin.getVersion() ) ) |
187 | |
{ |
188 | 1 | versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION ); |
189 | |
} |
190 | |
else |
191 | |
{ |
192 | 1 | versionRange = VersionRange.createFromVersion( plugin.getVersion() ); |
193 | |
} |
194 | |
|
195 | 2 | Artifact pluginArtifact = artifactFactory.createParentArtifact( plugin.getGroupId(), plugin |
196 | |
.getArtifactId(), versionRange.toString() ); |
197 | |
@SuppressWarnings( "unchecked" ) |
198 | 2 | List<ArtifactRepository> artifactRepositories = project.getPluginArtifactRepositories(); |
199 | 2 | if ( artifactRepositories == null ) |
200 | |
{ |
201 | 2 | artifactRepositories = new ArrayList<ArtifactRepository>(); |
202 | |
} |
203 | |
try |
204 | |
{ |
205 | 2 | MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact, |
206 | |
artifactRepositories, |
207 | |
localRepository ); |
208 | 2 | tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject |
209 | |
.getVersion(), pluginProject.getUrl() ) ); |
210 | |
} |
211 | 0 | catch ( ProjectBuildingException e ) |
212 | |
{ |
213 | 0 | log.info( "Could not build project for: " + plugin.getArtifactId() + ":" + e.getMessage(), e ); |
214 | 0 | tableRow( getPluginRow( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), null ) ); |
215 | 2 | } |
216 | |
|
217 | 2 | } |
218 | 1 | endTable(); |
219 | |
|
220 | 1 | endSection(); |
221 | 1 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
private String[] getPluginTableHeader() |
228 | |
{ |
229 | |
|
230 | 1 | String groupId = getI18nString( "dependencyManagement", "column.groupId" ); |
231 | 1 | String artifactId = getI18nString( "dependencyManagement", "column.artifactId" ); |
232 | 1 | String version = getI18nString( "dependencyManagement", "column.version" ); |
233 | 1 | return new String[] { groupId, artifactId, version }; |
234 | |
} |
235 | |
|
236 | |
private String[] getPluginRow( String groupId, String artifactId, String version, String link ) |
237 | |
{ |
238 | 2 | artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link ); |
239 | 2 | return new String[] { groupId, artifactId, version }; |
240 | |
} |
241 | |
|
242 | |
private Comparator<Plugin> getPluginComparator() |
243 | |
{ |
244 | 1 | return new Comparator<Plugin>() |
245 | 2 | { |
246 | |
|
247 | |
public int compare( Plugin a1, Plugin a2 ) |
248 | |
{ |
249 | 1 | int result = a1.getGroupId().compareTo( a2.getGroupId() ); |
250 | 1 | if ( result == 0 ) |
251 | |
{ |
252 | 1 | result = a1.getArtifactId().compareTo( a2.getArtifactId() ); |
253 | |
} |
254 | 1 | return result; |
255 | |
} |
256 | |
}; |
257 | |
} |
258 | |
} |
259 | |
} |