aboutsummaryrefslogtreecommitdiff
path: root/jacoco-maven-plugin/src/org/jacoco/maven/AbstractReportMojo.java
blob: 097ecb100f94207b056b02b536f941598ac75d8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Evgeny Mandrikov - initial API and implementation
 *    troosan - add support for format selection
 *
 *******************************************************************************/
package org.jacoco.maven;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import org.apache.maven.doxia.sink.SinkFactory;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenMultiPageReport;
import org.apache.maven.reporting.MavenReportException;
import org.jacoco.report.IReportGroupVisitor;
import org.jacoco.report.IReportVisitor;

/**
 * Base class for creating a code coverage report for tests of a single project
 * in multiple formats (HTML, XML, and CSV).
 */
public abstract class AbstractReportMojo extends AbstractMojo
		implements MavenMultiPageReport {

	/**
	 * Encoding of the generated reports.
	 */
	@Parameter(property = "project.reporting.outputEncoding", defaultValue = "UTF-8")
	String outputEncoding;

	/**
	 * A list of report formats to generate. Supported formats are HTML, XML and
	 * CSV. Defaults to all formats if no values are given.
	 *
	 * @since 0.8.7
	 */
	@Parameter(defaultValue = "HTML,XML,CSV")
	List<ReportFormat> formats;

	/**
	 * Name of the root node HTML report pages.
	 *
	 * @since 0.7.7
	 */
	@Parameter(defaultValue = "${project.name}")
	String title;

	/**
	 * Footer text used in HTML report pages.
	 *
	 * @since 0.7.7
	 */
	@Parameter
	String footer;

	/**
	 * Encoding of the source files.
	 */
	@Parameter(property = "project.build.sourceEncoding", defaultValue = "UTF-8")
	String sourceEncoding;

	/**
	 * A list of class files to include in the report. May use wildcard
	 * characters (* and ?). When not specified everything will be included.
	 */
	@Parameter
	List<String> includes;

	/**
	 * A list of class files to exclude from the report. May use wildcard
	 * characters (* and ?). When not specified nothing will be excluded.
	 */
	@Parameter
	List<String> excludes;

	/**
	 * Flag used to suppress execution.
	 */
	@Parameter(property = "jacoco.skip", defaultValue = "false")
	boolean skip;

	/**
	 * Maven project.
	 */
	@Parameter(property = "project", readonly = true)
	MavenProject project;

	public String getDescription(final Locale locale) {
		return getName(locale) + " Coverage Report.";
	}

	public boolean isExternalReport() {
		return true;
	}

	public String getCategoryName() {
		return CATEGORY_PROJECT_REPORTS;
	}

	/**
	 * Returns the list of class files to include in the report.
	 *
	 * @return class files to include, may contain wildcard characters
	 */
	List<String> getIncludes() {
		return includes;
	}

	/**
	 * Returns the list of class files to exclude from the report.
	 *
	 * @return class files to exclude, may contain wildcard characters
	 */
	List<String> getExcludes() {
		return excludes;
	}

	public boolean canGenerateReport() {
		if (skip) {
			getLog().info(
					"Skipping JaCoCo execution because property jacoco.skip is set.");
			return false;
		}
		if (!canGenerateReportRegardingDataFiles()) {
			getLog().info(
					"Skipping JaCoCo execution due to missing execution data file.");
			return false;
		}
		if (!canGenerateReportRegardingClassesDirectory()) {
			getLog().info(
					"Skipping JaCoCo execution due to missing classes directory.");
			return false;
		}
		return true;
	}

	abstract boolean canGenerateReportRegardingDataFiles();

	abstract boolean canGenerateReportRegardingClassesDirectory();

	abstract File getOutputDirectory();

	public void generate(
			@SuppressWarnings("deprecation") final org.codehaus.doxia.sink.Sink sink,
			final Locale locale) throws MavenReportException {
		generate(sink, null, locale);
	}

	public void generate(final org.apache.maven.doxia.sink.Sink sink,
			final SinkFactory sinkFactory, final Locale locale)
			throws MavenReportException {
		if (!canGenerateReport()) {
			return;
		}
		executeReport(locale);
	}

	/**
	 * This method is called when the report generation is invoked directly as a
	 * standalone Mojo.
	 */
	public void execute() throws MojoExecutionException {
		if (!canGenerateReport()) {
			return;
		}
		try {
			executeReport(Locale.getDefault());
		} catch (final MavenReportException e) {
			throw new MojoExecutionException("An error has occurred in "
					+ getName(Locale.ENGLISH) + " report generation.", e);
		}
	}

	private void executeReport(final Locale locale)
			throws MavenReportException {
		try {
			final ReportSupport support = new ReportSupport(getLog());
			loadExecutionData(support);
			addFormatters(support, locale);
			final IReportVisitor visitor = support.initRootVisitor();
			createReport(visitor, support);
			visitor.visitEnd();
		} catch (final IOException e) {
			throw new MavenReportException(
					"Error while creating report: " + e.getMessage(), e);
		}
	}

	private void addFormatters(final ReportSupport support, final Locale locale)
			throws IOException {
		getOutputDirectory().mkdirs();
		for (final ReportFormat f : formats) {
			support.addVisitor(f.createVisitor(this, locale));
		}
	}

	abstract void loadExecutionData(final ReportSupport support)
			throws IOException;

	abstract void createReport(final IReportGroupVisitor visitor,
			final ReportSupport support) throws IOException;

}