diff --git a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template index e2b70c631..a6818f3c5 100644 --- a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template +++ b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template @@ -57,7 +57,12 @@ addResidual () { } addDebugger () { - addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:$1" + local major_version=$(get_java_major_version) + local address="*:$1" + if [ $major_version -le 8 ]; then + address="$1" + fi + addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$address" } require_arg () { @@ -69,6 +74,24 @@ require_arg () { fi } +get_java_major_version () { + readonly java_version=$("$java_cmd" -version 2>&1 | awk -F '"' '/version/ {print $2}') + if [[ "$java_version" == "" ]]; then + echoerr "Could not determine Java version. Assuming 8." + echo 8 + else + local major=$(echo "$java_version" | cut -d'.' -f1) + if [[ "$major" -eq "1" ]]; then + local major=$(echo "$java_version" | cut -d'.' -f2) + fi + echo $major + fi +} + +echoerr () { + echo 1>&2 "$@" +} + # Allow user to specify java options. These get listed first per bash-template. if [ -n "$JAVA_OPTS" ] then @@ -95,13 +118,16 @@ get_java_cmd() { # Processes incoming arguments and places them in appropriate global variables. called by the run method. process_args () { local no_more_snp_opts=0 + # this depends on -java-home, so we need to delay calling addDebugger until all opts have been parsed + local should_add_debugger= + while [ $# -gt 0 ]; do case "$1" in --) shift && no_more_snp_opts=1 && break ;; -h|-help) usage; exit 1 ;; -mem) echo "!! WARNING !! -mem option is ignored. Please use -J-Xmx and -J-Xms" && shift 2 ;; - -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;; + -jvm-debug) require_arg port "$1" "$2" && should_add_debugger=$2 && shift 2 ;; -main) custom_mainclass="$2" && shift 2 ;; @@ -113,6 +139,10 @@ process_args () { esac done + if [[ $should_add_debugger ]]; then + addDebugger $should_add_debugger + fi + if [ $no_more_snp_opts ]; then while [ $# -gt 0 ]; do addResidual "$1" && shift @@ -158,6 +188,7 @@ app_mainclass=${{app_mainclass}} ${{template_declares}} +java_cmd="$(get_java_cmd)" process_args "$@" # Fallback to custom mainclass if main class is not provided (this is the case if the JAR contains multiple apps) @@ -170,8 +201,6 @@ if [ "$app_mainclass" = "" ] || [ $custom_mainclass ];then app_mainclass=$custom_mainclass fi -java_cmd="$(get_java_cmd)" - # If a configuration file exist, read the contents to $opts [ -f "$script_conf_file" ] && opts=$(loadConfigFile "$script_conf_file") diff --git a/src/sbt-test/ash/script-debug/build.sbt b/src/sbt-test/ash/script-debug/build.sbt index dbb68e1ea..7f93eae2c 100644 --- a/src/sbt-test/ash/script-debug/build.sbt +++ b/src/sbt-test/ash/script-debug/build.sbt @@ -1,5 +1,3 @@ -import java.net.ServerSocket - enablePlugins(JavaAppPackaging, AshScriptPlugin) name := "script-debug" diff --git a/src/sbt-test/bash/script-debug/build.sbt b/src/sbt-test/bash/script-debug/build.sbt index 2e2296537..0107e2301 100644 --- a/src/sbt-test/bash/script-debug/build.sbt +++ b/src/sbt-test/bash/script-debug/build.sbt @@ -1,5 +1,3 @@ -import java.net.ServerSocket - enablePlugins(JavaAppPackaging) name := "script-debug"