1

I have a Java project with Gradle. Also I use Groovy to generate some class that will be used in Java code. Gradle executes script in separate task below:

task generateClass(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'generatorScript'
}

If I run this task, it first starts Java compilation, and only after that executes script. So if compilation failed, my generator script will not be executed. As it was mentioned, script generates one class, which my Java code is actually depends on, so, if not generated, Java will not be compiled. Vicious circle.

Script itself does not depend on some Java classes and is placed in separate sources directory:

/src
   /main
      /java
          /...(java classes)
      /groovy
          generatorScript.groovy

It seems like nothing interferes me to execute script separately and independently from Java compilation.

How can I achieve that?

2
  • Maybe it's possible to specify classpath manually and don't include Java classes in there (but what does it must contain?). In this case, Java compilation will be executed before Groovy anyway, but will not touch project classes. Commented Oct 17, 2018 at 7:46
  • if you have groovy installed just run it with system command groovy generatorScript.groovy. or with java java -cp groovy-all-x.x.x.jar groovy.ui.GroovyMain generatorScript.groovy Commented Oct 17, 2018 at 8:45

1 Answer 1

2

The problem is that you have the generator groovy script in the main sourcesets, and you try to compile this groovy script to use it as classpath for your JavaExec task. This is why compileJava task is executed, I guess.

You could do in another way, using groovy.ui.GroovyMain to execute your script, with following solution based on this link

configurations {
    // a dedicated Configuration for Groovy classpath
    groovyScript
}

dependencies {
    // Set Groovy dependency so groovy.ui.GroovyMain can be found.
    groovyScript localGroovy()
}

task generateClass(type: JavaExec) {

    // Set class path used for running Groovy command line.
    classpath = configurations.groovyScript

    // Main class that runs the Groovy command line.
    main = 'groovy.ui.GroovyMain'

    // Pass your groovy script as argument of the GroovyMain main
    // (can be improved)
    args 'src/main/groovy/generatorScript.groovy'

}
Sign up to request clarification or add additional context in comments.

1 Comment

It works, thank you! It's strange that I have already tried this way, but for some reason unsuccessfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.