1 /*******************************************************************************
2  * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *    Evgeny Mandrikov - initial API and implementation
10  *    Kyle Lieber - implementation of CheckMojo
11  *
12  *******************************************************************************/
13 package org.jacoco.maven;
14 
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.Locale;
18 
19 import org.apache.maven.plugins.annotations.LifecyclePhase;
20 import org.apache.maven.plugins.annotations.Mojo;
21 import org.apache.maven.plugins.annotations.Parameter;
22 import org.jacoco.report.IReportGroupVisitor;
23 
24 /**
25  * Same as <code>report</code>, but provides default values suitable for
26  * integration-tests:
27  * <ul>
28  * <li>bound to <code>report-integration</code> phase</li>
29  * <li>different <code>dataFile</code></li>
30  * </ul>
31  *
32  * @since 0.6.4
33  */
34 @Mojo(name = "report-integration", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
35 public class ReportITMojo extends AbstractReportMojo {
36 
37 	/**
38 	 * Output directory for the reports. Note that this parameter is only
39 	 * relevant if the goal is run from the command line or from the default
40 	 * build lifecycle. If the goal is run indirectly as part of a site
41 	 * generation, the output directory configured in the Maven Site Plugin is
42 	 * used instead.
43 	 */
44 	@Parameter(defaultValue = "${project.reporting.outputDirectory}/jacoco-it")
45 	private File outputDirectory;
46 
47 	/**
48 	 * File with execution data.
49 	 */
50 	@Parameter(defaultValue = "${project.build.directory}/jacoco-it.exec")
51 	private File dataFile;
52 
53 	@Override
canGenerateReportRegardingDataFiles()54 	boolean canGenerateReportRegardingDataFiles() {
55 		return dataFile.exists();
56 	}
57 
58 	@Override
canGenerateReportRegardingClassesDirectory()59 	boolean canGenerateReportRegardingClassesDirectory() {
60 		return new File(getProject().getBuild().getOutputDirectory()).exists();
61 	}
62 
63 	@Override
loadExecutionData(final ReportSupport support)64 	void loadExecutionData(final ReportSupport support) throws IOException {
65 		support.loadExecutionData(dataFile);
66 	}
67 
68 	@Override
addFormatters(final ReportSupport support, final Locale locale)69 	void addFormatters(final ReportSupport support, final Locale locale)
70 			throws IOException {
71 		support.addAllFormatters(outputDirectory, outputEncoding, footer,
72 				locale);
73 	}
74 
75 	@Override
createReport(final IReportGroupVisitor visitor, final ReportSupport support)76 	void createReport(final IReportGroupVisitor visitor,
77 			final ReportSupport support) throws IOException {
78 		support.processProject(visitor, title, getProject(), getIncludes(),
79 				getExcludes(), sourceEncoding);
80 	}
81 
82 	@Override
getOutputDirectory()83 	protected String getOutputDirectory() {
84 		return outputDirectory.getAbsolutePath();
85 	}
86 
87 	@Override
setReportOutputDirectory(final File reportOutputDirectory)88 	public void setReportOutputDirectory(final File reportOutputDirectory) {
89 		if (reportOutputDirectory != null
90 				&& !reportOutputDirectory.getAbsolutePath().endsWith(
91 						"jacoco-it")) {
92 			outputDirectory = new File(reportOutputDirectory, "jacoco-it");
93 		} else {
94 			outputDirectory = reportOutputDirectory;
95 		}
96 	}
97 
getOutputName()98 	public String getOutputName() {
99 		return "jacoco-it/index";
100 	}
101 
getName(final Locale locale)102 	public String getName(final Locale locale) {
103 		return "JaCoCo IT";
104 	}
105 }
106