Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #495. Enhanced locateJDKPackagerTool to work on Windows when SBT... #496

Merged
merged 2 commits into from
Feb 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.typesafe.sbt.packager.jdkpackager

import java.io.FileNotFoundException

import com.typesafe.sbt.packager.chmod
import sbt._

import scala.tools.util.PathResolver
import scala.util.Try
/**
* Support/helper functions for interacting with the `javapackager` tool.
* @author <a href="mailto:[email protected]">Simeon H.K. Fitch</a>
Expand All @@ -11,19 +15,45 @@ import sbt._
object JDKPackagerHelper {

/** Attempts to compute the path to the `javapackager` tool. */
def locateJDKPackagerTool(): Option[File] = {
val jdkHome = sys.props.get("java.home").map(p ⇒ file(p))

// TODO: How to get version of javaHome target?
val javaVersion = VersionNumber(sys.props("java.specification.version"))
val toolname = javaVersion.numbers.take(2) match {
case Seq(1, m) if m >= 8 ⇒ "javapackager"
case _ ⇒ sys.error("Need at least JDK version 1.8 to run JDKPackager plugin")
def locateJDKPackagerTool(javaHome: Option[File]): Option[File] = {
val toolname = sys.props("os.name").toLowerCase match {
case os if os.contains("win") ⇒ "javapackager.exe"
case _ ⇒ "javapackager"
}
jdkHome
.map(f ⇒ if (f.getName == "jre") f / ".." else f)
.map(f ⇒ f / "bin" / toolname)
.filter(_.exists())

// This approach to getting JDK bits is borrowed from: http://stackoverflow.com/a/25163628/296509
// Starting with an ordered list of possible java directory sources, create derivative and
// then test for the tool. It's nasty looking because there's no canonical way of finding the
// JDK from the JRE, and JDK_HOME isn't always defined.
Seq(
// Build-defined
javaHome,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I didn't know that setting. One couldn't believe how hard it is to find the JDK on a system -.-

// Environment override
sys.env.get("JDK_HOME").map(file),
sys.env.get("JAVA_HOME").map(file),
// MacOS X
Try("/usr/libexec/java_home".!!.trim).toOption.map(file),
// From system properties
sys.props.get("java.home").map(file)
)
// Unlift Option-s
.flatten
// For each base directory, add the parent variant to cover nested JREs on Unix.
.flatMap {
f ⇒ Seq(f, f.getAbsoluteFile)
}
// On Windows we're often running in the JRE and not the JDK. If JDK is installed,
// it's likely to be in a parallel directory, with the "jre" prefix changed to "jdk"
.flatMap { f ⇒
if (f.getName.startsWith("jre")) {
Seq(f, f.getParentFile / ("jdk" + f.getName.drop(3)))
} else Seq(f)
}
// Now search for the tool
.map { n =>
n / "bin" / toolname
}
.find(_.exists)
}

/**
Expand Down Expand Up @@ -112,7 +142,7 @@ object JDKPackagerHelper {
// To help debug arguments, create a bash script doing the same.
val script = file(argMap("-outdir")) / "jdkpackager.sh"
IO.write(script, s"#!/bin/bash\n$argString\n")
chmod(script, "766")
Try(chmod(script, "766"))

Process(args)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,4 @@ trait JDKPackagerKeys {
| Defaults to generic Java icon.
""".stripMargin)

// To consider:
// val jvmOptionFile = SettingKey[Option[File]]("jvmOptionFile",
// "File with line delimited options to pass to the application JVM. " +
// "Defaults to `stage/conf/jvmopts")
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ object JDKPackagerPlugin extends AutoPlugin {
private val dirname = JDKPackager.name.toLowerCase

def javaPackagerSettings: Seq[Setting[_]] = Seq(
jdkPackagerTool := JDKPackagerHelper.locateJDKPackagerTool(),
jdkPackagerTool <<= javaHome apply JDKPackagerHelper.locateJDKPackagerTool,
jdkAppIcon := None,
jdkPackagerType := "image",
jdkPackagerBasename <<= packageName apply (_ + "-pkg")
// jvmOptionFile <<= sourceDirectory apply (src ⇒ Some(src / "conf" / "jvmopts")))
// mappings in JDKPackager <<= (mappings in Universal) map (_.filter(!_._2.startsWith("bin/")))
) ++ inConfig(JDKPackager)(
Seq(
sourceDirectory <<= sourceDirectory apply (_ / dirname),
Expand All @@ -57,7 +55,7 @@ object JDKPackagerPlugin extends AutoPlugin {
)

private def checkTool(maybePath: Option[File]) = maybePath.getOrElse(
sys.error("Please set key `jdkPackagerTool` to `javapackager` path")
sys.error("Please set key `jdkPackagerTool` to `javapackager` path, which should be find in the `bin` directory of JDK 8 installation.")
)

private def makePackageBuilder = Seq(
Expand All @@ -68,7 +66,7 @@ object JDKPackagerPlugin extends AutoPlugin {

(proc ! s.log) match {
case 0 ⇒ ()
case x ⇒ sys.error(s"Error running '$tool', exit status: $x")
case x ⇒ s.log.warn(s"'$tool' had exit status: $x")
}

// Oooof. Need to do better than this to determine what was generated.
Expand Down
3 changes: 2 additions & 1 deletion src/sphinx/formats/jdkpackager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ The `javapackager` tool comes with JDK 8, and found in the `bin` directory along

This plugin must be run on the platform of the target installer. The `javapackager` tool does not provide a means of creating, say, Windows installers on MacOS, etc.

To use create Windows launchers & installers, the WIX Toolset is required:
To use create Windows launchers & installers, the either the WIX Toolset (``msi``) or Inno Setup (``exe``) is required:

* `WIX Toolset <http://wixtoolset.org/>`_
* `Inno Setup <http://www.jrsoftware.org/isinfo.php>`_

For further details on the capabilities of `javapackager`, see the `windows <http://docs.oracle.com/javase/8/docs/technotes/tools/windows/javapackager.html>`_ and `Unix <http://docs.oracle.com/javase/8/docs/technotes/tools/unix/javapackager.html>`_ references. (Note: only a few of the possible settings are exposed through this plugin. Please submit a `Github <https://github.com/sbt/sbt-native-packager/issues>`_ issue or pull request if something specific is desired.)

Expand Down
3 changes: 2 additions & 1 deletion test-project-jdkpackager/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ mainClass in Compile := Some("ExampleApp")

enablePlugins(JDKPackagerPlugin)

maintainer := "Simeon H.K Fitch <[email protected]>"
// This becomes the Start Menu subdirectory for the windows installers.
maintainer := "Previously Owned Cats, Inc."

packageSummary := "JDKPackagerPlugin example package thingy"

Expand Down
Binary file not shown.