March 8, 201511 yr NOTE: This is not a guide on how to use Scala, or how to apply it for scripting. If there is enough interest in it, I can write a starting up guide later. When using Scala to create scripts, the exported scripts can be quite large. As in, the default sbt-assembly task creates a jar that exceeds 11MB just for a script skeleton. If you are familiar with how Scala works (and many other other java libraries), there are many things that are not used from the Scala library jar file that are included in there. How do we fix this problem in a sane way? Because Scala has a decent build tool used by many, I decided to use that to implement this solution. Here is a build.sbt file that defines a "buildScriptJar" task that packages up the compiled output into the smallest jar possible courtesy of ProGuard. import scala.sys.process.Process name := "OSBot-scala" version := "1.0" scalaVersion := "2.11.6" exportJars := true libraryDependencies += "net.sf.proguard" % "proguard-base" % "5.2" lazy val buildScriptJar = taskKey[Unit]("Builds OSBot script jar") buildScriptJar := { val managedjars: Seq[String] = (managedClasspath in Compile).value.files map (_.getCanonicalPath) val compiledjars: Seq[String] = (exportedProducts in Compile).value.files map (_.getCanonicalPath) val injars = compiledjars ++ managedjars var libjars: Seq[String] = (unmanagedClasspath in Compile).value.files map (_.getCanonicalPath) libjars = "<java.home>/lib/rt.jar" +: libjars val proguard: String = injars.filter(_ contains "proguard-base").head val output: String = "\"<user.home>/OSBot/Scripts/" + (if (name.value contains " ") "output" else name.value) + ".jar\"(!rootdoc.txt,!library.properties,!META-INF/**)" val options = Seq( "-jar", proguard, "@proguardconfig.pro", "-injars", injars mkString("\"", "\";\"", "\""), "-libraryjars", libjars mkString("\"", "\";\"", "\""), "-outjars", output ) Process("java", options).! match { case 0 => case n => sys error s"Proguard failed with exit code [$n]" } } It is a very simplistic task, where all it does is build that for you. You must start the compile task separately, this will not start it for you, although it will export the jars (needed). If you read the code, and you know a little about ProGuard, you can see that there is "@proguardconfig.pro" string inside the options Seq. This proguardconfig.pro is just your average configuration file for ProGuard, minus the in jars and out jars, ect. Here is my proguardconfig.pro file for reference... -dontskipnonpubliclibraryclassmembers -optimizationpasses 1 -allowaccessmodification -overloadaggressively -keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod -dontnote -dontwarn -keep,allowoptimization,allowobfuscation class * extends org.osbot.rs07.script.Script My general usage for this is calling "sbt compile buildScriptJar" and let it do the magic for me. Please configure this to suit your workflow. Anyways, I hope these build files help those of you that are using Scala. Edited March 8, 201511 yr by Bitrot
September 13, 201510 yr Thank you so much for posting this. Question: where in your SBT project do you put the proguardconfig.pro file?
Create an account or sign in to comment