如何从 Groovy 的 CliBuilder 中获取 运行 命令(或 jar,或脚本)的名称?
How can I get the name of the running Command (or jar, or script) from Groovy's CliBuilder?
我正在使用 Groovy 的 CliBuilder 来解析命令行参数并在命令行不正确时生成用法消息。作为使用消息的一部分,我希望能够打印所用程序的实际名称(因为版本号随每次构建而变化)。如何获取所使用程序的名称?
我希望能够做类似的事情:
def cli = new CliBuilder(usage: "java -jar ${this.name} -db DBCONNECTIONFILE")
但我似乎只能得到程序名后面的参数。
也许我可以从 pom 中获取它,但我不明白除了读取和解析 pom 之外如何从 pom 文件中获取它。
听起来您想获取当前正在执行的 jar 的名称。 How to get the path of a running JAR file?
中对此进行了介绍
new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
然后调用 .getName()。如果你只想要路径,那么你可以使用
MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()
我正在使用 Groovy 的 CliBuilder 来解析命令行参数并在命令行不正确时生成用法消息。作为使用消息的一部分,我希望能够打印所用程序的实际名称(因为版本号随每次构建而变化)。如何获取所使用程序的名称?
我希望能够做类似的事情:
def cli = new CliBuilder(usage: "java -jar ${this.name} -db DBCONNECTIONFILE")
但我似乎只能得到程序名后面的参数。
也许我可以从 pom 中获取它,但我不明白除了读取和解析 pom 之外如何从 pom 文件中获取它。
听起来您想获取当前正在执行的 jar 的名称。 How to get the path of a running JAR file?
中对此进行了介绍new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
然后调用 .getName()。如果你只想要路径,那么你可以使用
MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()