Customize the generated start scripts: add command-line parameters


(Alex Sko) #1

it would be nice to have a setting for startScripts task to add some parameters at the END of this string:

“%JAVA_EXE%” %DEFAULT_JVM_OPTS% %JAVA_OPTS% %LAUNCHER_OPTS% -classpath “%CLASSPATH%” my.Launcher %CMD_LINE_ARGS%

e.g.

“%JAVA_EXE%” %DEFAULT_JVM_OPTS% %JAVA_OPTS% %LAUNCHER_OPTS% -classpath “%CLASSPATH%” my.Launcher %CMD_LINE_ARGS% %MY_PREDEFINED_ARGS%

so the script users could easily customize the launcher script by adding

set MY_PREDEFINED_ARGS=“something” string at the top of the launcher script.

the goal is to make it easier for not-so-technical users to customize the launcher script. I used Windows script as an example above, but the same should be for Linux as well.

I could use

doLast {

windowsScript.text = windowsScript.text.replace("…", “…”)

}

but it looks a bit fragile and complex.

or maybe there’s some better “recommended way” to achieve this?

here’s my code to alter the generated scripts. this is way too complex… maybe I should just revert back to using my custom start scripts instead:

startScripts {
  // solution from http://forums.gradle.org/gradle/topics/specifying_memory_settings_in_application_plugin_start_scripts
  // TODO: Gradle 1.7 will support JDK options configuration without this trick. need to check when it becomes available.
  ext.jvmOpts = "-Xmx512m"
  inputs.property("jvmOpts", { ext.jvmOpts }) // for incremental build to work properly
    doLast {
    def optsEnvVar = "DEFAULT_JVM_OPTS"
    unixScript.text = unixScript.text.replace("$optsEnvVar=${'""'}", "$optsEnvVar=${'"'}$ext.jvmOpts${'"'}")
    windowsScript.text = windowsScript.text.replace("set $optsEnvVar=", "set $optsEnvVar=$ext.jvmOpts")
      // add sample settings at the top of the file
    def ADD_TOP_WIN = "@REM Sample configuration settings (uncomment and edit if needed):\n" +
                      "\n@REM set PD_ARGS=--port=8887 --openPageInWebBrowser" +
                      "\n@REM set LAUNCHER_OPTS=-Dpd.config.folder=C:\pd-config \n\n\n"
    def WIN_START_MARKER = "@if \"%DEBUG%\" == \"\" @echo off"
    windowsScript.text = windowsScript.text.replace(WIN_START_MARKER, ADD_TOP_WIN + WIN_START_MARKER)
    def ADD_TOP_LIN = "# Sample configuration settings (uncomment and edit if needed):\n" +
                      "\nexport PD_ARGS=\"--port=8887 --openPageInWebBrowser\"" +
                      "\nexport LAUNCHER_OPTS=-Dpd.config.folder=/var/etc/pd-config \n\n\n"
    def LIN_START_MARKER = "#!/usr/bin/env bash"
    unixScript.text = unixScript.text.replace(LIN_START_MARKER, ADD_TOP_LIN + LIN_START_MARKER)
      // add PD_ARGS to the launcher
    def PREFIX_LIN = "com.Launcher \"\$@\""
    unixScript.text = unixScript.text.replace(PREFIX_LIN, PREFIX_LIN + " \$PD_ARGS")
    def PREFIX_WIN = "com.Launcher %CMD_LINE_ARGS%"
    windowsScript.text = windowsScript.text.replace(PREFIX_WIN, PREFIX_WIN + " %PD_ARGS%")
  }
}