How to pass custom arguments to TestNG listeners in gradle?

I am running my TestNG test suite via gradle clean test and have my build.gradle configured as below to generate TestNG XML reports:

test {
   useTestNG() {
     useDefaultListeners = true

I would like to use a library which adds additional attributes to produced TestNG XML reports and suggest to use custom task to achieve this because apparently gradle does not allow us to set attributes on the XMLReporter used by TestNG.

task testngTest(type: JavaExec, dependsOn: [classes]) {
    description 'Run TestNG tests'
    mainClass = 'org.testng.TestNG'
    args('testng.xml', '-reporter',
    classpath = sourceSets.test.runtimeClasspath

I would like to ask whether we can still achieve the above functionality using test task provided by gradle itself? I do not want to create a custom task for this purpose and run my tests via gradle clean compileJava testngTest. What I want is to pass this option to TestNG from the test task itself and run my tests as usual with gradle clean test

I also checked out the documentation of gradle API, and the useTestNG options but I could not figure out the way to pass this commandline argument -reporter org.testng.reporters.XMLReporter:generateTestResultAttributes=true,generateGroupsAttribute=true to TestNG.

In Maven, there is the possibiliy to achieve this using maven surefire plugin as follows:








Could you please let me know whether this is at all possible to achieve using Gradle test task and useTestNG()?