View Javadoc
1   package org.codehaus.mojo.jdepend;
2   
3   /*
4    * #%L
5    * JDepend Maven Plugin
6    * %%
7    * Copyright (C) 2006 - 2014 Codehaus
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
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   * @author Karl-Heinz Marbaise
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       * Directory where the generated output site files will be located.
50       */
51      @Parameter( defaultValue = "${project.build.directory}/site", property = "jdepend.outputDirectory", required = true )
52      private String outputDirectory;
53  
54      /**
55       * Directory of the project.
56       */
57      @Parameter( defaultValue = "${basedir}", property = "jdepend.projectDirectory" )
58      private String projectDirectory;
59  
60      /**
61       * Directory containing the class files.
62       */
63      @Parameter( defaultValue = "${project.build.outputDirectory}", property = "jdepend.classDirectory", required = true )
64      private String classDirectory;
65  
66      /**
67       * Location of the generated JDepend xml report.
68       */
69      @Parameter( defaultValue = "${project.build.directory}/jdepend-report.xml", required = true, readonly = true )
70      private String reportFile;
71  
72      /**
73       * Skip execution of the plugin.
74       */
75      @Parameter( defaultValue = "false", property = "jdepend.skip" )
76      private boolean skip;
77  
78      /**
79       * Doxia Site Renderer
80       */
81      @Component
82      private Renderer siteRenderer;
83  
84      /*
85       * (non-Javadoc)
86       * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
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      * (non-Javadoc)
124      * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
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      * Sets and get the arguments passed for the JDepend.
138      * 
139      * @param argument Accepts parameter with "-file" string.
140      * @param locationXMLreportFile Accepts the location of the generated JDepend xml report file.
141      * @param classDir Accepts the location of the classes.
142      * @return String[] Returns the array to be pass as parameters for JDepend.
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      * (non-Javadoc)
176      * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
177      */
178     public String getDescription( Locale locale )
179     {
180         return getBundle( locale ).getString( "report.jdepend.description" );
181     }
182 
183     /*
184      * (non-Javadoc)
185      * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
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      * (non-Javadoc)
200      * @see org.apache.maven.reporting.MavenReport#getOutputName()
201      */
202     public String getOutputName()
203     {
204         return "jdepend-report";
205     }
206 
207     /*
208      * (non-Javadoc)
209      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
210      */
211     public MavenProject getProject()
212     {
213         return project;
214     }
215 
216     /**
217      * @param project
218      */
219     public void setProject( MavenProject project )
220     {
221         this.project = project;
222     }
223 
224     /*
225      * (non-Javadoc)
226      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
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      * @return The argument.
240      */
241     public String getArgument()
242     {
243         return "-file";
244     }
245 
246     /**
247      * @return
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      * (non-Javadoc)
261      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
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 }