In the startup scripts generated by the gradle application plugin, how can I pass the program name to the application?

Performing the gradle task of the application plugin installDistcreates a directory build/install/my-application-name/binthat contains shell scripts, my-application-nameand my-application-name.bat. Running any of these scripts launches the application, and the arguments passed to these scripts are passed to the main application.

In UNIX shell scripts, you can access the name that was used to execute the program, like $0. In fact, the UNIX version of the gradle-generated startup script uses it several times $0.

How to configure the gradle application plugin so that these scripts pass the value $0(and everything that is equivalent to Windows on Windows) to the base application, possibly as a Java system property?

+4
source share
2 answers

Since the parameter for obtaining the name of the running script in Linux ( $0) and in Windows ( %0) is used differently, the easiest way to create your own scripts is to use separate custom templates for the corresponding run script generators :

startScripts {
  unixStartScriptGenerator.template = resources.text.fromFile('unixStartScript.txt')
  windowsStartScriptGenerator.template = resources.text.fromFile('windowsStartScript.txt')
}

Default templates are easy to get a call, for example. unixStartScriptGenerator.template.asString()

Documentation for setting up startup scripts can be found here .

+3
source

, , jihor. , - :

startScripts {
    def gen = unixStartScriptGenerator
    gen.template = resources.text.fromString(
        gen.template.asString().replaceFirst('(?=\nDEFAULT_JVM_OPTS=.*?\n)') {
            '\nJAVA_OPTS="\\$JAVA_OPTS "\'"-Dprogname=\\$0"\''
        })

    // TODO: do something similar for windowsStartScriptGenerator
}

replace replaceFirst, . , lookahead, , . ( groovy replaceFirst, , , . , , .)

, :

JAVA_OPTS="$JAVA_OPTS -Dprogname=$0"

- :

JAVA_OPTS="$JAVA_OPTS "'"-Dprogname=$0"'

, $0 (, ), script $JAVA_OPTS eval set --.

( - , Windows, , .)

0

Source: https://habr.com/ru/post/1692319/


All Articles