Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto create a .jar including both sources (.java and .scala) and classes with sbt?

Tags:

class

jar

scala

sbt

I would like sbt package or any variant to produce a '.jar' from my project that would include also the sources ('.java' and '.scala' files).

This would be a mix of packageBin and packageSrc.

I did not:

  • find any task that would do this?
  • find how to adapt package task
  • nor define a new task of mine to achieve this

Thanks for any hint.

like image 312
ttamttam Avatar asked Mar 27 '15 08:03

ttamttam


People also ask

Which sbt command make project as jar?

When you uses Simple Build Tool Command 'sbt package', it creates a jar file that includes the class files from your source code and also the content from your src/main/resources folder. Your project dependencies (JAR files in your project's lib folder or managed dependencies declared in build. sbt).

How do I run a JAR file in sbt?

A JAR file created by SBT can be run by the Scala interpreter, but not the Java interpreter. This is because class files in the JAR file created by sbt package have dependencies on Scala class files (Scala libraries), which aren't included in the JAR file SBT generates.

What does %% mean in sbt?

This is part of SBT which play uses as a build tool. Specifically this is an import statement. The percent symbol % is a actually a method used to build dependencies. The double percent sign %% injects the current Scala version - this allows you to get the correct library for the version of scala you are running.


2 Answers

Looking at the documentation, you should probably add stuff to the mappings key in the packageBin scope. The following seems to work for me:

mappings in (Compile, packageBin) ++= (mappings in (Compile, packageSrc)).value
like image 97
0__ Avatar answered Oct 08 '22 15:10

0__


Here's how to setup a standalone task for creating an artifact with binaries and sources, leaving packageBin and packageSrc unaffected:

val packageBinSrc = taskKey[File]("Produces an artifact containing both binaries and sources.")

artifactClassifier in packageBinSrc := Some("binsrc")
inConfig(Compile) {
  import Defaults._
  packageTaskSettings(packageBinSrc, concatMappings(packageBinMappings, packageSrcMappings))
}

Optionally, if you fancy, you can redefine package to use packageBinSrc:

Keys.`package` := (packageBinSrc in Compile).value
like image 25
Dale Wijnand Avatar answered Oct 08 '22 15:10

Dale Wijnand