Sunday, December 12, 2010

Complement of mrhaki's "Run Java Application From Build Script"

If you want to know how to run java app in build.gradle, you should check mrhaki's post: Run Java Application From Build Script. That post gave me big help on this task. In this post, I will give a complement on a topic that missed in that post.
In my app, I have some code that needs data from command line, such as:
    def input= new DataInputStream(System.in)
println "Work Dir:"
def rootDir= input.readLine()
println "Test Plan:"
def testplan= input.readLine()
When I applied the method I learned from mrhaki's post, I could run my app, but have no chance to input some data from command line. The app exited with an exception because it did not pause and got null from command line! After digging the gradle api doc, I found I could set the value of standardInput property of JavaExec task. So I made some change to my build.gradle:
task (processplan, dependsOn: classes, type: JavaExec) {
main= '....'
classpath= sourceSets.main.runtimeClasspath
standardInput= System.in
}
Note that the last line. I set the value of standardInput to "System.in". Then I ran the build task again. This time, the app stopped to wait for user's inputting. And app worked fine!
So, If your app needs user to input some data from command line, you'd better set the value of standardInput. Hope this tip is help for you.

No comments: