How to run/execute a gradle task? - gradle

How to run this gradle task/include it in build task (https://github.com/prashant-ramcharan/courgette-jvm)
task regressionSuite(type: Test, dependsOn: testClasses) {
systemProperty('name', 'value')
include '**/RegressionTestSuite.class'
outputs.upToDateWhen { false }
}
When I do gradle clean regressionSuite is always giving me build successful.. but it's not executing the specified class. Specific file is in the path.
I'm new to gradle.. any help much appreciated!!

you need to configure also the testClassesDir and the classpath property of the test task, otherwise the class you define in your pattern can't be found nor the test can be executed:
task regressionSuite(type: Test) {
systemProperty('name', 'value')
include '**/RegressionTestSuite.class'
outputs.upToDateWhen { false }
classpath = sourceSets.test.runtimeClasspath
testClassesDir = sourceSets.test.output.classesDir
}

Related

Gradle disable task

Can I disable the task, only for one task?
For example
flywayMigrate {
doFirst {
gradle.startParameter.excludedTaskNames += "test"
// test.enabled = false
}
dependsOn flywayClean
dependsOn build
}
I want you to do flywayMigrate
The tests were turned off.
But when I run clean build tests were run also.
I think you can do it with the task graph of the build as follows:
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(flywayMigrate)) {
test.enabled = false
}
}
Here is a closure executed then the task graoh is built, just before task are configured or executed. It check's whether flywayMigrate task will be executed and if yes, it disables test task.
Here is how it's described in the official docs.

How do I get my custom gradle tasks to run?

I have a gradle project from which I want to generate two artifacts - a java jar, and a tarball of additional files.
So I added the following to my build.gradle
def serviceName = "kafka-schemas"
task packageDistribution(type: Copy) {
from "$buildDir/resources/main"
include "*.avsc"
into "$buildDir/schemas"
}
task archive(type: Tar) {
dependsOn 'packageDistribution'
compression = 'GZIP'
from("$buildDir/schemas") {
include "**/*.avsc"
}
into("${serviceName}")
}
project.tasks.findByName('build') dependsOn archive
project.tasks.findByName('build') dependsOn packageDistribution
However when I run gradle clean build it does not run my tasks.
What am I doing wrong?
Try this:
processResources.dependsOn('packageDistribution')
task packageDistribution(type: Zip) {
archiveFileName = "xxxxx.zip"
destinationDirectory = file("$buildDir/dist")
from "xxxx"
}

Running specific tests using gradle over multiple browsers

