Organizing files in a SBT-based scala project - scala

Newcomer to the Intellij IDE here, with no Java background. I've looked at Build Definition to get a brief idea on how should I organize my scala files, but their example doesn't cover the full structure of an SBT-based project shown attached.
Can you advise what each folder should be used for (e.g. where my source files should go, etc.) and also point me to sources where I can go read up more.
Thanks so much

It is described pretty well here:
http://www.scala-sbt.org/0.13.5/docs/Getting-Started/Directories.html
But to sum up.
.idea:
This contains the project files for your idea project, and has nothing directly to do with sbt itself. However idea (if auto refresh is enabled) updates its own project, each time the sbt build files change.
project:
This contains the sbt project files, except for the main build file (files ending in .sbt). Sbt build is itself based on scala, and if you need to have some scala code included in your build (e.g., code-generation/meta-programming, pre-compiler macros), then you can place scala source files in this directory. The code of these files can be used in your build system, and is not part of your project itself. To really understand how a build is made, then you will need to understand the difference in how sbt files and scala files for the build should be placed. When you run sbt, then it will search for .sbt files in the directory your are standing in, when these are found, it will search for scala files in the project directory. These files together are the source of the build system, but because these are source files, they need to be built before they can be used. To build this build system, sbt uses sbt. So a build system to build the build system is needed. It therefore looks for sbt files inside the project directory, and scala files for this build inside project/project and build these files to get a build system, that can build the build system (that can build your project). Actually it can continue recursive down to any project/project/project... directory, until it finds a project folder containing no scala files, and therefore needs no building before use.
The target folder inside project, is the target folder for the sbt build of your build definition. See below what a target folder is.
Normally you would not need to be concerned about this; just remember that build.sbt in your root directory is the build script for your project. project/plugins.sbt defines plugins activated for your build system, and project/build.properties contains special sbt properties. Currently the only sbt property I now of, is what version of sbt should be used.
src:
This is where your place the source files of your project. You should place any java sources in src/main/java, scala sources in src/main/scala. Resources are placed in src/main/resources.
The src/main/scala_2.11 folder is typically used, if you have some code that it not binary compatible with different versions of scala. In such cases you would be able to configure sbt to use different source files when building for different versions of scala. You probably do not need this, so I would advise to just delete the src/main/scala_2.11 folder.
Test sources are placed inside src/test/java and source/test/scala, and test resources are placed in src/test/resources.
target
This folder is the target folder for sbt. All compiled files, generated packages and so on are placed somewhere inside this dir.
Most things in this dir are not so interesting, as most of it is just internal sbt things. However if your build a jar file by calling sbt package, then it will be placed inside target/scala-x (where x is the scala version). There are also a lot of different plugins, that can package your application in different ways, and they will normally also place the package files somewhere inside the target dir.

Related

Include scala source files in sbt pack output jar

How can the scala source files of a project be included in the generated target jar produced by sbt pack?
Currently, when an IDE user of my jar tries to jump to a function in the library they will only get decompiled version of the code instead of the original source. However, other libraries pull from artifact repositories have the ability to jump to the original source code.
Thank you in advance for your consideration and response.
I think you can use:
packageSrc: Creates a jar file containing all main source files and
resources. The packaged paths are relative to src/main/scala and
src/main/resources. Similarly, test:packageSrc operates on test source
files and resources.
sbt Command Line Reference

sbt multi project: create resource in another sub-project

I have an sbt project with two sub-projects, A and B. A produces a standalone scala-based executable exe. When exe is run, it will produce a file out.xml. I want this file to be part of resources for project B. I do not want B to include any references to A's code, all I want is the out.xml file to be part of it. I suspect that http://www.scala-sbt.org/0.13.5/docs/Howto/generatefiles.html should be a good starting point, but I can't get my head around on how to split it between two projects. Any takers?
Since A is a dependency of the build process, which needs to run the executable to generate your xml file you would list it as a libraryDepencency in project/[something].sbt or project/project/[something].scala. This would make it available to code you put in build.sbt or project/[something].scala but not make it a transitive dependency of the resulting artifact of project B.
(Or you could of course make project A a sbt-plugin itself, or create yet another project which is a plugin depending on A that runs the executable.)

Copy file from sbt plugin to project folder

