Folders/packages in sbt ./project folder - scala

Looks strange to me, maybe i'm doing something wrong, but when i'm trying to launch sbt it can't find/compile files from folders/packages inside ./project folder, like:
root/
project/
deploy/DeployModule.scala
DeployConfig.scala
Build.scala
SBT can't resolve by build.deploy.DeployModule._ import, but if i move files from deploy folder into project folder it works. So it looks like SBT can't resolve files in nested folders inside project folder?

Sbt's meta build uses the default sbt build (with a few minor extras). As such, root-level .scala/.java files are picked up, but if you want things in sub-directories, you'll need to place them like so:
root/
project/
src/main/scala/deploy/
DeployModule.scala
DeployConfig.scala
Build.scala

Related

how to package and reference text file in resources folder

I have a spark project using scala and sbt. At one point it references a text file which I want to be packaged.
This is how it is referenced in the application source:
getClass.getResource("/myFile.txt")
This works fine running the source code with sbt run. But I want it to be packaged and deployed to a server.
In build.sbt, after some googling I have got this to work
import NativePackagerHelper._
mappings in Universal ++= directory("src/main/resources")
adding this meant that the myFile.txt appears in the resources folder in the package. created using
sbt universal:packageBin
resulting folder structure:
target - universal - bin
- lib
- resources
however when I run my packaged application from bin/my-application.bat , I get the following error
Exception in thread "main" org.apache.spark.sql.AnalysisException: Path does not exist: file:/C:/Software/my-application-0.0.1/lib/my-application-0.0.1.jar!/myFile.txt;
Bear in mind I have zero experience of deploying scala or jvm based things so you may have to spoonfeed me the explanation
EDIT I later realised that the text file was in fact included in the .jar file.
the issue then was that getResource does not work in this case and I had to adapt my code to use getResourceAsStream
This can have multiple reasons.
Include files in your resulting jar
You added this line, which is not correct
import NativePackagerHelper._
mappings in Universal ++= directory("src/main/resources")
The src/main/resources directory is the resourceDirectory in Compile and the contents are always present in the package jar file (not the zip!). So I would highly recommend removing this snippet as you will have your files twice in your classpath.
The mappings in Universal (documentation link) define the content of the created package (with universal:packageBin the zip file). I assume that you are using the JavaAppPackaging plugin, which configures your entire build. By default all dependencies and your actual build artifact end up in the libs folder. Start scripts are being place in bin.
The start scripts also create a valid classpath, which includes all libraries in lib and nothing else by default.
TL;DR You simply put your files in src/main/resources and they will be available on the classpath.
How to find files on the classpath
You posted this snippet
getClass.getResource("/myFile.txt")
This will lookup a file called myFile.txt in the roots of your classpath. As in the comment suggested you should open your application jar file and find a text file myFile.txt at the root, otherwise it won't be found.
hope that helps,
Muki

Nothing can be resolved in build.sbt and plugins.sbt

I have set up a Scala SBT project in Intellij.
However, nothing in build.sbt and plugins.sbt is resolved.
The project structure looks like this:
build.sbt for instance looks like this:
Why is this occuring?
I have seen IntelliJ choke on sbt projects and just drawing everything in red. I don't know what triggered that, but in those cases, I
quit IntelliJ
deleted the .idea project folder
created a fresh project from the sbt build
Then it always resolved this. In the latest version of the IntelliJ plugin, two projects are created per sbt build, like myname and myname-build. The second one is the sbt build if I'm not mistaken, and you must make sure that both are checked when importing the project in IntelliJ.

How to make sbt-native-packager refrain from putting my resources into a jar file?

I'm using the sbt-native-packager plugin to generate a start script for my application, which is very convenient as this plugin generates the correct classpath specification with all my library dependencies. I am not distributing this applictaion, therefore I'm not packaging the entire thing into one tarball. I just use the lib directory generated by sbt-native-packager that contains all the jar-files on which my project depends, both third-party libraries as well as the jar-file that contains my own class and resource files.
In my project's src/main/resources directory I have files that I want to be able to edit without having to use sbt-native-packager to regenerate the entire installation, for example configuration files. This is difficult because those files are zipped up in the jar file with all my classes.
Question: how can I tell sbt-native-packager not to put my resource files into a jar-file, while still generating the start-script with the correct classpath for those resource files to be located and read by my application as they are now from within the jar file? If this means leaving all my class files out of a jar file that is fine, as long as the files from src/main/resources remain as files that I can change without re-invoking sbt stage and as long as the start-script works.
While it is possible to filter these resources I would suggest to put them into a different directory and add them to the classpath.
Modifying the start script generated by sbt-native-packager is a bit cumbersome as the class com.typesafe.sbt.packager.archetypes.JavaAppBashScript that is generating the classpath is prefixing all paths with $lib_dir/. The cleanest approach would probably be to provide your own implementation and use that to generate the bashScriptDefines.
A simpler but hacky way would be to just add the following lines to your build.sbt:
packageArchetype.java_server
// add your config files to the classpath for running inside sbt
unmanagedClasspath in Compile += Attributed.blank(sourceDirectory.value/"main"/"config")
// map all files in src/main/config to config in the packaged app
mappings in Universal ++= {
val configDir = sourceDirectory.value/"main"/"config"
for {
file <- (configDir ** AllPassFilter).get
relative <- file.relativeTo(configDir.getParentFile)
mapping = file -> relative.getPath
} yield mapping
}
scriptClasspath ~= (cp => "../config" +: cp)
This will prepend $lib_dir/../config to your start script's classpath. If your app has to run on Windows you will have to provide similar settings for the batScriptDefines.

how to create project files in sbt

hi i am new in sbt i am following this tutorial
http://www.scala-sbt.org/0.13/tutorial/Hello.html
i followed the same steps on the shell the program displays "hi"
i am confused i don't have these files in my hello folder
Sources in src/main/scala or src/main/java
Tests in src/test/scala or src/test/java
Data files in src/main/resources or src/test/resources
jars in lib
and also i dont't have the build.sbt file i am following this tutorial as it is i have only hw.scala file and a target folder
mt scala version is 2.11.1 and sbt version is sbt 0.13.5
am i doing something wrong ?
Just create build.sbt and write appropriate lines into it. Same goes for mentioned directories -- stock sbt does not create files and folders for you, but it has to recognize them, once they're there.

(/home/user/.sbt) has been deprecated. Please use the standard location: /home/user/project?

When I start the sbt console I get this:
alex#alex-K43U:~$ sbt console [warn] Alternative project directory
.sbt (/home/alex/.sbt) has been deprecated since sbt 0.12.0. [warn]
Please use the standard location: /home/alex/project [info] Loading
project definition from /home/alex/.sbt [info] Set current project to
default-22b2b7 (in build file:/home/alex/)
I just started using scala and sbt, so I'm not really sure what the warning means. It means that I have to move all the content of /home/alex/.sbt to /home/alex/project?
(I have this folder too: /home/alex/sbt which has a bin folder and a jansi-license.txt file. I think that's how I installed sbt).
You can run sbt from any dedicated folder besides your home to get rid of this warning.
This warn appears because you run sbt from the directory that contains the .sbt folder, that in your case is /home/alex/.sbt
Create a folder (named project or something else) inside /home/alex/ and run sbt from there. Don't move the .sbt data inside the new folder because the warn will appear again.