Skip to content

Commit

Permalink
Support for absolute paths in classpath (#882)
Browse files Browse the repository at this point in the history
* Support for absolute paths when adding to classpath

Do not prefix with $lib_dir when an absolute path is added to the
classpath

* Add absolute path support in Windows

* Test case for absolute path support

* add comment

* bat filename should match project name

* Explicit check for path len
  • Loading branch information
hayssams authored and muuki88 committed Sep 28, 2016
1 parent 29a5ce1 commit 148243d
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.typesafe.sbt.packager.archetypes

import java.io.File
import java.net.URL

/**
Expand Down Expand Up @@ -34,7 +35,7 @@ object JavaAppAshScript {
extras

private def makeClasspathDefine(cp: Seq[String]): String = {
val fullString = cp map (n => "$lib_dir/" + n) mkString ":"
val fullString = cp map (n => if (n.startsWith(File.separator)) n else "$lib_dir/" + n) mkString ":"
"app_classpath=\"" + fullString + "\"\n"
}
def generateScript(defines: Seq[String], template: URL = bashTemplateSource): String = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.typesafe.sbt.packager.archetypes

import java.io.File
import java.net.URL

/**
Expand Down Expand Up @@ -34,7 +35,7 @@ object JavaAppBashScript {
extras

private def makeClasspathDefine(cp: Seq[String]): String = {
val fullString = cp map (n => "$lib_dir/" + n) mkString ":"
val fullString = cp map (n => if (n.startsWith(File.separator)) n else "$lib_dir/" + n) mkString ":"
"declare -r app_classpath=\"" + fullString + "\"\n"
}
def generateScript(defines: Seq[String], template: URL = bashTemplateSource): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ object JavaAppBatScript {

def makeWindowsRelativeClasspathDefine(cp: Seq[String]): String = {
def cleanPath(path: String): String = path.replaceAll("/", "\\\\")
def isAbsolute(path: String): Boolean =
path.length > 3 && // check path len is long enough to hold a windows absolute path ("c:\ ...")
Character.isLetter(path(0)) &&
path(1) == ':'

def makeRelativePath(path: String): String =
"%APP_LIB_DIR%\\" + cleanPath(path)
"set \"APP_CLASSPATH=" + (cp map makeRelativePath mkString ";") + "\""
"set \"APP_CLASSPATH=" + (cp map { path => if (isAbsolute(path)) path else makeRelativePath(path) } mkString ";") + "\""
}

def makeMainClassDefine(mainClass: String) = {
Expand Down
15 changes: 15 additions & 0 deletions src/sbt-test/bash/absolute-path-in-bash/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
enablePlugins(JavaAppPackaging)

name := "absolute-path-in-bash"

version := "0.1.0"

scriptClasspath in bashScriptDefines ++= Seq("/dummy/absolute/path", "relative/path")

TaskKey[Unit]("run-check") := {
val dir = (stagingDirectory in Universal).value

val bash = IO.read(dir / "bin" / "absolute-path-in-bash")
assert(bash contains ":/dummy/absolute/path")
assert(bash contains ":$lib_dir/relative/path")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object MainApp extends App {
println("SUCCESS!")
}
3 changes: 3 additions & 0 deletions src/sbt-test/bash/absolute-path-in-bash/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Run the staging and check the script.
> stage
> run-check
21 changes: 21 additions & 0 deletions src/sbt-test/universal/absolute-path/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
enablePlugins(JavaAppPackaging)

name := "absolute-path"

version := "0.1.0"

scriptClasspath in bashScriptDefines ++= Seq("/dummy/absolute/path", "relative/path")

scriptClasspath in batScriptReplacements ++= Seq("x:\\dummy\\absolute\\path", "relative\\path")

TaskKey[Unit]("run-check") := {
val dir = (stagingDirectory in Universal).value

val bash = IO.read(dir / "bin" / "absolute-path")
assert(bash contains ":/dummy/absolute/path")
assert(bash contains ":$lib_dir/relative/path")

val bat = IO.read(dir / "bin" / "absolute-path.bat")
assert(bat contains ";x:\\dummy\\absolute\\path")
assert(bat contains "%APP_LIB_DIR%\\relative\\path")
}
1 change: 1 addition & 0 deletions src/sbt-test/universal/absolute-path/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object MainApp extends App {
println("SUCCESS!")
}
3 changes: 3 additions & 0 deletions src/sbt-test/universal/absolute-path/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Run the staging and check the script.
> stage
> run-check
15 changes: 15 additions & 0 deletions src/sbt-test/windows/absolute-path-in-bat/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
enablePlugins(JavaAppPackaging)

name := "absolute-path-in-bat"

version := "0.1.0"

scriptClasspath in batScriptReplacements ++= Seq("x:\\dummy\\absolute\\path", "relative\\path")

TaskKey[Unit]("run-check") := {
val dir = (stagingDirectory in Universal).value

val bat = IO.read(dir / "bin" / "absolute-path-in-bat.bat")
assert(bat contains ";x:\\dummy\\absolute\\path")
assert(bat contains "%APP_LIB_DIR%\\relative\\path")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object MainApp extends App {
println("SUCCESS!")
}
3 changes: 3 additions & 0 deletions src/sbt-test/windows/absolute-path-in-bat/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Run the staging and check the script.
> stage
> run-check

0 comments on commit 148243d

Please sign in to comment.