Hook IDEA Sync for override iws xml file

I use standart idea plugin like this:

apply plugin: 'java'
apply plugin: 'idea'

idea {
        project {
            languageLevel = '1.8'
            jdkName = 'JDK_1_8'
            vcs = 'Git'
        }
    
        module {
            downloadSources = true
            downloadJavadoc = true
        }
    
        workspace {
            iws {
                withXml { provider ->
                    def node = provider.asNode()
                    def runManager = node.component.find { it.'@name' == 'RunManager' }
                    def propertiesComponent = node.component.find { it.'@name' == 'PropertiesComponent' }
                    def limit = propertiesComponent.property.find { it.'@name' == 'recentsLimit' }
                    limit.'@value' = "0"   
                    def defaultTestNGConf = runManager.configuration.find {
                        it.'@default' == 'true' && it.'@type' == 'TestNG'
                    }
                    def vmParametersOption = defaultTestNGConf.option.find { it.'@name' == 'VM_PARAMETERS' }             
                    vmParametersOption.'@value' = "-ea  -DmyProps=...."
                }
            }
        }
    }

but this cofigs not created after importing project to IDEA and i should execute task: ideaWorkspace.
I know that the IDEA after importing or click to synchronizing project - calls task in bacground.Using modification iws file after sync/import - is veri important for me, I was able to hook it through build.gradle:

    apply plugin: 'java'
    apply plugin: 'idea'
    
    group 'propertie'
    version '1.0-SNAPSHOT'
    sourceCompatibility = 1.8
    
    task wrapper(type: Wrapper) {
      gradleVersion = '2.7-rc-2'
      distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile 'org.testng:testng:6.9.4'
    }

tasks.withType(GenerateIdeaWorkspace).all { ideaWorkspace ->

    def generateWs = file("idea.generate.workspace.properties")
    generateWs.withWriterAppend("UTF-8") { writer ->
        ideaWorkspace.properties.each { k ,v ->
            writer.write("$k = $v\n")
        }
    }
}

Sounds good,now we should iterate all task properties to file, because idea sync haven’t console output. Make sure property file in my root project dir is created, it indicate about task working.
After them i try override XML file, but my xml not overrided.

ideaWorkspace.workspace.iws.withXml { xmlWorkspace ->
        def node = xmlWorkspace.asNode()
        def runManager = node.component.find { it.'@name' == 'RunManager' }
        def defaultTestNGConf = runManager.configuration.find {
            it.'@default' == 'true' && it.'@type' == 'TestNG'
        }
        def vmParametersOption = defaultTestNGConf.option.find { it.'@name' == 'VM_PARAMETERS' }
        vmParametersOption.'@value' = "-ea " + "Parameter overrided!"
    }
}

any ideas?

I think found solution, but it is very ugly, anyone help me for simplifying?

    apply plugin: 'idea'
        ...   
    tasks.withType(GeneratorTask).all { GeneratorTask generatorTask ->
        String name = generatorTask.name
        if (name.contentEquals("ideaWorkspace")) {
            File inputFile = generatorTask.getInputFile();
            if (inputFile != null && inputFile.exists()) {
                try {
                    domainObject = generatorTask.generator.read(inputFile);
                } catch (RuntimeException e) {
                    throw new GradleException(String.format("Cannot parse file '%s'.\n"
                            + "       Perhaps this file was tinkered with? In that case try delete this file and then retry.",
                            inputFile), e);
                }
            } else {
                domainObject = generatorTask.defaultInstance()
            }
            generatorTask.beforeConfigured.execute(domainObject)
            generatorTask.configure(domainObject)
            generatorTask.afterConfigured.execute(domainObject)
            def node = domainObject.properties.xml
            def runManager = node.component.find { it.'@name' == 'RunManager' }
            def defaultTestNGConf = runManager.configuration.find {
                it.'@default' == 'true' && it.'@type' == 'TestNG'
            }
            def vmParametersOption = defaultTestNGConf.option.find { it.'@name' == 'VM_PARAMETERS' }
            vmParametersOption.'@value' = "-ea -DmyProperty="
            domainObject.properties.xml = node
            generatorTask.generator.write(domainObject, generatorTask.getOutputFile());
            generatorTask.didWork = true
        }
}