Configuring Coffeescript SBT in Build.scala not build.sbt? - scala

Often I come across some instructions which tell me how to add a SBT tool to build.sbt, but actually I have a Build.scala, not a build.sbt. So I want to know how to do the same in my Build.scala?
The particular case that is causing me trouble is Coffeescript SBT which has instructions for how to add it to a build.sbt. However I don't have a built.sbt, I have a Build.scala, so I don't know what to do.
The code referenced here also helps to solve this problem.

Two options:
Go ahead and create build.sbt with the line given in the CoffeeScript instructions. It can coexist with Build.scala.
In Build.scala, find the "settings" line within the project and add ++ coffeeSettings to it. You may also need import coffeescript.Plugin.coffeeSettings at the top.

Generally I still use plugins.sbt in projects that use Build.scala for their main configuration.
Just add project/plugins.sbt with the resolver and addSbtPlugin lines.you need.

Related

Scala "not found: object com" - should i really add entry in build.sbt if there are no other dependencies?

I have created basic Scala Play application with https://www.playframework.com/getting-started play-scala-seed. This project compiles and runs with sbt run. But I have another Scala project that compiles and runs and which I have submitted to my local Ivy repository with command sbt publishLocal. This other project was saved at C:\Users\tomr\.ivy2\local\com.agiintelligence\scala-isabelle_2.13\master-SNAPSHOT as a result of this command.
Then I imported (exactly so - imported, no just opened) my Play project in IntelliJ and I used Project - Open Module Settings - Project Settings - Libraries to add com.agiintelligence jar from my ivy2 location. After such operations IntelliJ editor recognizes com.agiintelligence classes. That is fine.
But when I am trying to run my Play application with sbt run, I experience the error message not found: object com that is exactly when compiling import com.agiintelligence line in my Scala controller file of Play application.
Of course - such error has been reported and resolved with, e.g. object play not found in scala application
But that solution suggests to append build.sbt file. My build.sbt file is pretty bare:
name := """agiintelligence"""
organization := "com.agiintelligence"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.5"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.skaraintelligence.controllers._"
// Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.skaraintelligence.binders._"
My Play application contains (as can bee seen from the IntelliJ project pane) some tens of 'external libraries' (it shows my com.agiintelligence jar as well), but why should I add my own ivy2 library in build.sbt file if no other libraries are listed here? What is different with my library? It is on my computer, in the repository as expected already?
Of course, I can try to add it build.sbt and issue sbt update and see what happens, but I can not understand this logic? Can someone explain it and provide some clue to intelligible solution of my error message?
My Play application contains (as can bee seen from the IntelliJ project pane) some tens of 'external libraries'
Those are probably just transitive dependencies of your Play dependency, that is why sbt downloaded all of them and put them in your classpath so you could use them without you needing to tell it about them; because the pom of Play already did.
It is not that the build tool or the IDE magically added all those dependencies for you because they read your mind and magically understood you wanted them. And that for some reason the magic stopped working for your own library.
Why it is not sufficient to list it Project-Setting--External Libraries in IntelliJ only?
That is sufficient for the IDE to work, but not for the build tool. The build tool is independent of the IDE; it doesn't know about it. sbt just knows about the dependencies you configured in your definition file.
Even more, you should always configure your dependencies on your build tool and then import that in the IDE; rather than the opposite. IDEs are graphical tools, so their state can not be committed, can not be shared, can not keep track of changes, can not be used in CI / CD environments; additionally, different teammates may want to use different IDEs.
I resolved the error message by adding line in build.sbt file
libraryDependencies += "de.unruh" %% "scala-isabelle" % "master-SNAPSHOT"
and by subsequent run of sbt update.
Error is solved, but the main question still stand - why I had to do this? Why there are tens of dependencies that are not listed in build.sbt and why should I list my dependency in build.sbt and why it is not sufficient to list it Project-Setting--External Libraries in IntelliJ only?
OK, comment by #Luis_Miguel_Mejía_Suárez gave the explanation, that comment is the actual and expected answer to my question.

How to use SBT's externalPom() command

