1 package org.codehaus.mojo.jdepend;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import jdepend.xmlui.JDepend;
24 import org.apache.maven.doxia.sink.Sink;
25 import org.apache.maven.doxia.siterenderer.Renderer;
26 import org.apache.maven.plugins.annotations.Component;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.reporting.AbstractMavenReport;
30 import org.apache.maven.reporting.MavenReportException;
31 import java.io.File;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Locale;
35 import java.util.ResourceBundle;
36
37
38
39
40 public abstract class AbstractJDependMojo
41 extends AbstractMavenReport
42 {
43 JDependXMLReportParser xmlParser;
44
45 @Parameter( defaultValue = "${project}", readonly = true, required = true )
46 private MavenProject project;
47
48
49
50
51 @Parameter( defaultValue = "${project.build.directory}/site", property = "jdepend.outputDirectory", required = true )
52 private String outputDirectory;
53
54
55
56
57 @Parameter( defaultValue = "${basedir}", property = "jdepend.projectDirectory" )
58 private String projectDirectory;
59
60
61
62
63 @Parameter( defaultValue = "${project.build.outputDirectory}", property = "jdepend.classDirectory", required = true )
64 private String classDirectory;
65
66
67
68
69 @Parameter( defaultValue = "${project.build.directory}/jdepend-report.xml", required = true, readonly = true )
70 private String reportFile;
71
72
73
74
75 @Parameter( defaultValue = "false", property = "jdepend.skip" )
76 private boolean skip;
77
78
79
80
81 @Component
82 private Renderer siteRenderer;
83
84
85
86
87
88 public void executeReport( Locale locale )
89 throws MavenReportException
90 {
91 if ( skip )
92 {
93 getLog().info( "Skipping execution on behalf of user" );
94 return;
95 }
96
97 try
98 {
99 File outputDirFile = new File( outputDirectory );
100
101 if ( !outputDirFile.exists() )
102 {
103 boolean success = outputDirFile.mkdirs();
104 if ( !success )
105 {
106 throw new MavenReportException( "Could not create directory " + outputDirectory );
107 }
108 }
109
110 JDepend.main( getArgumentList( getArgument(), getReportFile(), getClassDirectory() ) );
111
112 xmlParser = new JDependXMLReportParser( new File( getReportFile() ) );
113
114 generateReport( locale );
115 }
116 catch ( Exception e )
117 {
118 throw new MavenReportException( "Failed to execute JDepend", e );
119 }
120 }
121
122
123
124
125
126 public boolean canGenerateReport()
127 {
128 File srcDir = new File( classDirectory );
129 if ( !srcDir.exists() )
130 {
131 return false;
132 }
133 return true;
134 }
135
136
137
138
139
140
141
142
143
144 private String[] getArgumentList( String argument, String locationXMLreportFile, String classDir )
145 {
146 List<String> argList = new ArrayList<String>();
147
148 argList.add( argument );
149
150 argList.add( locationXMLreportFile );
151
152 argList.add( classDir );
153
154 return (String[]) argList.toArray( new String[argList.size()] );
155 }
156
157 public void generateReport( Locale locale )
158 throws MavenReportException
159 {
160 Sink sink;
161 ReportGenerator report = new ReportGenerator();
162 try
163 {
164 sink = getSink();
165
166 report.doGenerateReport( getBundle( locale ), sink, xmlParser );
167 }
168 catch ( Exception e )
169 {
170 throw new MavenReportException( "Failed to generate JDepend report", e );
171 }
172 }
173
174
175
176
177
178 public String getDescription( Locale locale )
179 {
180 return getBundle( locale ).getString( "report.jdepend.description" );
181 }
182
183
184
185
186
187 public String getName( Locale locale )
188 {
189 return getBundle( locale ).getString( "report.jdepend.name" );
190 }
191
192 private ResourceBundle getBundle( Locale locale )
193 {
194 return ResourceBundle.getBundle( "org.codehaus.mojo.jdepend.jdepend-report", locale,
195 this.getClass().getClassLoader() );
196 }
197
198
199
200
201
202 public String getOutputName()
203 {
204 return "jdepend-report";
205 }
206
207
208
209
210
211 public MavenProject getProject()
212 {
213 return project;
214 }
215
216
217
218
219 public void setProject( MavenProject project )
220 {
221 this.project = project;
222 }
223
224
225
226
227
228 public String getOutputDirectory()
229 {
230 return outputDirectory;
231 }
232
233 public void setOutputDirectory( String outputDirectory )
234 {
235 this.outputDirectory = outputDirectory;
236 }
237
238
239
240
241 public String getArgument()
242 {
243 return "-file";
244 }
245
246
247
248
249 public String getReportFile()
250 {
251 return reportFile;
252 }
253
254 public void setReportFile( String reportFile )
255 {
256 this.reportFile = reportFile;
257 }
258
259
260
261
262
263 public Renderer getSiteRenderer()
264 {
265 return siteRenderer;
266 }
267
268 public void setSiteRenderer( Renderer siteRenderer )
269 {
270 this.siteRenderer = siteRenderer;
271 }
272
273 public String getProjectDirectory()
274 {
275 return projectDirectory;
276 }
277
278 public void setProjectDirectory( String projectDirectory )
279 {
280 this.projectDirectory = projectDirectory;
281 }
282
283 public String getClassDirectory()
284 {
285 return classDirectory;
286 }
287
288 public void setClassDirectory( String classDirectory )
289 {
290 this.classDirectory = classDirectory;
291 }
292 }