How to generate a Code using Groovy and then compile again using Gradle


(GP) #1

Hi Gradle Team,

I’m a newbie in Gradle. I have written a Groovy script that in turn will generate a bunch of more Groovy Scripts. After completing the auto-generation of Groovy scripts, now I would like to compile the newly generated code and package it as a Jar. I’m a bit stuck with this process.

The Gradle script looks as below:
generator.gradle

group 'com.test'
version '1.0'

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.5'

    // logback dependencies
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.22'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.10'
}

task runScript(type: JavaExec) {
    description 'Run Groovy script'
    main = 'com.app.cif.load.test.ServiceEngine'
    classpath = sourceSets.main.runtimeClasspath
}

build.gradle looks like this:

group 'com.test'
version '1.0'

apply plugin: 'groovy'
apply from: 'generator.gradle'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.5'

    // logback dependencies
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.22'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.10'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

When I run
gradle -q runScript
the groovy class com.app.cif.load.test.ServiceEngine generates all the needed dynamic groovy scripts

then I run
gradle build
to create the final jar

I would like to club these two tasks together. But I’m not able. Can someone please suggest me the right approach to follow here?