Skip to content

Commit

Permalink
prependShellScript: Option[Seq[String]]. sbt#97
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Jan 5, 2014
1 parent 70127c4 commit 7745dc1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,23 @@ artifact in (Compile, assembly) ~= { art =>
addArtifact(artifact in (Compile, assembly), assembly)
```

### Prepending Shebang

You can prepend shell script to the fat jar as follows:

```scala
assemblyOption in assembly ~= { _.copy(prependShellScript = Some(defaultShellScript)) }

jarName in assembly := { s"${name.value}-${version.value}" }
```

This will prepend the following shell script to the jar.

```
#!/usr/bin/env sh
exec java -jar "$0" "$@"
```

License
-------

Expand Down
23 changes: 23 additions & 0 deletions notes/0.10.2.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
[97]: https://github.com/sbt/sbt-assembly/pull/97
[98]: https://github.com/sbt/sbt-assembly/issues/98
[@mrt]: https://github.com/mrt
[@tlockney]: https://github.com/tlockney

### prependShellScript

sbt-assembly 0.10.2 adds an option to prepend shell script to to the fat jar.

assemblyOption in assembly ~= { _.copy(prependShellScript = Some(defaultShellScript)) }

jarName in assembly := { s"${name.value}-${version.value}" }

The above will prepend the following shell script to the jar, which apparently is legal in the state of Washington and Colorado.

$ head target/scala-2.10/helloworld-0.1-SNAPSHOT
#!/usr/bin/env sh
exec java -jar "$0" "$@"

The fat jar can be executed given that you have `sh` on the system:

$ ./target/scala-2.10/helloworld-0.1-SNAPSHOT
Hello

This feature was contributed by [@tlockney][@tlockney] as [#97][97].

### bug fixes

Expand Down
52 changes: 27 additions & 25 deletions src/main/scala/sbtassembly/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,19 @@ object Plugin extends sbt.Plugin {
}
}
Package.makeJar(ms, outPath, manifest, log)
if (ao.prependShellScript) {
ao.prependShellScript foreach { shellScript: Seq[String] =>
val tmpFile = cacheDir / "assemblyExec.tmp"
if (tmpFile.exists()) tmpFile.delete()
val jarCopy = IO.copyFile(outPath, tmpFile)
IO.writeLines(outPath, ao.scriptToPrepend, append = false)
IO.writeLines(outPath, shellScript, append = false)
val jarBytes = IO.readBytes(tmpFile)
tmpFile.delete()
IO.append(outPath, jarBytes)
try {
Seq("chmod", "+x", outPath.toString).!
}
catch {
case e: IOException log.warn("Could not run 'chmod +x' on jarfile. Perhaps chmod command is not available?")
case e: IOException => log.warn("Could not run 'chmod +x' on jarfile. Perhaps chmod command is not available?")
}
}
}
Expand Down Expand Up @@ -432,6 +432,8 @@ object Plugin extends sbt.Plugin {
case _ => MergeStrategy.deduplicate
}

val defaultShellScript: Seq[String] = Seq("#!/usr/bin/env sh", """exec java -jar "$0" "$@"""") // "

lazy val baseAssemblySettings: Seq[sbt.Def.Setting[_]] = Seq(
assembly := Assembly.assemblyTask(assembly).value,
assembledMappings in assembly := Assembly.assembledMappingsTask(assembly).value,
Expand All @@ -451,28 +453,29 @@ object Plugin extends sbt.Plugin {
assembleArtifact in packageDependency := true,
mergeStrategy in assembly := defaultMergeStrategy,
excludedJars in assembly := Nil,
assemblyOption in assembly <<= (assembleArtifact in packageBin,
assembleArtifact in packageScala, assembleArtifact in packageDependency,
mergeStrategy in assembly,
excludedJars in assembly,
streams) map {
(includeBin, includeScala, includeDeps, ms, excludedJars, s) =>
AssemblyOption(assemblyDirectory = s.cacheDirectory / "assembly",
includeBin = includeBin,
includeScala = includeScala,
includeDependency = includeDeps,
mergeStrategy = ms,
excludedJars = excludedJars,
excludedFiles = defaultExcludedFiles,
cacheOutput = true,
cacheUnzip = true,
appendContentHash = false)
assemblyOption in assembly := {
val s = streams.value
AssemblyOption(
assemblyDirectory = s.cacheDirectory / "assembly",
includeBin = (assembleArtifact in packageBin).value,
includeScala = (assembleArtifact in packageScala).value,
includeDependency = (assembleArtifact in packageDependency).value,
mergeStrategy = (mergeStrategy in assembly).value,
excludedJars = (excludedJars in assembly).value,
excludedFiles = defaultExcludedFiles,
cacheOutput = true,
cacheUnzip = true,
appendContentHash = false,
prependShellScript = None)
},
assemblyOption in packageScala <<= (assemblyOption in assembly) map { opt =>
opt.copy(includeBin = false, includeScala = true, includeDependency = false)

assemblyOption in packageScala := {
val ao = (assemblyOption in assembly).value
ao.copy(includeBin = false, includeScala = true, includeDependency = false)
},
assemblyOption in packageDependency <<= (assemblyOption in assembly) map { opt =>
opt.copy(includeBin = false, includeScala = true, includeDependency = true)
assemblyOption in packageDependency := {
val ao = (assemblyOption in assembly).value
ao.copy(includeBin = false, includeScala = true, includeDependency = true)
},

// packageOptions
Expand Down Expand Up @@ -518,5 +521,4 @@ case class AssemblyOption(assemblyDirectory: File,
cacheOutput: Boolean = true,
cacheUnzip: Boolean = true,
appendContentHash: Boolean = false,
prependShellScript: Boolean = false,
scriptToPrepend: Seq[String] = Seq("#!/usr/bin/env sh", """exec java -jar "$0" "$@""""))
prependShellScript: Option[Seq[String]] = None)

0 comments on commit 7745dc1

Please sign in to comment.