Skip to content

Commit

Permalink
move to another plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nazoking committed Nov 11, 2014
1 parent ab49d7e commit af0cfe7
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,7 @@ object JavaAppPackaging extends AutoPlugin with JavaAppStartScript {
scriptClasspathOrdering <++= (Keys.dependencyClasspath in Runtime, projectDependencyArtifacts) map universalDepMappings,
scriptClasspathOrdering <<= (scriptClasspathOrdering) map { _.distinct },
mappings in Universal <++= scriptClasspathOrdering,
scriptClasspath := {
val allClasspath = makeRelativeClasspathNames(scriptClasspathOrdering.value)
val manifest = new java.util.jar.Manifest()
manifest.getMainAttributes().putValue("Class-Path", allClasspath.mkString(" "))
val jarFile = (target in Universal).value / "lib" / "manifest.jar"
IO.jar(Seq.empty, jarFile, manifest)
Seq((Keys.projectID, Keys.artifact in Compile in Keys.packageBin).map(makeManifestJarName).value)
},
mappings in Universal += {
((target in Universal).value / "lib" / "manifest.jar") -> ("lib/"+(Keys.projectID, Keys.artifact in Compile in Keys.packageBin).map(makeManifestJarName).value)
},
scriptClasspath <<= scriptClasspathOrdering map makeRelativeClasspathNames,
bashScriptExtraDefines := Nil,
bashScriptConfigLocation <<= bashScriptConfigLocation ?? None,
bashScriptDefines <<= (Keys.mainClass in Compile, scriptClasspath, bashScriptExtraDefines, bashScriptConfigLocation) map { (mainClass, cp, extras, config) =>
Expand Down Expand Up @@ -117,9 +107,6 @@ object JavaAppPackaging extends AutoPlugin with JavaAppStartScript {
else "../" + name
}

private def makeManifestJarName(id:ModuleID, art:Artifact): String =
makeJarName(id.organization, id.name, id.revision, art.name, art.classifier).replaceFirst("\\.jar$","-manifest.jar")

// Constructs a jar name from components...(ModuleID/Artifact)
private def makeJarName(org: String, name: String, revision: String, artifactName: String, artifactClassifier: Option[String]): String =
(org + "." +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.typesafe.sbt.packager
package archetypes.jar

import sbt._
import sbt.Keys.{ mappings, target, name, mainClass, sourceDirectory }
import com.typesafe.sbt.packager.Keys.{ scriptClasspath }
import com.typesafe.sbt.SbtNativePackager.{ Universal }

object ClasspathJarPlugin extends AutoPlugin {

object autoImport {
val classspathJarName = TaskKey[String]("classpath-jar-name", "classpath-jar name")
}
import autoImport._

override def requires = archetypes.JavaAppPackaging

override lazy val projectSettings = Seq[Setting[_]](
classspathJarName <<= (Keys.packageBin in Compile, Keys.projectID, Keys.artifact in Compile in Keys.packageBin) map { (jar, id, art) =>
makeJarName(id.organization, id.name, id.revision, art.name, art.classifier)
},
mappings in Universal += {
((target in Universal).value / "lib" / classspathJarName.value) -> ("lib/" + classspathJarName.value)
},
scriptClasspath := {
writeJar((target in Universal).value / "lib" / classspathJarName.value, scriptClasspath.value)
Seq(classspathJarName.value)
}
)

// Constructs a jar name from components...(ModuleID/Artifact)
private def makeJarName(org: String, name: String, revision: String, artifactName: String, artifactClassifier: Option[String]): String =
(org + "." +
name + "-" +
Option(artifactName.replace(name, "")).filterNot(_.isEmpty).map(_ + "-").getOrElse("") +
revision +
artifactClassifier.filterNot(_.isEmpty).map("-" + _).getOrElse("") +
"-classpath.jar")

/** write jar file */
private def writeJar(jarFile: File, allClasspath: Seq[String]) = {
val manifest = new java.util.jar.Manifest()
manifest.getMainAttributes().putValue("Class-Path", allClasspath.mkString(" "))
IO.jar(Seq.empty, jarFile, manifest)
}
}
29 changes: 29 additions & 0 deletions src/sbt-test/jar/classpath-jar/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
enablePlugins(ClasspathJarPlugin)

name := "classpath-jar-test"

version := "0.1.0"

// test dependencies sample
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-kernel" % "2.3.4"
)

TaskKey[Unit]("check-classspath") := {
val dir = (stagingDirectory in Universal).value
val bat = IO.read(dir / "bin" / "classpath-jar-test.bat")
assert(bat contains "set \"APP_CLASSPATH=%APP_LIB_DIR%\\classpath-jar-test.classpath-jar-test-0.1.0-classpath.jar\"")
val jar = new java.util.jar.JarFile(dir / "lib" / "classpath-jar-test.classpath-jar-test-0.1.0-classpath.jar")
assert(jar.getManifest().getMainAttributes().getValue("Class-Path").toString() contains "com.typesafe.akka.akka-actor")
jar close
}

TaskKey[Unit]("run-check") := {
val dir = (stagingDirectory in Universal).value
val cmd = if(System.getProperty("os.name").contains("Windows")){
Seq("cmd", "/c", (dir / "bin" / "classpath-jar-test.bat").getAbsolutePath)
}else{
Seq((dir / "bin" / "classpath-jar-test").getAbsolutePath)
}
assert(Process(cmd).!! contains "SUCCESS!")
}
1 change: 1 addition & 0 deletions src/sbt-test/jar/classpath-jar/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"))
19 changes: 19 additions & 0 deletions src/sbt-test/jar/classpath-jar/src/main/scala/test/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test

// use dependency library

import akka.actor._
import akka.routing.RoundRobinRouter

class PrintActor extends Actor{
def receive = {
case msg => println(msg)
}
}

object Test extends App {
val system = ActorSystem("testSystem")
val router = system.actorOf(Props[PrintActor])
router ! "SUCCESS!!"
system.shutdown
}
21 changes: 21 additions & 0 deletions src/sbt-test/jar/classpath-jar/src/templates/bat-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@echo off
rem this template is minimal for test independent

@setlocal enabledelayedexpansion
@echo off

set "@@APP_ENV_NAME@@_HOME=%~dp0\\.."

set "APP_LIB_DIR=%@@APP_ENV_NAME@@_HOME%\lib\"

set _JAVACMD=%JAVACMD%

if "%_JAVACMD%"=="" (
if not "%JAVA_HOME%"=="" (
if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
)
)

@@APP_DEFINES@@

"%_JAVACMD%" -cp "%APP_CLASSPATH%" %APP_MAIN_CLASS% %*
5 changes: 5 additions & 0 deletions src/sbt-test/jar/classpath-jar/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Run the staging and check the script.
> stage
$ exists target/universal/stage/lib/classpath-jar-test.classpath-jar-test-0.1.0-classpath.jar
> check-classspath
> run-check

0 comments on commit af0cfe7

Please sign in to comment.