I want to import below code packaged as jar to another sbt intellij project how could i do this?
package yyy
object Hello extends Greeting with App {
println(greeting)
}
trait Greeting {
lazy val greeting: String = "hello"
}
If you have this code packaged as a jar, you can simply place it in the lib/ folder of the other sbt project. It should be on the classpath, so you'll be able to do import yyy._ in the code.
You can read more about unmanaged dependencies in the sbt documentation. Here's an excerpt from it:
Unmanaged dependencies work like this: add jars to lib and they will be placed on the project classpath. Not much else to it!
Dependencies in lib go on all the classpaths (for compile, test, run, and console).
There’s nothing to add to build.sbt to use unmanaged dependencies, though you could change the unmanagedBase key if you’d like to use a different directory rather than lib.
Related
I am working on a live project involving technologies like Scala, Akka and Slick. I have to implement the SMS functionality. After googling I didn't get a single example using Scala, but I got a few using Java and some comments saying that Simplewire is best to implement this functionality. But I am not able to find the library dependencies for any of them. Help me to resolve and implement this simplewire example using Scala.
I tried to start like this,
import com.simplewire.sms._;
object SMSHelper {
def sendSMS = {
val sms = new SMS() // SMS() is not resolving
}
// ...
}
From the sbt documentation:
Unmanaged dependencies work like this: add jars to lib and they will
be placed on the project classpath. Not much else to it!
Download the JAR file from here and simply copy it into the lib folder located at the root of your SBT project then sbt compile and it should work.
If you want to place your libraries in another folder (for instance custom_lib), add this line to your build.sbt file :
unmanagedBase := baseDirectory.value / "custom_lib"
I have a simple Scala command line App that I want to package using SBT.
object Transform extends App {
val source = scala.io.Source.fromFile(args(0))
...
}
I can't seem to find anything in the SBT docs or an online example of a SBT configuration/command that would allows me to create a standalone executable jar (java -jar ...) with the appropriate manifest and dependencies included.
I did find SBT Assembly, but it looks to be a plugin for SBT < 0.13.5.
sbt-onejar was created for exactly this use case.
I have several SBT 0.13 / Play 2.2 projects (websites). They are all multi-module as they share some common functionality. This makes their project configuration files both complex and almost identical, but not quite.
I would like to be able to share as much as possible of these configuration files across the projects (frequent play updates makes keeping 5+ websites up to date a royal pain, not to mention all the almost-identical-but-evolving dependency lists across the projects).
build.properties and plugins.sbt are identical across projects and can be overwritten by a simple script. Great.
Build.scala is trickier - I would like to introduce a shared base class like so:
abstract class MyBuildBase extends Build { ... }
that in Build.scala do:
object ApplicationBuild extends MyBuildBuild { ... }
In order for this to make any sense at all, MyBuildBase.scala needs to be shared across projects. This can be done with svn:external, which operates on directories. Which means I need to somehow make this shared directory accessible when Build.scala is compiled (otherwise sbt complains loudly).
Reading http://www.scala-sbt.org/0.13.0/docs/Detailed-Topics/Classpaths.html and http://www.scala-sbt.org/0.13.0/docs/Getting-Started/Full-Def.html it seems like this should be possible.
However, it is exceptionally unclear to me what to actually put in the project/project/Build.scala file to actually achieve this - I can't find an example of "an sbt build file that's intended to build an sbt build file and include some extra source files in the build".
Any suggestions?
What you probably want to do is create a plugin, or shared library.
You can make an sbt project with a build like follows:
build.sbt
sbtPlugin := true
organization := "you"
name := "common-build"
version := "1.0"
Then create in src/main/scala your abstract class "MyBuildBase". Release this project as an sbt plugin.
Then in your other projects, you can use this as a library/plugin. In project/plugins.sbt add:
addSbtPlugin("you" % "common-build" % "1.0")
And this will resolve your common build library when building your build.
If you need more information, look up more about sbt plugins and ignore the part about making something that extends a Plugin. Plugins are just libraries versioned with sbt's version number and your own. You should be able to put whatever code you want in there to share between builds.
Note: in 2016, Build.scala is deprecated for Build.sbt.
Here is the new (Dec. 2016) multi-module with App Scala sbt template by Michael Lewis.
Usage
sbt new lewismj/sbt-template.g8
You can then run:
sbt compile
sbt publish-local
sbt assembly
It is based on Scala SBT template (Library)
This giter8 template will write SBT build files for a Scala library.
I use SBT 0.11.0. It requires that all sources must be placed in:
[project]/src/main/scala/
However I want a custom package such as:
[project]/src/com/test/...
I can leave things as SBT requires, then specify in every source file the custom package:
package com.test
But I got some problems with Eclipse auto-features such as import statements, generating new classes... Eclipse always adds main.scala before com.test. Is there something I can do to solve this?
Edited
For example I have this:
[project]
src
main
scala
com
test
A.scala
package com.test
...
B.scala
package com.test
...
In B, I use some functions of A, I type it, then press Ctrl+Shift+O to let Eclipse import A. Eclipse does this:
import main.scala.com.test.A
But in A I set package to com.test (not main.scala.com.test).
This is small example. But when I have many source files, I need to refactor my code, things will be harder...
You are confusing source folders and packages!
sbt by convention uses the source folder src/main/scala for Scala code. Within this folder you can create whichever packages you want, e.g. a subfolder foo/bar with a file Baz.scala that contains the following code:
package foo.bar
object Baz {
...
}
If you use the eclipse plugin for sbt then you can tell sbt to generate an Eclipse project which has all the details configured to work correctly with sbt's project structure. All the details you need to add it to your sbt project and to run it are at the link above.
When building a web application SBT is able to collect all my jar dependencies into the WAR file.
Is this possible to have SBT put all the jars I depend on in my non-web application into a directory so I can easily put them onto my class path when running the app?
Yes, you can put something like this in your project definition class:
val libraryJarPath = outputPath / "lib"
def collectJarsTask = {
val jars = mainDependencies.libraries +++ mainDependencies.scalaJars
FileUtilities.copyFlat(jars.get, libraryJarPath, log)
}
lazy val collectJars = task { collectJarsTask; None } dependsOn(compile)
and run the task via collect-jars in your SBT console. This will copy the scala-library.jar and the jars used for compilation in a directory called lib in the same directory as your classes directory.
In my honest opinion don't bother with sbt-assembly. I'm new with scala but I'm quite technology agnostic, I handle a lot of technologies and sbt-assembly it's not clean. Just an opinion.
I would recommend you sbt-pack. Awesome piece of work. It will give you the runnable scripts as well, for both.. WINDOWS AND LINUX.
https://github.com/xerial/sbt-pack
You can use sbt-assembly to make a fat jar with all dependencies: https://github.com/sbt/sbt-assembly