This page is no longer maintained — Please continue to the home page at www.scala-lang.org

sbt - create runnable jar?

3 replies
Marc Weber
Joined: 2010-02-16,
User offline. Last seen 2 years 3 weeks ago.

Hi scala users,

In the past I've been using Maven to create runnable jars:

org.apache.maven.plugins
maven-jar-plugin

true
SaveTVUrlGetter.SaveTVUrlGetter.App

Is there a similar sbt way to do this and how to run the resulting
archive?

Marc Weber

Brian Clapper
Joined: 2009-05-18,
User offline. Last seen 42 years 45 weeks ago.
Re: sbt - create runnable jar?

On 6/17/10 6:09 PM, Marc Weber wrote:
> Hi scala users,
>
> In the past I've been using Maven to create runnable jars:
>
>
> org.apache.maven.plugins
> maven-jar-plugin
>
>
>
> true
> SaveTVUrlGetter.SaveTVUrlGetter.App
>
>
>
>
>
> Is there a similar sbt way to do this and how to run the resulting
> archive?
>
> Marc Weber

Marc,

I recommend that you take this question over to the SBT mailing list (which
I've CC'd on this response). Also, take a look at:

http://code.google.com/p/simple-build-tool/wiki/BuildConfiguration#Packa...

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: sbt - create runnable jar?

"sbt run" will launch your app. "sbt package" packages it into a jar, however not including (re-packaging) dependancies. there is a proguard plug-in for sbt that can be used to created fully self-contained jars.

Am 18.06.2010 um 00:09 schrieb Marc Weber:

> Hi scala users,
>
> In the past I've been using Maven to create runnable jars:
>
>
> org.apache.maven.plugins
> maven-jar-plugin
>
>
>
> true
> SaveTVUrlGetter.SaveTVUrlGetter.App
>
>
>
>
>
> Is there a similar sbt way to do this and how to run the resulting
> archive?
>
> Marc Weber

Peter Salanki
Joined: 2010-01-27,
User offline. Last seen 42 years 45 weeks ago.
Re: sbt - create runnable jar?

I add this to my project.scala to have proguard create an executable jar with all dependencies:

/****** Configuration *******/
override def mainClass = Some("st.salanki.torssnt.Torssnt")

/******** Proguard *******/
lazy val outputJar = outputPath / (name + "-" + version + "-standalone.jar")

val proguardJar = "net.sf.proguard" % "proguard" % "4.3" % "tools->default"
val toolsConfig = config("tools")
def rootProjectDirectory = rootProject.info.projectPath
val proguardConfigurationPath: Path = outputPath / "proguard.pro"
lazy val proguard = proguardTask dependsOn(`package`, writeProguardConfiguration)
private lazy val writeProguardConfiguration = writeProguardConfigurationTask dependsOn `package`
//lazy val pack = packTask dependsOn(proguard)

private def proguardTask = task {
FileUtilities.clean(outputJar :: Nil, log)
val proguardClasspathString = Path.makeString(managedClasspath(toolsConfig).get)
val configFile = proguardConfigurationPath.toString
val exitValue = Process("java", List("-Xmx256M", "-cp", proguardClasspathString, "proguard.ProGuard", "@" + configFile)) ! log
if(exitValue == 0) None else Some("Proguard failed with nonzero exit code (" + exitValue + ")")
}

private def writeProguardConfigurationTask =
task {
/* the template for the proguard configuration file
* You might try to remove "-keep class *" and "-keep class *", but this might break dynamic classloading.
*/
val outTemplate = """
|-dontskipnonpubliclibraryclasses
|-dontskipnonpubliclibraryclassmembers
|-dontoptimize
|-dontobfuscate
|-dontshrink
|-dontpreverify
|-dontnote
|-dontwarn
|-libraryjars %s
|%s
|-outjars %s
|-ignorewarnings
|-keep class *
|-keep class %s { *** main(...); }
|"""

val defaultJar = (outputPath / defaultJarName).asFile.getAbsolutePath
log.debug("proguard configuration using main jar " + defaultJar)

val externalDependencies = Set() ++ (
mainCompileConditional.analysis.allExternals ++ compileClasspath.get.map { _.asFile }
) map { _.getAbsoluteFile } filter { _.getName.endsWith(".jar") }

def quote(s: Any) = '"' + s.toString + '"'
log.debug("proguard configuration external dependencies: \n\t" + externalDependencies.mkString("\n\t"))
// partition jars from the external jar dependencies of this project by whether they are located in the project directory
// if they are, they are specified with -injars, otherwise they are specified with -libraryjars
val (externalJars, libraryJars) = externalDependencies.toList.partition(jar => Path.relativize(rootProjectDirectory, jar).isDefined)
log.debug("proguard configuration library jars locations: " + libraryJars.mkString(", "))
// exclude properties files and manifests from scala-library jar
val inJars = (quote(defaultJar) :: externalJars.map(quote(_) + "(!META-INF/**,!*.txt)")).map("-injars " + _).mkString("\n")

val proguardConfiguration = outTemplate.stripMargin.format(libraryJars.map(quote).mkString(File.pathSeparator), inJars, quote(outputJar.absolutePath), mainClass.get)
log.debug("Proguard configuration written to " + proguardConfigurationPath)
FileUtilities.write(proguardConfigurationPath.asFile, proguardConfiguration, log)
}

On Jun 18, 2010, at 1:51 PM, Sciss wrote:

> "sbt run" will launch your app. "sbt package" packages it into a jar, however not including (re-packaging) dependancies. there is a proguard plug-in for sbt that can be used to created fully self-contained jars.
>
> Am 18.06.2010 um 00:09 schrieb Marc Weber:
>
>> Hi scala users,
>>
>> In the past I've been using Maven to create runnable jars:
>>
>>
>> org.apache.maven.plugins
>> maven-jar-plugin
>>
>>
>>
>> true
>> SaveTVUrlGetter.SaveTVUrlGetter.App
>>
>>
>>
>>
>>
>> Is there a similar sbt way to do this and how to run the resulting
>> archive?
>>
>> Marc Weber
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland