AgentTask.java

    1/*******************************************************************************
    2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
    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 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.ant;
   14
   15import org.apache.tools.ant.BuildException;
   16
   17/**
   18 * Ant task that will unpack the coverage agent jar and generate the JVM options
   19 * required to use it
   20 * 
   21 * @ant.task category="java"
   22 * @author Brock Janiczak
   23 * @version $Revision: $
   24 */
   25public class AgentTask extends AbstractCoverageTask {
   26
   27    private String property;
   28
   29    /**
   30     * Sets the name of the property to hold the agent JVM options
   31     * 
   32     * @ant.required
   33     * @param property
   34     *            Name of the property to be populated
   35     */
   36    public void setProperty(final String property) {
   37        this.property = property;
   38    }
   39
   40    /**
   41     * Gets the name of the property to hold the agent JVM options
   42     * 
   43     * @return Name of the property to be populated
   44     */
   45    public String getProperty() {
   46        return property;
   47    }
   48
   49    /**
   50     * Unpacks a private copy of the JaCoCo agent and populates
   51     * <code>property</code> with the JVM arguments required to use it. The
   52     * value set into the property is only valid for the lifetime of the current
   53     * JVM. The agent jar will be removed on termination of the JVM.
   54     */
   55    @Override
   56    public void execute() throws BuildException {
   57        if (property == null || property.length() == 0) {
   58            throw new BuildException("Property is mandatory");
   59        }
   60        final JvmArgumentHelper jvmArgumentHelper = new JvmArgumentHelper();
   61        getProject().setNewProperty(property,
   62                jvmArgumentHelper.createJavaAgentParam(getAgentOptions()));
   63    }
   64}