I have a Maven POM file that the deployment engineers need to deploy the system in the enterprise. I have developers using SBT for a Scala project. They use SBT targets that just aren't supported in Maven. We'd like to use the Maven POM file to define the dependencies, slurp in those dependencies in SBT, and define SBT development targets there.
According to the SBT documentation, the externalPom() command is the way to do that. But even with the simplest POM file (two developers have tried this with two different simple POM files that defined different dependencies), the externalPom() command seems to half work. The SBT targets clearly recognize the dependency defined in the POM, but can't resolve it. This error arises:
Cannot add dependency 'commons-collections#commons-collections;3.2.2'
to configuration 'default' of module
default#maven-sbt$sources_javadoc;0.1-SNAPSHOT because this
configuration doesn't exist!
When the externalPom() command is commented out and the equivalent dependency added directly in the build.sbt file everything goes swimmingly. The dependency comes directly from Maven Central in both cases; one from copying the dependency from the Maven tab and one from copying the dependency from the SBT tab. Once again, two developers are seeing exactly the same thing, from two different dependencies. The thing that's common is that both developers have reduced the build.sbt file down to a single statement. In the "slurp from POM" case, that statement is externalPom(). In the "plain old SBT" case, that statement is the dependency copied from Maven Central. The POM file is a dependency list with a single dependency (as simple as we can make it and still test externalPom()).
We suspect that we need something else in the build.sbt to make the externalPom() command work but we don't know what it is. Any help with that would be greatly appreciated.
I did some experimentation with this, and was able to replicate your error in my experiments.
I'm still a bit of a Scala / SBT newbie, but I created a build.sbt file that looks like so:
val Default = config("default")
lazy val root = (project in file(".")).
configs(Default).
settings(
externalPom()
)
This did compile for me!
One non-obvious catch: I had to make sure to include the scala-library in my POM file as a dependency

Testing the build.sbt file

I've recently encountered several cases where I added new dependencies to my project which brought some other transitive dependencies which caused my SBT package to fail
I was wondering if there's any way to automatically prevent issues likes this from happening?
Several ideas I had in mind but not sure if they can be done:
Write tests for the build.sbt so I can check the libraryDependencies and verify I exclude the correct transitive dependencies
Import scala code from the project and use it inside my build.sbt - this way I can at least be sure my code is tested and verifies specific dependencies are omitted
Is any of the options I mentioned possible? If not, is there another more common way to achieve what I want? Or should I just bite the bullet?
Thanks

How do I execute Scrooge from commandline?

I was able to include scrooge in my SBT project (the scrooge-sbt-plugin in my plugins.sbt as well as the library dependencies in my build.sbt), but I haven't been able to figure out how to execute scrooge from the commandline as listed here http://twitter.github.io/scrooge/CommandLine.html.
A bit late to the party.
#partycoder was indeed right, however a bit more may help those
who like myself aren't too sure.
Assuming your *.thrift files are located in src/main/thrift simply running sbt scrooge-gen will pick up the files and deposit them in target/src_managed/.
If your *.thrift files are not located in src/main/thrift and perhaps in src/main/resources/thrift, you can setscroogeThriftSourceFolder in you build.sbt using this example:
scroogeThriftSourceFolder in Compile <<= baseDirectory {
base => base / "src/main/resources/thrift/"
}
This setting and others can be found here.

How to declare a project dependency in SBT 0.10?

I use SBT 0.10.0.
How do I download/retrieve project dependencies?
For example, for slf4s only this line is mentioned:
val slf4s = "com.weiglewilczek.slf4s" %% "slf4s" % "1.0.6
Where do I need to put this line, and how do I get the library?
I presume you're using SBT 0.10.0, because earlier versions will put your deps in lib_managed automatically.
In build.sbt, put the following line:
retrieveManaged := true
You create a project/build subdirectory in your project and put a scala file with the above content there.
Then when you start sbt from your project root directory the
update
command will retrieve your dependencies.
Note that it will only analyse your project configuration once by default. If you change it, you have to call reload
UPDATE:
let the project class extend DefaultProject:
class SomeProjectName(info: ProjectInfo) extends DefaultProject(info)
I don't know which version of sbt you are using.
For 0.10, Daniel C. Sobral made a blog post about creation of an sbt project:
dcsobral-project-creation-guide
Maybe this helps.