From ab49d7e9411f81466f39c7af4e977877d99b0c44 Mon Sep 17 00:00:00 2001 From: nazoking Date: Tue, 28 Oct 2014 14:51:42 +0900 Subject: [PATCH 1/2] fix too long classpaths in script(bat/bash). Create manifest.jar it contains Class-Path manifest. java classpath option need only a manifest.jar. --- .../sbt/packager/archetypes/JavaApp.scala | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/scala/com/typesafe/sbt/packager/archetypes/JavaApp.scala b/src/main/scala/com/typesafe/sbt/packager/archetypes/JavaApp.scala index 1a18f2724..ee88667b4 100644 --- a/src/main/scala/com/typesafe/sbt/packager/archetypes/JavaApp.scala +++ b/src/main/scala/com/typesafe/sbt/packager/archetypes/JavaApp.scala @@ -59,7 +59,17 @@ object JavaAppPackaging extends AutoPlugin with JavaAppStartScript { scriptClasspathOrdering <++= (Keys.dependencyClasspath in Runtime, projectDependencyArtifacts) map universalDepMappings, scriptClasspathOrdering <<= (scriptClasspathOrdering) map { _.distinct }, mappings in Universal <++= scriptClasspathOrdering, - scriptClasspath <<= scriptClasspathOrdering map makeRelativeClasspathNames, + 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) + }, bashScriptExtraDefines := Nil, bashScriptConfigLocation <<= bashScriptConfigLocation ?? None, bashScriptDefines <<= (Keys.mainClass in Compile, scriptClasspath, bashScriptExtraDefines, bashScriptConfigLocation) map { (mainClass, cp, extras, config) => @@ -107,6 +117,9 @@ 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 + "." + From af0cfe73b4921867a468e841677983b5ad54798d Mon Sep 17 00:00:00 2001 From: nazoking Date: Tue, 11 Nov 2014 14:37:39 +0900 Subject: [PATCH 2/2] move to another plugin --- .../sbt/packager/archetypes/JavaApp.scala | 15 +----- .../sbt/packager/jar/ClasspathJarPlugin.scala | 46 +++++++++++++++++++ src/sbt-test/jar/classpath-jar/build.sbt | 29 ++++++++++++ .../jar/classpath-jar/project/plugins.sbt | 1 + .../src/main/scala/test/Test.scala | 19 ++++++++ .../classpath-jar/src/templates/bat-template | 21 +++++++++ src/sbt-test/jar/classpath-jar/test | 5 ++ 7 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 src/main/scala/com/typesafe/sbt/packager/jar/ClasspathJarPlugin.scala create mode 100644 src/sbt-test/jar/classpath-jar/build.sbt create mode 100644 src/sbt-test/jar/classpath-jar/project/plugins.sbt create mode 100644 src/sbt-test/jar/classpath-jar/src/main/scala/test/Test.scala create mode 100644 src/sbt-test/jar/classpath-jar/src/templates/bat-template create mode 100644 src/sbt-test/jar/classpath-jar/test diff --git a/src/main/scala/com/typesafe/sbt/packager/archetypes/JavaApp.scala b/src/main/scala/com/typesafe/sbt/packager/archetypes/JavaApp.scala index ee88667b4..1a18f2724 100644 --- a/src/main/scala/com/typesafe/sbt/packager/archetypes/JavaApp.scala +++ b/src/main/scala/com/typesafe/sbt/packager/archetypes/JavaApp.scala @@ -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) => @@ -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 + "." + diff --git a/src/main/scala/com/typesafe/sbt/packager/jar/ClasspathJarPlugin.scala b/src/main/scala/com/typesafe/sbt/packager/jar/ClasspathJarPlugin.scala new file mode 100644 index 000000000..5ab594623 --- /dev/null +++ b/src/main/scala/com/typesafe/sbt/packager/jar/ClasspathJarPlugin.scala @@ -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) + } +} diff --git a/src/sbt-test/jar/classpath-jar/build.sbt b/src/sbt-test/jar/classpath-jar/build.sbt new file mode 100644 index 000000000..feb1a6545 --- /dev/null +++ b/src/sbt-test/jar/classpath-jar/build.sbt @@ -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!") +} diff --git a/src/sbt-test/jar/classpath-jar/project/plugins.sbt b/src/sbt-test/jar/classpath-jar/project/plugins.sbt new file mode 100644 index 000000000..b53de154c --- /dev/null +++ b/src/sbt-test/jar/classpath-jar/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version")) diff --git a/src/sbt-test/jar/classpath-jar/src/main/scala/test/Test.scala b/src/sbt-test/jar/classpath-jar/src/main/scala/test/Test.scala new file mode 100644 index 000000000..23a87b45e --- /dev/null +++ b/src/sbt-test/jar/classpath-jar/src/main/scala/test/Test.scala @@ -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 +} diff --git a/src/sbt-test/jar/classpath-jar/src/templates/bat-template b/src/sbt-test/jar/classpath-jar/src/templates/bat-template new file mode 100644 index 000000000..e69e06fff --- /dev/null +++ b/src/sbt-test/jar/classpath-jar/src/templates/bat-template @@ -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% %* diff --git a/src/sbt-test/jar/classpath-jar/test b/src/sbt-test/jar/classpath-jar/test new file mode 100644 index 000000000..c40a5f301 --- /dev/null +++ b/src/sbt-test/jar/classpath-jar/test @@ -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