I created a new SBT project HelloScala, then I created a package called week6 and then a Scala worksheet under it. Eventually, I got the following directory structure:
~/HelloScala/.idea
~/HelloScala/project
~/HelloScala/src/main/scala
~/HelloScala/src/main/scala-2.12/week6/hello.sc
However, when I put package week6 in the worksheet hello.sc, IntelliJ warns that Project names doesn't correspond to directories structure. My questions are:
What should be the correct directory structure IntelliJ/SBT expects? Is it specified in some .xml file?
Notice that IntelliJ created two scala subdirectories, scala and scala-2.12. Is it correct? Or IntelliJ somehow failed to recognize the scala which had already existed but download and install another scala?
Thank you!
I don not use IntelliJ that often, but usually Scala packages should go into src/main/scala.
The structure of a Scala project is nothing too different from a Java one. Minimally you will need:
myproject
├── build.sbt
└── src
└── main
└── scala
└── mypackage
which is a project with a single package, a SBT script and nothing else.
For a more complex example, a Scala project could look like:
project
├── build.sbt
├── project
├── target
└── src
├── main
│ ├── java
│ ├── resource
│ └── scala
└── test
├── java
├── resource
└── scala
in which the project folder is used by SBT for various purposes, the target folder contains the compiled .class files and .jar packages.
To answer your questions:
The above structure is used implicitly by the SBT plugin. Maybe it is customizable, but in most cases it is suggested against doing this.
Your Scala source code should go into src/main/scala. I have no idea where does the other directory come from.
#sgu It seems I misunderstood your question and unfortunately I cannot leave a comment. The file "hello.sc" you are trying to deal with is treated by IntelliJ as a "Scala worksheet". I guess it is a REPL behind IntelliJ which evaluates as you edit. However it is not treated the same as a "Scala source file", so adding package xxx gives you the warning. If you want to create a package, the source files should be in ".scala" extension.
You should put all your source code under "main/scala" directory. "Scala-2.12" is the directory, that would be created with sbt under "project/target" after you compile/build it.
Have you tried clicking the Make project option on top of the scala sheet in Intellij Idea? I use that option when I need to import oher packages into one scala worksheet.
Related
I'm learning bloc state management in flutter but I saw code here made me to ask some questions like ,
why they separated the repository as an external package, then use it in the bloc? . why didn't just create repository auth in the project lib folder like this
├── android
├── ios
├── lib
│ └── authentication_repository
└── test
and what is the benefit of this approach when should I use it in my future projects?
I am trying to get a main method to run in Scala in Intellij, but it is not working, and I don't know why.
File structure looks like this...
.
├── cse116.iml
├── pom.xml
├── src
│ ├── Main.scala
└── target
└── classes
Main.scala looks like this...
object Main {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
When I try to do "Run" on Intellij, I get the following error:
Error: Could not find or load main class Main
I am not sure what configuration is messed up. What may be some common reasons for this happening?
Try to right click on your mouse when you are in Main.scala file and select "run 'Main.main()'" option with green triangle
Assuming you are running a recent version of intelliJ, have the Scala plugin installed and your dependencies for Scala is correct in your pom file. You should see a tiny green arrow either on the left hand side of your main class or when right clicking within that class. Also, if you are on macOS there is the ctrl+shift+r to run the file as shown in the image below.
If that is not the case, consider creating a new Scala project from the IDE itself. You'd then choose an sbt project. sbt is the equivalent of maven in the Scala world. This one "should" work, if it doesn't it is very likely to give you a better Scala specific error message(s)
I'm sure I've seen a page that shows how to put SBT instructions inside a scala file, instead of build.sbt. The advantage is that everything then lives in a single file, which makes it easier to use Scala as a scripting language.
But I can't now find the example... can anyone help?
BTW, I know about ammonite, but it has limitations compared to SBT.
I think you're looking for this page of the sbt docs: Scripts, REPL, and Dependencies. There are also some other resources on the topic:
an example project by Eric Richardson with detailed instructions: ekrich/sbt-scripting
a post by Eugene Yokota (maintainer of sbt): scripting with Scala (it's from 2014, so I'm not sure if it's still relevant)
an example script gist by Seth Tisue
You can create your customize scala file under project/ directory and import it in build.sbt, like:
├── build.sbt
└── project
└── Settings.scala
so you can import it in build.sbt:
import Settings._
I am using scala typesafe config (version 1.2.1) in one of my projects to read the application.conf file, my project has dependency on multiple other projects and I create a jar with dependencies to run the dependency projects.
Problem - those projects also use typesafe and has application.conf files in top level directory and my maven jar with dependencies pick up only one application.conf in same classpath and drops rest of them (I tried using maven shade plugin to merge these conf files and I don't want to go that route). I am trying to place these application.conf files in packages, so they don't get overriden in jar with dependencies, but typesafe doesn't seem to recognize the files under a package name in resources directory, it can only find the conf file if placed under top level resources directory.
Is there a better solution to achieve this by using typesafe ? or I am open to using a totally different config library for scala if there is any.
Given a file structure like this:
$tree src/main/resources/
src/main/resources
└── path
└── to
└── file
└── xxx.conf
You can load the config with the following command:
val config = ConfigFactory.load("path/to/file/xxx.conf")
I have multiple maven projects with their individual pom.xml's an a parent pom.xml.
my_workspace
├── application-project
│ ├── pom.xml
│ └── src
│ ├── main
│ └── ...
│ └── test
│ └── ...
├── common-project
│ ├── pom.xml
│ └── src
│ ├── main
│ └── ...
│ └── test
│ └── ...
└── pom.xml
Now, from Eclipse, I can right click on common-project or application-project, I see Run As Maven... options there. But when I need to run a specific goal for all the projects, i.e. which is in the parent pom.xml, I don't understand how to go about it, if not using command line?
I guess you have appropriate plug-in, if not already having, install m2eclipse plugin first
Run > Run Configuration > Maven Build
Double click[*], choose the project, put down the goals, put down profile if any.
Fron next time use this (will be available in drop down) to execute your own goals.
[*] The form that opens here has all the capability to construct a maven command that you can run from command line. You might need to play around a bit to get what you actually wanted.
Updated as question was modified:
OK, so your parent project is your workspace folder. worry not.
Run > Run Configuration > Maven Build
Double click, browse filesystem and select the workspace folder, put down the goals, put down profile if any. I have just tested it. It works.
Yet another way, as OP found out, that you can click variables... button beneath the text-field for Base Directory and choose workspace_loc to point to your workspace directory as the base directory for Maven build.