How to configure gradle application plugin to set

2019-05-19 06:08发布

gradle application plugin rocks and creates the following directory structure

app bin - scripts to start the server lib - jar files {anything else from src/dist just goes in app}

The thing I can't figure out is the scripts do not make the server run in the app directory which is annoying. Is there a way so that it can be configured and will run in the app directory ? (or a way to make it set the user.dir to .. relative from the bin directory).

This is quite frustrating since I have to do some sort of check and error out saying the program must be run from app diretory so then I know how to lookup files.

Right now, if you have a property -Dlogback.configurationFile=config/logback.xml

and you run the start script from anywhere on the machine other than the app directory, logging silently stops working.

thanks, Dean

2条回答
劫难
2楼-- · 2019-05-19 06:17

Based on Jeff Gaer's answer, I got to the following answer on my version of gradle/groovy. gradle version 2.14-rc-1 groovy 2.4.4

I only tested on mac.

BIG NOTE: The bash one has '"args"' in the script which is annoying while windows only had "args" so the replaces I had to do were a little different. I marked his answer as correct since it got me to the answer.

applicationDefaultJvmArgs = ['JVM_ARGS_TRICK']

//Here, we must modify the start scripts to set user.dir property so no matter where the script is run from,
//the user.dir will point to APPNAME directory
CreateStartScripts startScripts = project.startScripts
startScripts.with {
doLast {
    //A slash in groovy allows us to not escape double quotes AND single quotes
    def slashString = /'"JVM_ARGS_TRICK"'/
    //for some reason, gradle prints '"jvm args"'  why?  I don't know but must swap quote and double quote
    unixScript.text = unixScript.text.replace(slashString,
               '"-Dlogback.configurationFile=$APP_HOME/config/logback.xml -Duser.dir=$APP_HOME"')
    windowsScript.text = windowsScript.text.replace('"JVM_ARGS_TRICK"',
               '"-Dlogback.configurationFile=%APP_HOME%/config/logback.xml -Duser.dir=%APP_HOME%"')
}
}
查看更多
女痞
3楼-- · 2019-05-19 06:37

The following code worked for me ( only tested on linux , not sure about the windows section(

// set user.dir to the distribution directory so that the config directory can be located
CreateStartScripts startScripts = project.startScripts
startScripts.with {
    doLast {
        unixScript.text = unixScript.text.replaceFirst('(?<=DEFAULT_JVM_OPTS=)((\'|\")(.*)(\'|"))(?=\n)',
                 "-Duser.dir=$APP_HOME")
        windowsScript.text = windowsScript.text.replaceFirst('(?<=DEFAULT_JVM_OPTS=)(.*)(?=\r\n)',
                '-Duser.dir=%APP_HOME%')
    }
}
查看更多
登录 后发表回答