1 /******************************************************************************* 2 * Copyright (c) 2009, 2019 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 * 11 *******************************************************************************/ 12 package org.jacoco.maven; 13 14 import java.io.File; 15 16 import org.apache.maven.plugins.annotations.LifecyclePhase; 17 import org.apache.maven.plugins.annotations.Mojo; 18 import org.apache.maven.plugins.annotations.Parameter; 19 import org.apache.maven.plugins.annotations.ResolutionScope; 20 21 /** 22 * <p> 23 * Prepares a property pointing to the JaCoCo runtime agent that can be passed 24 * as a VM argument to the application under test. Depending on the project 25 * packaging type by default a property with the following name is set: 26 * </p> 27 * 28 * <ul> 29 * <li>tycho.testArgLine for packaging type eclipse-test-plugin and</li> 30 * <li>argLine otherwise.</li> 31 * </ul> 32 * 33 * <p> 34 * If your project already defines VM arguments for test execution, be sure that 35 * they will include property defined by JaCoCo. 36 * </p> 37 * 38 * <p> 39 * One of the ways to do this in case of maven-surefire-plugin - is to use 40 * syntax for <a href="http://maven.apache.org/surefire/maven-surefire-plugin/faq.html#late-property-evaluation">late property evaluation</a>: 41 * </p> 42 * 43 * <pre> 44 * <plugin> 45 * <groupId>org.apache.maven.plugins</groupId> 46 * <artifactId>maven-surefire-plugin</artifactId> 47 * <configuration> 48 * <argLine>@{argLine} -your -extra -arguments</argLine> 49 * </configuration> 50 * </plugin> 51 * </pre> 52 * 53 * <p> 54 * You can define empty property to avoid JVM startup error <code>Could not find or load main class @{argLine}</code> 55 * when using late property evaluation and jacoco-maven-plugin not executed. 56 * </p> 57 * 58 * <p> 59 * Another way is to define "argLine" as a Maven property rather than 60 * as part of the configuration of maven-surefire-plugin: 61 * </p> 62 * 63 * <pre> 64 * <properties> 65 * <argLine>-your -extra -arguments</argLine> 66 * </properties> 67 * ... 68 * <plugin> 69 * <groupId>org.apache.maven.plugins</groupId> 70 * <artifactId>maven-surefire-plugin</artifactId> 71 * <configuration> 72 * <!-- no argLine here --> 73 * </configuration> 74 * </plugin> 75 * </pre> 76 * 77 * <p> 78 * Resulting coverage information is collected during execution and by default 79 * written to a file when the process terminates. 80 * </p> 81 * 82 * @since 0.5.3 83 */ 84 @Mojo(name = "prepare-agent", defaultPhase = LifecyclePhase.INITIALIZE, requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true) 85 public class AgentMojo extends AbstractAgentMojo { 86 87 /** 88 * Path to the output file for execution data. 89 */ 90 @Parameter(property = "jacoco.destFile", defaultValue = "${project.build.directory}/jacoco.exec") 91 private File destFile; 92 93 /** 94 * @return the destFile 95 */ 96 @Override getDestFile()97 File getDestFile() { 98 return destFile; 99 } 100 101 } 102