View Javadoc

1   package org.apache.maven.plugin.coreit;
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 org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.reporting.MavenReport;
26  import org.codehaus.doxia.sink.Sink;
27  
28  import java.io.File;
29  import java.lang.reflect.InvocationHandler;
30  import java.lang.reflect.Method;
31  import java.lang.reflect.Proxy;
32  import java.util.List;
33  import java.util.Locale;
34  
35  /**
36   * Generates the available/configured reports.
37   * 
38   * @goal generate
39   * @phase site
40   * @requiresReports true
41   * 
42   * @author Benjamin Bentmann
43   * @version $Id: GenerateMojo.java 712313 2008-11-07 23:59:37Z bentmann $
44   */
45  public class GenerateMojo
46      extends AbstractMojo
47  {
48  
49      /**
50       * The path to the output directory of the site.
51       * 
52       * @parameter default-value="${project.reporting.outputDirectory}"
53       */
54      private File outputDirectory;
55  
56      /**
57       * The language for the reports.
58       * 
59       * @parameter default-value="en"
60       */
61      private String language = "en";
62  
63      /**
64       * A flag whether to ignore errors from reports and continue the generation.
65       * 
66       * @parameter default-value="false"
67       */
68      private boolean ignoreErrors;
69  
70      /**
71       * The reports configured for the current build.
72       * 
73       * @parameter expression="${reports}"
74       * @required
75       * @readonly
76       */
77      private List reports;
78  
79      /**
80       * Runs this mojo.
81       * 
82       * @throws MojoExecutionException If the output file could not be created.
83       */
84      public void execute()
85          throws MojoExecutionException, MojoFailureException
86      {
87          getLog().info( "[MAVEN-CORE-IT-LOG] Using output directory " + outputDirectory );
88  
89          Locale locale = new Locale( language );
90          getLog().info( "[MAVEN-CORE-IT-LOG] Using locale " + locale );
91  
92          InvocationHandler handler = new InvocationHandler()
93          {
94  
95              public Object invoke( Object proxy, Method method, Object[] args )
96                  throws Throwable
97              {
98                  return null;
99              }
100 
101         };
102         Sink sink = (Sink) Proxy.newProxyInstance( getClass().getClassLoader(), new Class[] { Sink.class }, handler );
103 
104         for ( int i = 0; i < reports.size(); i++ )
105         {
106             MavenReport report = (MavenReport) reports.get( i );
107 
108             if ( report.canGenerateReport() )
109             {
110                 getLog().info( "[MAVEN-CORE-IT-LOG] Generating report " + report );
111                 try
112                 {
113                     report.setReportOutputDirectory( outputDirectory );
114                     report.generate( sink, locale );
115                 }
116                 catch ( Throwable e )
117                 {
118                     getLog().warn( "[MAVEN-CORE-IT-LOG]   " + e, e );
119                     if ( !ignoreErrors )
120                     {
121                         throw new MojoExecutionException( "Failed to generate report " + report, e );
122                     }
123                 }
124             }
125             else
126             {
127                 getLog().info( "[MAVEN-CORE-IT-LOG] Skipping report " + report );
128             }
129         }
130     }
131 
132 }