How do I start a test server on a different process?

(Tiago Veloso) #1

I am trying to get gradle to start a server on a different process. I have the following task, but it is not working, also this is not OS independent. Is there a way to execute something like this on a different process in a way that os OS independent?

task startServer(type: Exec) {

workingDir file("${buildDir}/aux/server/server-S31-SNAPSHOT")

ignoreExitValue true

def cmd = ‘nohup java -client -d64 -Xms256M -Xmx512M -DisableExplicitGC -XX:MaxPermSize=128M -jar server-S31-SNAPSHOT.jar &’.split()

commandLine cmd }

(Luke Daley) #2

Try the JavaExec task.

(Tiago Veloso) #3

How would I execute a jar using the JavaExec?

(Tiago Veloso) #4

How would I execute a jar using the JavaExec?

(Luke Daley) #5

Much the same way you did with the Exec task. The API is just slightly different because it provides stronger API for Java process creation.

(Tiago Veloso) #6

I have the following task: 'task startServer(type: JavaExec) {

workingDir file("${buildDir}/aux/server/server-S31-SNAPSHOT")

ignoreExitValue true

def args = ‘-client -d64 -Xms256M -Xmx512M -DisableExplicitGC -XX:MaxPermSize=128M -jar’.split(’ ')

allJvmArgs = args

main = ‘server-S31-SNAPSHOT.jar’ }’

But I get the following error when running: > Cannot cast object ‘[Ljava.lang.String;@33571678’ with class ‘[Ljava.lang.String;’ to class ‘java.lang.Iterable’ due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.lang.Iterable(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)

Am I not spliting the arg string correctly?

(Luke Daley) #7

args is an Iterable. split() returns a String[] which is not Iterable.

Also, the main property is the main class. You need to put this jar name on the args list, not as the ‘main’ property.

(Tiago Veloso) #8

How do I get an Iterable then? I am using ‘tokenize()’ now, and still no success.

'task startServer(type: JavaExec) {

workingDir = file("${buildDir}/aux/server/netmedepro-test-server-S31-SNAPSHOT")

allJvmArgs = “-client -d64 -Xms256M -Xmx512M -DisableExplicitGC -XX:MaxPermSize=128M”.tokenize()

main = ‘-jar’

args = ‘netmedepro-test-server-S31-SNAPSHOT.jar’ }’

This is the error I get now:

java.lang.UnsupportedOperationException (no error message)

(Luke Daley) #9

Call toList() on the String[] or better yet use the jvmArgs(Object…) method and avoid using one long string.

(Tiago Veloso) #10

Ok I got it to work, but this didn’t solve my initial problem. The server is started but it is still in the same process, i.e. the server process stalls gradle while waiting for calls. Basically my script is stalled in my startServer task. I want it to start the server and perhaps delay the script a little (while the server starts) and continue to other tasks. Ans this is not happening.

(Luke Daley) #11

It is a different process. Gradle is just blocking while the other process runs. If you want non blocking behaviour you’ll have to write your own task and use Java’s ProcessBuilder API. Gradle doesn’t have anything above this for async process management.