1 package org.apache.maven.plugin.javadoc;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.ResourceBundle;
26
27 import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext;
28 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.reporting.MavenReport;
32 import org.apache.maven.reporting.MavenReportException;
33 import org.codehaus.doxia.sink.Sink;
34 import org.codehaus.plexus.util.StringUtils;
35
36 /**
37 * Generates documentation for the <code>Java code</code> in the project using the standard
38 * <a href="http://java.sun.com/j2se/javadoc/">Javadoc Tool</a>.
39 *
40 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
41 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
42 * @version $Id: JavadocReport.html 829389 2012-08-19 17:23:07Z hboutemy $
43 * @since 2.0
44 * @goal javadoc
45 * @execute phase="generate-sources"
46 * @see <a href="http://java.sun.com/j2se/javadoc/">Javadoc Tool</a>
47 * @see <a href="http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/javadoc.html#options">Javadoc Options</a>
48 */
49 public class JavadocReport
50 extends AbstractJavadocMojo
51 implements MavenReport
52 {
53 // ----------------------------------------------------------------------
54 // Report Mojo Parameters
55 // ----------------------------------------------------------------------
56
57 /**
58 * Specifies the destination directory where javadoc saves the generated HTML files.
59 *
60 * @parameter expression="${project.reporting.outputDirectory}/apidocs"
61 * @required
62 */
63 private File reportOutputDirectory;
64
65 /**
66 * The name of the destination directory.
67 * <br/>
68 *
69 * @since 2.1
70 * @parameter expression="${destDir}" default-value="apidocs"
71 */
72 private String destDir;
73
74 /**
75 * The name of the Javadoc report.
76 *
77 * @since 2.1
78 * @parameter expression="${name}"
79 */
80 private String name;
81
82 /**
83 * The description of the Javadoc report.
84 *
85 * @since 2.1
86 * @parameter expression="${description}"
87 */
88 private String description;
89
90 // ----------------------------------------------------------------------
91 // Report public methods
92 // ----------------------------------------------------------------------
93
94 /** {@inheritDoc} */
95 public String getName( Locale locale )
96 {
97 if ( StringUtils.isEmpty( name ) )
98 {
99 return getBundle( locale ).getString( "report.javadoc.name" );
100 }
101
102 return name;
103 }
104
105 /** {@inheritDoc} */
106 public String getDescription( Locale locale )
107 {
108 if ( StringUtils.isEmpty( description ) )
109 {
110 return getBundle( locale ).getString( "report.javadoc.description" );
111 }
112
113 return description;
114 }
115
116 /** {@inheritDoc} */
117 public void generate( Sink sink, Locale locale )
118 throws MavenReportException
119 {
120 outputDirectory = getReportOutputDirectory();
121
122 executeReport( locale );
123 }
124
125 /** {@inheritDoc} */
126 public String getOutputName()
127 {
128 return destDir + "/index";
129 }
130
131 /** {@inheritDoc} */
132 public boolean isExternalReport()
133 {
134 return true;
135 }
136
137 /** {@inheritDoc} */
138 public boolean canGenerateReport()
139 {
140 boolean canGenerate;
141 if ( aggregate != isAggregator() )
142 {
143 canGenerate = false;
144 }
145 else if ( isAggregator() && !project.isExecutionRoot() )
146 {
147 canGenerate = false;
148 }
149 else
150 {
151 List sourcePaths = getSourcePaths();
152
153 List files = getFiles( sourcePaths );
154
155 canGenerate = canGenerateReport( files );
156 }
157 return canGenerate;
158 }
159
160 /** {@inheritDoc} */
161 public String getCategoryName()
162 {
163 return CATEGORY_PROJECT_REPORTS;
164 }
165
166 /** {@inheritDoc} */
167 public File getReportOutputDirectory()
168 {
169 if ( reportOutputDirectory == null )
170 {
171 return outputDirectory;
172 }
173
174 return reportOutputDirectory;
175 }
176
177 /**
178 * Method to set the directory where the generated reports will be put
179 *
180 * @param reportOutputDirectory the directory file to be set
181 */
182 public void setReportOutputDirectory( File reportOutputDirectory )
183 {
184 if ( ( reportOutputDirectory != null ) && ( !reportOutputDirectory.getAbsolutePath().endsWith( destDir ) ) )
185 {
186 this.reportOutputDirectory = new File( reportOutputDirectory, destDir );
187 }
188 else
189 {
190 this.reportOutputDirectory = reportOutputDirectory;
191 }
192 }
193
194 /** {@inheritDoc} */
195 public void execute()
196 throws MojoExecutionException, MojoFailureException
197 {
198 if ( skip )
199 {
200 getLog().info( "Skipping javadoc generation" );
201 return;
202 }
203
204 try
205 {
206 RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
207 SiteRendererSink sink = new SiteRendererSink( context );
208 Locale locale = Locale.getDefault();
209 generate( sink, locale );
210 }
211 catch ( MavenReportException e )
212 {
213 if ( failOnError )
214 {
215 throw new MojoExecutionException( "An error has occurred in " + getName( Locale.ENGLISH )
216 + " report generation:" + e.getMessage(), e );
217 }
218
219 getLog().error( "An error has occurred in " + getName( Locale.ENGLISH )
220 + " report generation:" + e.getMessage(), e );
221 }
222 catch ( RuntimeException e )
223 {
224 if ( failOnError )
225 {
226 throw e;
227 }
228
229 getLog().error( e.getMessage(), e );
230 }
231 }
232
233 /** {@inheritDoc} */
234 protected boolean isAggregator()
235 {
236 // only here for backward compatibility, this flag does not work reliably
237 return aggregate;
238 }
239
240 /**
241 * Gets the resource bundle for the specified locale.
242 *
243 * @param locale The locale of the currently generated report.
244 * @return The resource bundle for the requested locale.
245 */
246 private ResourceBundle getBundle( Locale locale )
247 {
248 return ResourceBundle.getBundle( "javadoc-report", locale, getClass().getClassLoader() );
249 }
250 }