1 | |
package org.apache.maven.plugins.surefire.report; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Arrays; |
25 | |
import java.util.Collections; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
import java.util.Locale; |
29 | |
import java.util.ResourceBundle; |
30 | |
|
31 | |
import org.apache.maven.doxia.siterenderer.Renderer; |
32 | |
import org.apache.maven.model.ReportPlugin; |
33 | |
import org.apache.maven.project.MavenProject; |
34 | |
import org.apache.maven.reporting.AbstractMavenReport; |
35 | |
import org.apache.maven.reporting.MavenReportException; |
36 | |
import org.codehaus.plexus.util.PathTool; |
37 | |
import org.codehaus.plexus.util.StringUtils; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 4 | public class SurefireReportMojo |
49 | |
extends AbstractMavenReport |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
private File outputDirectory; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private Renderer siteRenderer; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private MavenProject project; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
private boolean showSuccess; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private File[] reportsDirectories; |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
private File reportsDirectory; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
private List reactorProjects; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
private String outputName; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
private File xrefLocation; |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private boolean linkXRef; |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
private boolean aggregate; |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public void executeReport( Locale locale ) |
138 | |
throws MavenReportException |
139 | |
{ |
140 | 4 | final List reportsDirectoryList = new ArrayList(); |
141 | |
|
142 | 4 | if ( reportsDirectory != null ) |
143 | |
{ |
144 | 4 | if ( reportsDirectories != null ) |
145 | |
{ |
146 | 0 | reportsDirectoryList.addAll( Arrays.asList( reportsDirectories ) ); |
147 | |
} |
148 | 4 | reportsDirectoryList.add( reportsDirectory ); |
149 | |
} |
150 | 4 | if ( aggregate ) |
151 | |
{ |
152 | 0 | if ( !project.isExecutionRoot() ) |
153 | |
{ |
154 | 0 | return; |
155 | |
} |
156 | 0 | if ( reportsDirectories == null ) |
157 | |
{ |
158 | 0 | for ( Iterator i = getProjectsWithoutRoot().iterator(); i.hasNext(); ) |
159 | |
{ |
160 | 0 | reportsDirectoryList.add( getSurefireReportsDirectory( (MavenProject) i.next() ) ); |
161 | |
} |
162 | |
} |
163 | |
else |
164 | |
{ |
165 | |
|
166 | |
|
167 | 0 | String parentBaseDir = getProject().getBasedir().getAbsolutePath(); |
168 | 0 | for ( Iterator i = getProjectsWithoutRoot().iterator(); i.hasNext(); ) |
169 | |
{ |
170 | 0 | MavenProject subProject = (MavenProject) i.next(); |
171 | 0 | String moduleBaseDir = subProject.getBasedir().getAbsolutePath(); |
172 | 0 | for ( int d = 0; d < reportsDirectories.length; d++ ) |
173 | |
{ |
174 | 0 | String reportDir = reportsDirectories[d].getPath(); |
175 | 0 | if ( reportDir.startsWith( parentBaseDir ) ) |
176 | |
{ |
177 | 0 | reportDir = reportDir.substring( parentBaseDir.length() ); |
178 | |
} |
179 | 0 | File reportsDirectory = new File( moduleBaseDir, reportDir ); |
180 | 0 | if ( reportsDirectory.exists() && reportsDirectory.isDirectory() ) |
181 | |
{ |
182 | 0 | getLog().debug( "Adding report dir : " + moduleBaseDir + reportDir ); |
183 | 0 | reportsDirectoryList.add( reportsDirectory ); |
184 | |
} |
185 | |
} |
186 | 0 | } |
187 | 0 | } |
188 | |
} |
189 | |
else |
190 | |
{ |
191 | 4 | if ( reportsDirectoryList.size() == 0 ) |
192 | |
{ |
193 | |
|
194 | 0 | reportsDirectoryList.add( getSurefireReportsDirectory( project ) ); |
195 | |
} |
196 | |
} |
197 | |
|
198 | 4 | SurefireReportGenerator report = |
199 | |
new SurefireReportGenerator( reportsDirectoryList, locale, showSuccess, determineXrefLocation() ); |
200 | |
|
201 | 4 | report.doGenerateReport( getBundle( locale ), getSink() ); |
202 | 4 | } |
203 | |
|
204 | |
private File getSurefireReportsDirectory( MavenProject subProject ) |
205 | |
{ |
206 | 0 | String buildDir = subProject.getBuild().getDirectory(); |
207 | 0 | return new File( buildDir + "/surefire-reports" ); |
208 | |
} |
209 | |
|
210 | |
private List getProjectsWithoutRoot() |
211 | |
{ |
212 | 0 | List result = new ArrayList(); |
213 | 0 | for ( Iterator i = reactorProjects.iterator(); i.hasNext(); ) |
214 | |
{ |
215 | 0 | MavenProject subProject = (MavenProject) i.next(); |
216 | 0 | if ( !project.equals( subProject ) ) |
217 | |
{ |
218 | 0 | result.add( subProject ); |
219 | |
} |
220 | 0 | } |
221 | 0 | return result; |
222 | |
|
223 | |
} |
224 | |
|
225 | |
private String determineXrefLocation() |
226 | |
{ |
227 | 4 | String location = null; |
228 | |
|
229 | 4 | if ( linkXRef ) |
230 | |
{ |
231 | 3 | String relativePath = PathTool.getRelativePath( getOutputDirectory(), xrefLocation.getAbsolutePath() ); |
232 | 3 | if ( StringUtils.isEmpty( relativePath ) ) |
233 | |
{ |
234 | 1 | relativePath = "."; |
235 | |
} |
236 | 3 | relativePath = relativePath + "/" + xrefLocation.getName(); |
237 | 3 | if ( xrefLocation.exists() ) |
238 | |
{ |
239 | |
|
240 | 0 | location = relativePath; |
241 | |
} |
242 | |
else |
243 | |
{ |
244 | |
|
245 | 3 | for ( Iterator reports = project.getReportPlugins().iterator(); reports.hasNext(); ) |
246 | |
{ |
247 | 2 | ReportPlugin report = (ReportPlugin) reports.next(); |
248 | |
|
249 | 2 | String artifactId = report.getArtifactId(); |
250 | 2 | if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) ) |
251 | |
{ |
252 | 2 | location = relativePath; |
253 | |
} |
254 | 2 | } |
255 | |
} |
256 | |
|
257 | 3 | if ( location == null ) |
258 | |
{ |
259 | 1 | getLog().warn( "Unable to locate Test Source XRef to link to - DISABLED" ); |
260 | |
} |
261 | |
} |
262 | 4 | return location; |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
public String getName( Locale locale ) |
269 | |
{ |
270 | 0 | return getBundle( locale ).getString( "report.surefire.name" ); |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public String getDescription( Locale locale ) |
277 | |
{ |
278 | 0 | return getBundle( locale ).getString( "report.surefire.description" ); |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
protected Renderer getSiteRenderer() |
285 | |
{ |
286 | 8 | return siteRenderer; |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
protected MavenProject getProject() |
293 | |
{ |
294 | 0 | return project; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
public String getOutputName() |
301 | |
{ |
302 | 4 | return outputName; |
303 | |
} |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
protected String getOutputDirectory() |
309 | |
{ |
310 | 7 | return outputDirectory.getAbsolutePath(); |
311 | |
} |
312 | |
|
313 | |
private ResourceBundle getBundle( Locale locale ) |
314 | |
{ |
315 | 4 | return ResourceBundle.getBundle( "surefire-report", locale, this.getClass().getClassLoader() ); |
316 | |
} |
317 | |
} |