I'm using Geb/Spock for automated testing. I'm using Gradle as my build tool.
I'd like to call different gradle tasks to build and run a specific spec(test) or a suite of specs.
I dont know enough about the gradle build lifecycle to completely understand what is going on here: https://github.com/geb/geb-example-gradle/blob/master/build.gradle
plugins {
id "idea"
id "groovy"
id "com.energizedwork.webdriver-binaries" version "1.4"
id "com.energizedwork.idea-base" version "1.4"
}
ext {
// The drivers we want to use
drivers = ["firefox", "chrome", "chromeHeadless"]
ext {
groovyVersion = '2.4.12'
gebVersion = '2.2'
seleniumVersion = '3.6.0'
chromeDriverVersion = '2.32'
geckoDriverVersion = '0.18.0'
}
}
repositories {
mavenCentral()
}
dependencies {
// If using Spock, need to depend on geb-spock
testCompile "org.gebish:geb-spock:$gebVersion"
testCompile("org.spockframework:spock-core:1.1-groovy-2.4") {
exclude group: "org.codehaus.groovy"
}
testCompile "org.codehaus.groovy:groovy-all:$groovyVersion"
// If using JUnit, need to depend on geb-junit (3 or 4)
testCompile "org.gebish:geb-junit4:$gebVersion"
// Drivers
testCompile "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
testCompile "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
}
webdriverBinaries {
chromedriver chromeDriverVersion
geckodriver geckoDriverVersion
}
drivers.each { driver ->
task "${driver}Test"(type: Test) {
group JavaBasePlugin.VERIFICATION_GROUP
outputs.upToDateWhen { false } // Always run tests
systemProperty "geb.build.reportsDir", reporting.file("geb/$name")
systemProperty "geb.env", driver
}
}
test {
dependsOn drivers.collect { tasks["${it}Test"] }
enabled = false
}
tasks.withType(Test) {
maxHeapSize = "1g"
jvmArgs '-XX:MaxMetaspaceSize=128m'
testLogging {
exceptionFormat = 'full'
}
}
tasks.withType(GroovyCompile) {
groovyOptions.forkOptions.memoryMaximumSize = '256m'
}
I've tried inserting the following into build.gradle:
task dataGen {
include '**com.company.project.spec.util/DataGenerationUtilSpec.groovy'
}
task sanity {
include '**com.company.project.spec.sanity.*'
}
But calling these tasks (gradle sanity) results in a build failure:
Could not find method include() for arguments [**com.company.project.spec.util/DataGenerationUtilSpec.groovy] on task ':dataGen' of type org.gradle.api.DefaultTask
Obviously there's existing build instructions since I can call gradle build and all the specs run on Chrome, I'm just not sure how to add more tasks
I think these 2 tasks are test tasks so it should look like that :
task dataGen (type: Test) {
include '**com.company.project.spec.util/DataGenerationUtilSpec.groovy'
}
task sanity (type: Test) {
include '**com.company.project.spec.sanity.*'
}
You can use Spock annotation to control the test or the Spec, see example here.
You will have to define annotation classes and define the Spock config file to use that annotation. You then annotate the specific Specification (or test).
Now you will have to define the Spock config file in the task or from a parameter.

Exclude a module or dir or subtask from gradle configuration and execution

I have a task with name test and code is as below :
tasks {
"test"(Test::class) {
useJUnitPlatform {
excludeTags = setOf("e2e", "integration")
}
}
When execute this task with gradle command ./gradlew test --info, gradle is scanning all the modules of my project and generating a configuration with has some tasks from module named data-export-ui-kjs.
I want gradle to exclude the tasks from data-export-ui-kjs module while configuring & executing test task. I have used below code to achieve this but its not successful
gradle.taskGraph.whenReady {
TaskExecutionGraphListener { graph ->
if(it.name.contains("data-export-ui-kjs")) {
it.enabled = false
}
}
}
Kindly help me to get this done. Thanks in advance
Working as expected. Modified the code. Its syntax issue itseems

How to require system property only if task is executed?

I have a gradle script with a custom test task:
task integrationTest(type:Test) {
jvmArgs '-DmyParam=' + System.getProperty('myParam')
}
I want to fail the build with a nice message if the property is missing, but only if my task will be executed. I tried adding
task integrationTest(type:Test) {
if (!System.hasProperty('myParam')) throw new InvalidUserDataException("Missing property myParam")
jvmArgs '-DmyParam=' + System.getProperty('myParam')
}
but this will fail even if the integrationTest task isn't called.
How do I check this only when executing the task, instead of on gradle startup?
You can simply use a doFirst closure. The closure will be executed before the task action, so sufficiently early to modify the task, but not in configuration phase, but during execution phase, so, only if the task is executed:
task integrationTest(type:Test) {
doFirst {
jvmArgs '-DmyParam=' + System.getProperty('myParam')
}
}
Please note, that you can use the systemProperty method to define system properties:
systemProperty 'myParam', System.getProperty('myParam')
// instead of
jvmArgs '-DmyParam=' + System.getProperty('myParam')
A colleague helped me find the answer. Moving the exception to a doFirst block will produce the desired result:
task integrationTest(type:Test) {
doFirst {
if (!System.hasProperty('myParam')) throw new InvalidUserDataException("Missing property myParam")
}
jvmArgs '-DmyParam=' + System.getProperty('myParam')
}
or, if you need to fail the build before any task is executed, you can check if the task will be called:
project.gradle.taskGraph.whenReady { 
  if(project.gradle.taskGraph.hasTask(":integrationTest")) {
    if (!System.hasProperty('myParam')) throw new InvalidUserDataException("Missing property myParam")
   }
}

Resources