使用 Spring Boot LaunchScript 时如何获取当前工作目录

How do I get the current working directory when using a Spring Boot LaunchScript

没有not this one

    var cwd = Path.of( "" );
    log.info( "user.dir: {}", Path.of( System.getProperty( "user.dir" ) ) );
    log.info( "cwd: {}", cwd.toAbsolutePath() );
    log.info( "PWD: {}", System.getenv( "PWD" ) );
❯ ./modules/app/build/libs/brix-0.1.0.jar --logging.level.com.xenoterracide.brix=info java module foo                                      # brix -> ccushing/release !
INFO  - Starting Application using Java 11.0.11 on CarbonX1-8-Manjaro with PID 51641 (/home/xeno/IdeaProjects/brix/modules/app/build/libs/brix-0.1.0.jar started by xeno in /home/xeno/IdeaProjects/brix/modules/app/build/libs) - com.xenoterracide.brix.Application
INFO  - No active profile set, falling back to default profiles: default - com.xenoterracide.brix.Application
INFO  - Started Application in 0.843 seconds (JVM running for 1.633) - com.xenoterracide.brix.Application
INFO  - user.dir: /home/xeno/IdeaProjects/brix/modules/app/build/libs - com.xenoterracide.brix.configloader.service.ConfigValueProcessor
INFO  - cwd: /home/xeno/IdeaProjects/brix/modules/app/build/libs - com.xenoterracide.brix.configloader.service.ConfigValueProcessor
INFO  - PWD: /home/xeno/IdeaProjects/brix/modules/app/build/libs - com.xenoterracide.brix.configloader.service.ConfigValueProcessor
Overwrite [yN] module/src/test/java/com/xenoterracide/TestApplication.java 
❯ pwd                                                                                                                                      # brix -> ccushing/release !
/home/xeno/IdeaProjects/brix ####!!!!! THIS ONE

^ 最后一个,以跨平台兼容的方式。

我正在使用 Spring 启动 jar 启动脚本。

tasks.withType<BootJar> {
  mainClass.set("com.xenoterracide.brix.Application")
  archiveBaseName.set("brix")
  launchScript {
    properties(
      mapOf(
        "spring.config.location" to "classpath:application.properties"
      )
    )
  }
}

如何获取真实的工作目录?

您不能使用默认启动脚本。 It uses the directory that contains the jar file as the working directory。这主要针对用作服务而不是 CLI 应用程序的应用程序,并且无法使用默认脚本进行自定义。

我建议使用 Spring Boot 的功能来构建一个完全可执行的 jar 文件,其中包含满足您需要的自定义启动脚本。您可以使用 script 属性:

tasks.getByName<BootJar>("bootJar") {
    launchScript {
        script = file("src/custom.script")
    }
}

Here 是来自 CLI 应用程序的脚本,可以提供有用的起点:


#!/bin/bash

[[ -n "$DEBUG" ]] && set -x

working_directory="$(pwd)"

# Follow symlinks to find the real jar
cd "$(dirname "[=11=]")" || exit 1
[[ -z "$jarfile" ]] && jarfile=$(pwd)/$(basename "[=11=]")
while [[ -L "$jarfile" ]]; do
  jarfile=$(readlink "$jarfile")
  cd "$(dirname "$jarfile")" || exit 1
  jarfile=$(pwd)/$(basename "$jarfile")
done

cd "$working_directory"

# Find Java
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
    javaexe="$JAVA_HOME/bin/java"
elif type -p java > /dev/null 2>&1; then
    javaexe=$(type -p java)
elif [[ -x "/usr/bin/java" ]];  then
    javaexe="/usr/bin/java"
else
    echo "Unable to find Java"
    exit 1
fi

arguments=(-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar "$jarfile" $RUN_ARGS "$@")

"$javaexe" "${arguments[@]}"
exit $?