I'm writing an sbt plugin to help with deployment. It depends on sbt-native-packager. Principally it adds a deploy task. However, I also need it to copy a bash script run-class.sh into the /bin folder of the package.
How do I copy a file from the sbt plugin to my project? Presently my only idea is to add the file to src/main/resources/run-class.sh in the plugin and generate a file using sbt. Then I can supply a Universal mapping to put the file in the sbt-native-packager package.
Is there an easier way to get a file from the plugin into my sbt project?
You are on the right track with Generating files, specifically Generate resources. You can keep your original file either as a resource or String, but important thing is that the files are generated into resourceManaged in Compile, which is under target. This folder is typically skipped from version control.

Sbt's gen-idea always creates two IntelliJ projects

I am new to SBT. Just be curious that why does sbt's gen-idea always generate two IntelliJ projects:
.idea
.idea_modules
When I open the generated project, the "project" directory is always there as a separate project different with the top level project. The name is "myproject-build".
Just wondering whether this is normal?
Thanks.
Yes this is normal, this is the default behavior. You can change it by excluding some folders (see the doc available here : https://github.com/mpeltonen/sbt-idea at Exclude some folders)
So what is the difference between .idea and .idea_modules?
.idea_module generates an IDEA module while .idea generates an IDEA project.
In short a project can be multi-module or single-module and also contains IntelliJ libraries.
In longer version from the doc (http://confluence.jetbrains.com/display/IDEADEV/Structure+of+IntelliJ+IDEA+Project)
Project
In IntelliJ IDEA, a project encapsulates all your source code,
libraries, build instructions into a single organizational unit.
Everything you do in IntelliJ IDEA, is done within the context of a
project. A project defines some collections referred to as modules and
libraries. Depending on the logical and functional requirements to the
project, you can create a single-module or a multi-module project.
Module
A module is a discrete unit of functionality that can be run, tested,
and debugged independently. Modules includes such things as source
code, build scripts, unit tests, deployment descriptors, etc. In the
project, each module can use a specific SDK or inherit SDK defined on
the project level (see the SDK section later in this document). A
module can depend on other modules of the project.
Yes, it is ok for that SBT plugin for IDEA.
Usually IDEA project consists of top-level .idea directory (which contains configuration common to the project) and several *.iml files, one for each module in the project (module-specific configuration, like facets, excluded directories, custom dependencies). These files are usually located in the top-level directories of corresponding modules.
On the other hand, SBT plugin does something unusual. It creates standard .idea directory, but it stores all project modules in one location, namely .idea_modules directory in the top-level directory of the project. This is fully supported by IDEA project structure, which is a set of XMLs after all.
As for project directory/module, it is a standard feature of SBT builds. It contains your build configuration. See SBT manual on this.
The foregoing was about SBT plugin which is currently present in plugins repo. There is an official SBT plugin in active development which keeps familiar modules structure (no .idea_modules directory) and has higher integration with SBT. The latter is most prominent in dependency management - official plugin extracts dependencies, even unmanaged, and makes them available for the IDE; current SBT plugin cannot do that.

eclipse projects and compiled data

in my Java Eclipse project that contains JUnit tests, I also have a package "resource" that contains all input data used for the tests. But when compiling JUnit tests, the Java compile also data available in resources, so I find the same data in the "bin" folder. Is there a way to avoid this?
thanks.
If you have a particular package within the source path you want to exclude (your resources folder for example), you can right click on the package and select: Build Path > Exclude.
This will tell Eclipse that you don't want to include that package as part of the build.
This is making a couple of assumptions: that you're using Eclipse Helios (because the option might be different in older versions), and that the resources are stored in the same folder as your regular java source files (because if resources is in a folder by itself, you can remove that entire folder from the build by using Build Path > Configure Build Path -> Source tab.
Update:
After the discussion in the comments regarding why you would or would not want to copy resources into the bin directory:
The contents of your bin directory should be ignored and not checked into to a version control system (when using CVS, bin should be an entry in the .cvsignore file)
The resources are only duplicated on your local machine, which is fast and hard discs are big. I'm not sure you should be worrying about this
If you're using Class.getResource to access those resources, they need to be on the classpath somewhere. The bin directory is as good a place as any
So, realistically (barring some unknown, like the files are hundreds of gigabytes or something), I don't think you need to be concerned about excluding these files from the build.