1 /******************************************************************************* 2 * Copyright (c) 2009, 2015 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 * Brock Janiczak - initial API and implementation 10 * 11 *******************************************************************************/ 12 package org.jacoco.ant; 13 14 import java.io.File; 15 import java.io.IOException; 16 17 import org.apache.tools.ant.BuildException; 18 import org.apache.tools.ant.Task; 19 import org.jacoco.agent.AgentJar; 20 import org.jacoco.core.runtime.AgentOptions; 21 import org.jacoco.core.runtime.AgentOptions.OutputMode; 22 23 /** 24 * Base class for all coverage tasks that require agent options 25 */ 26 public class AbstractCoverageTask extends Task { 27 28 private final AgentOptions agentOptions; 29 30 private File destfile; 31 32 private boolean enabled; 33 34 /** 35 * Create default agent options 36 */ 37 protected AbstractCoverageTask() { 38 super(); 39 agentOptions = new AgentOptions(); 40 destfile = new File(AgentOptions.DEFAULT_DESTFILE); 41 enabled = true; 42 } 43 44 /** 45 * @return Whether or not the current task is enabled 46 */ 47 public boolean isEnabled() { 48 return enabled; 49 } 50 51 /** 52 * Sets whether or not the current task is enabled 53 * 54 * @param enabled 55 * Enablement state of the task 56 */ 57 public void setEnabled(final boolean enabled) { 58 this.enabled = enabled; 59 } 60 61 /** 62 * Sets the location to write coverage execution data to. Default is 63 * <code>jacoco.exec</code>. 64 * 65 * @param file 66 * Location to write coverage execution data to 67 */ 68 public void setDestfile(final File file) { 69 destfile = file; 70 } 71 72 /** 73 * Append execution coverage data if a coverage file is already present. 74 * Default is <code>true</code> 75 * 76 * @param append 77 * <code>true</code> to append execution data to an existing file 78 */ 79 public void setAppend(final boolean append) { 80 agentOptions.setAppend(append); 81 } 82 83 /** 84 * List of wildcard patterns classes to include for instrumentation. Default 85 * is <code>*</code> 86 * 87 * @param includes 88 * Wildcard pattern of included classes 89 */ 90 public void setIncludes(final String includes) { 91 agentOptions.setIncludes(includes); 92 } 93 94 /** 95 * List of wildcard patterns classes to exclude from instrumentation. 96 * Default is the empty string, no classes excluded 97 * 98 * @param excludes 99 * Wildcard pattern of excluded classes 100 */ 101 public void setExcludes(final String excludes) { 102 agentOptions.setExcludes(excludes); 103 } 104 105 /** 106 * List of wildcard patterns for classloaders that JaCoCo will not 107 * instrument classes from. Default is 108 * <code>sun.reflect.DelegatingClassLoader</code> 109 * 110 * @param exclClassLoader 111 * Wildcard pattern of class loaders to exclude 112 */ 113 public void setExclClassLoader(final String exclClassLoader) { 114 agentOptions.setExclClassloader(exclClassLoader); 115 } 116 117 /** 118 * Sets whether classes from the bootstrap classloader should be 119 * instrumented. 120 * 121 * @param include 122 * <code>true</code> if bootstrap classes should be instrumented 123 */ 124 public void setInclBootstrapClasses(final boolean include) { 125 agentOptions.setInclBootstrapClasses(include); 126 } 127 128 /** 129 * Sets the session identifier. Default is a auto-generated id 130 * 131 * @param id 132 * session identifier 133 */ 134 public void setSessionId(final String id) { 135 agentOptions.setSessionId(id); 136 } 137 138 /** 139 * Dump coverage data on VM termination. Default is <code>true</code> 140 * 141 * @param dumpOnExit 142 * <code>true</code> to write coverage data on VM termination 143 */ 144 public void setDumpOnExit(final boolean dumpOnExit) { 145 agentOptions.setDumpOnExit(dumpOnExit); 146 } 147 148 /** 149 * Sets the output method. Default is <code>file</code> 150 * 151 * @param output 152 * Output method 153 */ 154 public void setOutput(final String output) { 155 agentOptions.setOutput(output); 156 } 157 158 /** 159 * Sets the IP address or hostname to bind to when output method is tcp 160 * server or connect to when the output method is tcp client. Default is 161 * <code>localhost</code> 162 * 163 * @param address 164 * Address to bind or connect to 165 */ 166 public void setAddress(final String address) { 167 agentOptions.setAddress(address); 168 } 169 170 /** 171 * Sets the Port to bind to when the output method is tcp server or connect 172 * to when the output method is tcp client. Default is <code>6300</code> 173 * 174 * @param port 175 * port to bind to or connect to 176 */ 177 public void setPort(final int port) { 178 agentOptions.setPort(port); 179 } 180 181 /** 182 * Sets the directory where all class files seen by the agent should be 183 * dumped to. 184 * 185 * @param dir 186 * dump output location 187 */ 188 public void setClassdumpdir(final File dir) { 189 agentOptions.setClassDumpDir(dir.getAbsolutePath()); 190 } 191 192 /** 193 * Sets whether the agent should expose functionality via JMX. 194 * 195 * @param jmx 196 * <code>true</code> if JMX should be enabled 197 */ 198 public void setJmx(final boolean jmx) { 199 agentOptions.setJmx(jmx); 200 } 201 202 /** 203 * Creates JVM argument to launch with the specified JaCoCo agent jar and 204 * the current options 205 * 206 * @return JVM Argument to pass to new VM 207 */ 208 protected String getLaunchingArgument() { 209 return prepareAgentOptions().getVMArgument(getAgentFile()); 210 } 211 212 private AgentOptions prepareAgentOptions() { 213 if (OutputMode.file.equals(agentOptions.getOutput())) { 214 agentOptions.setDestfile(destfile.getAbsolutePath()); 215 } 216 return agentOptions; 217 } 218 219 private File getAgentFile() { 220 try { 221 File agentFile = null; 222 final String agentFileLocation = getProject().getProperty( 223 "_jacoco.agentFile"); 224 if (agentFileLocation != null) { 225 agentFile = new File(agentFileLocation); 226 } else { 227 agentFile = AgentJar.extractToTempLocation(); 228 getProject().setProperty("_jacoco.agentFile", 229 agentFile.toString()); 230 } 231 232 return agentFile; 233 } catch (final IOException e) { 234 throw new BuildException("Unable to extract agent jar", e, 235 getLocation()); 236 } 237 } 238 239 } 240