Reputation: 1986
How can I change the location of the jacoco exec
file to be out of project directory, from a custom gradle plugin. I have below code in my plugin :
project.afterEvaluate {
def testTasks = project.tasks.withType(Test)
testTasks.each { task ->
task.jacoco.destinationFile = file("/home/skgupta/jacoco/${task.name}.exec")
}
}
But when I run the test, the .exec
file is generated under <projectDir>/jacoco/<taskName>.exec
When I change the value of destinationFile
directory in my build.gradle
, it works perfectly fine.
What am I missing here ?
EDIT: Here the complete code for plugin
class TestInfraPlugin implements Plugin<Project> {
Project project
void apply(Project target) {
this.project = target
logger = target.logger
configureConfigurations(target)
configureTestNG()
configureCodeCoverage(target)
}
public void configureTestNG() {
logger.debug "Configuring the EMTest task with default values."
project.afterEvaluate {
project.ext.testClassesDir = new File(project.properties['emdi.T_WORK'] + '/testClasses')
/* Create one task of type ExtractConfiguration, to extract the lrgConfig configuration to $T_WORK/testClasses
* The extraction of the test jar before the tests are run is mandatory because the 'Test' task
* ONLY works on the files in directory and NOT on the jars. Thus we extract the jar and point the testClasses
* property to the location where the jar is extracted.
*
* NOTE:
* 1. This is only one task per gradle.
* 2. All the jars in the lrgConfig are extracted before any testblock is run.
* 3. The LRG will not run if user uses other configuration than 'lrgConfig'
* (which applies without cutover to 'Test' task also.)
*/
def testTasks = project.tasks.withType(EMTest)
/* Silently log and return if there are no EMTest tasks.
* This case would be hit :
* a) When testinfraPlugin is applied to build.gradle
* b) (in Future)if <lrg>.gradle has test-blocks of non-Java types.
*/
if (testTasks != null && testTasks.size() == 0) {
logger.info "There are no tasks of type EMTest."
return
}
def extractTask = project.tasks.findByPath('extractTestClasses') ?:
project.task('extractTestClasses', type: ExtractConfiguration) {
configuration = project.configurations.testConfig
to = project.testClassesDir
}
/*
* 1. Adding the 'extractTask' to all EMTest, to ensure that 'extractTask' is run before any 'EMTest'.
* 2. For lazy evaluation of lrgConfig, we are NOT running the task here, but just adding as dependent task.
*/
testTasks.each { task ->
logger.debug "Adding dependsOn extractTask for task: ${task.name}"
task.dependsOn extractTask
/* TODO: The code below is to change the location of the jacoco file, but it is not working
* When the same is tried directly from build.gradle (tried as fresh project), it works.
* I have opened SO question as: http://stackoverflow.com/questions/28039011/create-jacoco-exec-file-out-of-gradle-project-dir
* If we have solution for this, we can get-away with the workaround we applied in the TestResultsHandler.groovy
*
* NOTE: I tried with beforeEvaluate, and it really does not work because EMTest tasks are not evaluated by then.
* I also tried with 'java.io.File', etc other options. Nothing works.
*
*/
task.jacoco.destinationFile = task.project.file( '/scratch/skgupta/git_storage/emdi/work/jacocobuild/' + task.name + '.exec')
println "Value of destinationFile in jacoco extension is "+ task.jacoco.getDestinationFile()
}
}
logger.debug "Applying plugins implicitly."
applyPlugin('java')
/* Apply jacoco plugin */
applyPlugin('jacoco')
}
/**
* Apply plugin with given name.
*
* @param pluginName - The name of the plugin to apply.
*
*/
protected void applyPlugin(String pluginName) {
logger.debug "Checking if the plugin, ${pluginName} is already applied."
if (project.plugins.hasPlugin(pluginName)) {
logger.debug "Plugin is already applied."
return
}
logger.debug "Applying plugin, ${pluginName}"
project.apply plugin: pluginName
logger.debug "Verifying if the plugin is applied."
if (project.plugins.hasPlugin(pluginName)) {
logger.debug "Successfully applied plugin, ${pluginName}"
}
else {
logger.error "Failed to apply plugin, ${pluginName}"
}
}
/**
* Add the configuration, lrgConfig, to the current project.
*
* The configuration lrgConfig defines the dependencies for the tools like testng, restassured, etc.
* @param project - the project to which the config will be added.
*/
protected void configureConfigurations(Project project) {
logger.debug "Creating the configurations to hold tools GAV"
project.configurations {
/* This will be empty configuration, and will be populated by lrg owner with gav of the lrg jar */
testConfig
/* This will be empty configuration, and will be populated by lrg owner during test jar build */
lrgConfig
/* Configuration to hold the gav of the testng jar(s) */
testNG
/* Configuration to hold the GAV of the webdriver jar(s) */
webdriver
/* Configuration to hold the GAV of the restAssured jar(s) */
restAssured
}
project.dependencies {
/*
* Currently adding opensource testng jar path here. This will be replaced by
* GAV of the tools testng only jar once they decouple it.
* Bug 19276096 - SPLIT OUT TESTNG JAR FROM EM-QATOOL-UIFWK-WEBDRIVER.JAR|
*/
testNG group: 'com.mycomp.myprod.emdi', name: 'em-qatool-uifwk-webdriver',version: '1.0.0', transitive: false
/*
* webdriver config would contain reference to the tools webdriver jar, which currently
* also has testng fwk in it. This will be replaced by gav of the webdriver only jar once
* it is decoupled
* Bug 19276096 - SPLIT OUT TESTNG JAR FROM EM-QATOOL-UIFWK-WEBDRIVER.JAR|
*/
webdriver group: 'com.mycomp.myprod.emdi', name: 'em-qatool-uifwk-webdriver',version: '1.0.0', transitive: false
/* Add rest-assured libraries */
restAssured group: 'org.codehaus.groovy', name:'groovy-all', version:'2.2.1'
restAssured group: 'com.jayway.restassured', name:'json-path', version:'1.8.1'
restAssured group: 'org.apache.commons', name:'commons-lang3', version:'3.1'
restAssured group: 'org.hamcrest', name:'hamcrest-core', version: '1.3'
restAssured group: 'org.apache.httpcomponents', name:'httpclient', version:'4.3.1'
restAssured group: 'org.apache.httpcomponents', name:'httpcore', version:'4.3'
restAssured group: 'org.apache.httpcomponents', name:'httpmime', version:'4.3.1'
restAssured group: 'com.jayway.restassured', name:'xml-path', version:'1.9.0'
restAssured group: 'com.jayway.restassured', name:'rest-assured-common', version:'2.3.1'
restAssured group: 'com.jayway.restassured', name:'rest-assured', version:'2.3.1'
}
/*
* Currently the QA's build.gradle use lrgConfig for compilation.
* Hence setting lrgConfig as well with the testng gav for backward compatibility
* TODO: This must be removed in future versions of testinfra
*
*/
project.configurations.lrgConfig.extendsFrom(project.configurations.testNG, project.configurations.restAssured)
}
}
Upvotes: 1
Views: 2753
Reputation: 17336
Depending upon the Gradle version, you might have to use different parameters.
See if the following "test" (and it's jacoco) section helps. The following code is available in one of the common $GRADLE_HOME/init.d/some.global.common.gradle file so that I don't have to add the same code in every project.
test {
maxParallelForks = 5
forkEvery = 50
ignoreFailures = true
testReportDir = file("$buildDir/reports/tests/UT")
testResultsDir = file("$buildDir/test-results/UT")
//testLogging.showStandardStreams = true
//onOutput { descriptor, event ->
// logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
//}
//Following Jacoco test section is required only in Jenkins instance extra common file
jacoco {
//The following vars works ONLY with 1.6 of Gradle
destPath = file("$buildDir/jacoco/UT/jacocoUT.exec")
classDumpPath = file("$buildDir/jacoco/UT/classpathdumps")
//Following vars works only with versions >= 1.7 version of Gradle
//destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
// classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
}
}
task integrationTest( type: Test) {
//Always run tests
outputs.upToDateWhen { false }
ignoreFailures = true
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
testReportDir = file("$buildDir/reports/tests/IT")
testResultsDir = file("$buildDir/test-results/IT")
//Following Jacoco test section is required only in Jenkins instance extra common file
//jacoco {
//This works with 1.6
// destPath = file("$buildDir/jacoco/IT/jacocoIT.exec")
// classDumpPath = file("$buildDir/jacoco/IT/classpathdumps")
//Following works only with versions >= 1.7 version of Gradle
//destinationFile = file("$buildDir/jacoco/IT/jacocoIT.exec")
// classDumpFile = file("$buildDir/jacoco/IT/classpathdumps")
//}
}
OR
try putting your above code in the following block (i.e. replace lines with // with yours):
project.gradle.taskGraph.whenReady {
//integrationTest {
// ignoreFailures = true
//}
}
Upvotes: 2