hl037_
hl037_

Reputation: 3907

Best way to add a code generation step in the build process?

I have a python script that modifies a part of a java class (add static fields) according to another file.

What is the best way to add it in the Gradle build process (My script requires pyton3 (so it would be better to tell the user if he doesn't have it) and need to be executed before the java compilation) ?

Upvotes: 2

Views: 109

Answers (1)

RaGe
RaGe

Reputation: 23805

I'm not certain what the best way to run python from gradle is, but one candidate is to use the exec task:

task runPython(type:Exec) {
   workingDir 'path_to_script'
   commandLine /*'cmd', '/c'*/ 'python', 'my_script.py'
}

You might have to add cmd and /c on windows. You can also parse/assert on stdout for success conditions. See here for more about Exec.

As for ensuring that this task always runs before compileJava, you have to add this task as a dependsOn for the compileJava task:

compileJava.dependsOn runPython

alternate syntax:

compileJava{
    dependsOn runPython
}

When compileJava dependsOn runPython, runPython is executed first everytime compileJava is invoked.

Upvotes: 2

Related Questions