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