2

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

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212

2 Answers2

2

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%')
    }
}
Jeff Gaer
  • 134
  • 1
  • 11
  • hmmmm, different version of gradle/groovy then. The $ seems to cause problems and I tried escaping with \ but that didn't work and tried single quotes..... error:Could not get unknown property 'APP_HOME' for task ':mycoolness-prod:startScripts' of type org.gradle.api.tasks.application.CreateStartScripts. – Dean Hiller Sep 03 '16 at 03:13
  • I like the idea though!!! It blew away my logging option as well so I think I may try just sticking USERDIR and replace that with APP_HOME as soon as I figure out how to escape $ properly – Dean Hiller Sep 03 '16 at 03:20
  • awesome!!! I got there with your answer...I slip in a token in the args that I then swap out which worked. THANKS!!!! – Dean Hiller Sep 03 '16 at 03:43
1

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%"')
}
}
Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
  • original solution broke because logback does new File("config/logback.xml") and that does not resolve vs. user.dir :( so need to prefix APP_HOME on that since logback lookup ignores the user.dir property :(. – Dean Hiller Sep 03 '16 at 20:37
  • One more note is that inside the logback file ${user.dir} must be used as well to reference the application directory correctly(pretty annoying this is all not from user.dir in the first place) – Dean Hiller Sep 03 '16 at 20:45
  • 1
    Dean, thanks for your answer. It worked for me with distribution tasks, but for `run` task I got a error about `JVM_ARGS_TRICK`, so I changed `JVM_ARGS_TRICK` to `-Ddummy`. Now it works for both distribution and run. – Anton Nov 26 '17 at 09:32