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 too long classpaths in script(bat/bash). #397

Merged
merged 2 commits into from
Nov 11, 2014
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
@@ -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