|
| 1 | +package org.codefilarete.maven.jacoco; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Locale; |
| 7 | + |
| 8 | +import org.apache.maven.doxia.sink.SinkFactory; |
| 9 | +import org.apache.maven.plugin.AbstractMojo; |
| 10 | +import org.apache.maven.plugin.MojoExecutionException; |
| 11 | +import org.apache.maven.plugins.annotations.Parameter; |
| 12 | +import org.apache.maven.project.MavenProject; |
| 13 | +import org.apache.maven.reporting.MavenMultiPageReport; |
| 14 | +import org.apache.maven.reporting.MavenReportException; |
| 15 | +import org.jacoco.report.IReportGroupVisitor; |
| 16 | +import org.jacoco.report.IReportVisitor; |
| 17 | + |
| 18 | +/** |
| 19 | + * Base class for creating a code coverage report for tests of a single project |
| 20 | + * in multiple formats (HTML, XML, and CSV). |
| 21 | + */ |
| 22 | +public abstract class AbstractReportMojo extends AbstractMojo implements MavenMultiPageReport { |
| 23 | + |
| 24 | + /** |
| 25 | + * Encoding of the generated reports. |
| 26 | + */ |
| 27 | + @Parameter(property = "project.reporting.outputEncoding", defaultValue = "UTF-8") |
| 28 | + String outputEncoding; |
| 29 | + |
| 30 | + /** |
| 31 | + * A list of report formats to generate. Supported formats are HTML, XML and |
| 32 | + * CSV. Defaults to all formats if no values are given. |
| 33 | + * |
| 34 | + * @since 0.8.7 |
| 35 | + */ |
| 36 | + @Parameter(defaultValue = "HTML,XML,CSV") |
| 37 | + List<ReportFormat> formats; |
| 38 | + |
| 39 | + /** |
| 40 | + * Name of the root node HTML report pages. |
| 41 | + * |
| 42 | + * @since 0.7.7 |
| 43 | + */ |
| 44 | + @Parameter(defaultValue = "${project.name}") |
| 45 | + String title; |
| 46 | + |
| 47 | + /** |
| 48 | + * Footer text used in HTML report pages. |
| 49 | + * |
| 50 | + * @since 0.7.7 |
| 51 | + */ |
| 52 | + @Parameter |
| 53 | + String footer; |
| 54 | + |
| 55 | + /** |
| 56 | + * Encoding of the source files. |
| 57 | + */ |
| 58 | + @Parameter(property = "project.build.sourceEncoding", defaultValue = "UTF-8") |
| 59 | + String sourceEncoding; |
| 60 | + |
| 61 | + /** |
| 62 | + * A list of class files to include in the report. May use wildcard |
| 63 | + * characters (* and ?). When not specified everything will be included. |
| 64 | + */ |
| 65 | + @Parameter |
| 66 | + List<String> includes; |
| 67 | + |
| 68 | + /** |
| 69 | + * A list of class files to exclude from the report. May use wildcard |
| 70 | + * characters (* and ?). When not specified nothing will be excluded. |
| 71 | + */ |
| 72 | + @Parameter |
| 73 | + List<String> excludes; |
| 74 | + |
| 75 | + /** |
| 76 | + * Flag used to suppress execution. |
| 77 | + */ |
| 78 | + @Parameter(property = "jacoco.skip", defaultValue = "false") |
| 79 | + boolean skip; |
| 80 | + |
| 81 | + /** |
| 82 | + * Maven project. |
| 83 | + */ |
| 84 | + @Parameter(property = "project", readonly = true) |
| 85 | + MavenProject project; |
| 86 | + |
| 87 | + public String getDescription(final Locale locale) { |
| 88 | + return getName(locale) + " Coverage Report."; |
| 89 | + } |
| 90 | + |
| 91 | + public boolean isExternalReport() { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + public String getCategoryName() { |
| 96 | + return CATEGORY_PROJECT_REPORTS; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns the list of class files to include in the report. |
| 101 | + * |
| 102 | + * @return class files to include, may contain wildcard characters |
| 103 | + */ |
| 104 | + List<String> getIncludes() { |
| 105 | + return includes; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the list of class files to exclude from the report. |
| 110 | + * |
| 111 | + * @return class files to exclude, may contain wildcard characters |
| 112 | + */ |
| 113 | + List<String> getExcludes() { |
| 114 | + return excludes; |
| 115 | + } |
| 116 | + |
| 117 | + public boolean canGenerateReport() { |
| 118 | + if (skip) { |
| 119 | + getLog().info("Skipping JaCoCo execution because property jacoco.skip is set."); |
| 120 | + return false; |
| 121 | + } |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + abstract File getOutputDirectory(); |
| 126 | + |
| 127 | + public void generate( |
| 128 | + @SuppressWarnings("deprecation") final org.codehaus.doxia.sink.Sink sink, |
| 129 | + final Locale locale) throws MavenReportException { |
| 130 | + generate(sink, null, locale); |
| 131 | + } |
| 132 | + |
| 133 | + public void generate(final org.apache.maven.doxia.sink.Sink sink, |
| 134 | + final SinkFactory sinkFactory, final Locale locale) |
| 135 | + throws MavenReportException { |
| 136 | + if (!canGenerateReport()) { |
| 137 | + return; |
| 138 | + } |
| 139 | + executeReport(locale); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * This method is called when the report generation is invoked directly as a |
| 144 | + * standalone Mojo. |
| 145 | + */ |
| 146 | + public void execute() throws MojoExecutionException { |
| 147 | + if (!canGenerateReport()) { |
| 148 | + return; |
| 149 | + } |
| 150 | + try { |
| 151 | + executeReport(Locale.getDefault()); |
| 152 | + } catch (final MavenReportException e) { |
| 153 | + throw new MojoExecutionException("An error has occurred in " |
| 154 | + + getName(Locale.ENGLISH) + " report generation.", e); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private void executeReport(final Locale locale) |
| 159 | + throws MavenReportException { |
| 160 | + try { |
| 161 | + final ReportSupport support = new ReportSupport(getLog()); |
| 162 | + loadExecutionData(support); |
| 163 | + addFormatters(support, locale); |
| 164 | + final IReportVisitor visitor = support.initRootVisitor(); |
| 165 | + createReport(visitor, support); |
| 166 | + visitor.visitEnd(); |
| 167 | + } catch (final IOException e) { |
| 168 | + throw new MavenReportException( |
| 169 | + "Error while creating report: " + e.getMessage(), e); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private void addFormatters(final ReportSupport support, final Locale locale) |
| 174 | + throws IOException { |
| 175 | + getOutputDirectory().mkdirs(); |
| 176 | + for (final ReportFormat f : formats) { |
| 177 | + support.addVisitor(f.createVisitor(this, locale)); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + abstract void loadExecutionData(final ReportSupport support) |
| 182 | + throws IOException; |
| 183 | + |
| 184 | + abstract void createReport(final IReportGroupVisitor visitor, |
| 185 | + final ReportSupport support) throws IOException; |
| 186 | + |
| 187 | +} |
0 commit comments