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