Idea SBT support: cannot resolve symbol in vanilla project - scala

I am using Intellij UE 2017.3. The steps that I undertook were:
Create a new project from Lightbend templates
Check import sbt sources (tried without as well)
Try the suggested solutions from this thread
As a result in my build.sbt nothing seems to be imported, regardless of whether before or after trying the suggested fixes, (even the Dependencies object in /project folder).
Here is Dependencies object contents:
import sbt._
object Dependencies {
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
}
I attach below screenshot with the errors and project structure. Note that in External Libraries scalatest version is different from scalaVersion, but former is correctly imported in Dependencies object.
The errors that appear are:
for Dependencies: Cannot resolve symbol
for settings: Cannot resolve reference settings with such signature, Cannot resolve symbol settings
for List: Type mismatch: expected: Def.SettingsDefinition,
actual Seq[Def.Setting[_]]
for name and libraryDependencies: too many
arguments for method settings
sbt.version is 1.1.1

Related

sbt: set the base-directory of a remote RootProject

Disclaimer: I am new to sbt and Scala so I might be missing obvious things.
My objective here is to use the Scala compiler as a library from my main project. I was initially doing that by manually placing the scala jars in a libs directory in my project and then including that dir in my classpath. Note that at the time I wasn't using sbt. Now, I want to use sbt and also download the scala sources from github, build the scala jars and then build my project. I start by creating 2 directories: myProject and myProject/project. I then create the following 4 files:
The sbt version file:
// File 1: project/build.properties
sbt.version=0.13.17
The plugins file (not relevant to this question):
// File 2: project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.7.0")
The build.sbt file:
// File 3: build.sbt
lazy val root = (project in file(".")).
settings(
inThisBuild(List(
organization := "me",
scalaVersion := "2.11.12",
version := "0.1.0-SNAPSHOT"
)),
name := "a name"
).dependsOn(ScalaDep)
lazy val ScalaDep = RootProject(uri("https://github.com/scala/scala.git"))
My source file:
// File 4: Test.scala
import scala.tools.nsc.MainClass
object Test extends App {
println("Hello World !")
}
If I run sbt inside myProject then sbt will download the scala sources from github and then try to compile them. The problem is that the base-directory is still myProject. This means that if the scala sbt source files refer to something that is in the scala base-directory they won't find it. For example, the scala/project/VersionUtil.scala file tries to open the scala/versions.properties file that lies in the scala base-directory.
Question: How can I set sbt to download a github repo and then build it using that project's base-directory instead of mine's (by that I mean the base-directory of myProject in the above example) ??
Hope that makes sense.
I would really appreciate any feedback on this.
Thanks in advance !
In the Scala ecosystem you usually depend on binary artifacts (libraries) that are published in Maven or Ivy repositories. Virtually all Scala projects publish binaries, including the compiler. So all you have to do is add the line below to your project settings:
libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value
dependsOn is used for dependencies between sub-projects in the same build.
For browsing sources you could use an IDE. IntelliJ IDEA can readily import Sbt projects and download/attach sources for library dependencies. Eclipse has an Sbt plugin that does the same. Ensime also, etc. Or just git clone the repository.

Play Soap Client

I am reading this article
https://playframework.github.io/play-soap/SbtWsdl.html
and based on this. I wrote the following build.sbt file
name := "PlaySOAPClient"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
"com.typesafe.play" % "play-soap-client_2.11" % "1.1.3"
)
WsdlKeys.packageName := Some("com.foo")
WsdlKeys.wsdlTasks in Compile := Seq(
WsdlKeys.WsdlTask((sourceDirectory.value / "main" / "wsdl" / "foo.wsdl").toURI.toURL)
)
and plugins.sbt
resolvers += Resolver.url("play-sbt-plugins", url("https://dl.bintray.com/playframework/sbt-plugin-releases/"))(Resolver.ivyStylePatterns)
addSbtPlugin("com.typesafe.sbt" % "sbt-play-soap" % "1.1.3")
When I do sbt compile the plugin does generate some code. but that code does not compile
Error:scalac: missing or invalid dependency detected while loading class file 'PlaySoapClient.class'.
Could not access type Configuration in value play.api,
because it (or its dependencies) are missing. Check your build definition for
missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
A full rebuild may help if 'PlaySoapClient.class' was compiled against an incompatible version of play.api.
Warning:scalac: Class javax.inject.Singleton not found - continuing with a stub.
/Users/User/IdeaProjects/PlaySOAPClient/target/scala-2.11/wsdl/main/sources/com/foo/webservices/FooWebService.scala
Error:(13, 8) object inject is not a member of package javax
#javax.inject.Singleton
Error:(14, 107) object api is not a member of package play
class FooWebService #javax.inject.Inject() (apacheCxfBus: play.soap.ApacheCxfBus, configuration: play.api.Configuration) extends play.soap.PlaySoapClient(apacheCxfBus, configuration) {
Error:(14, 32) object inject is not a member of package javax
class FooWebService #javax.inject.Inject() (apacheCxfBus: play.soap.ApacheCxfBus, configuration: play.api.Configuration) extends play.soap.PlaySoapClient(apacheCxfBus, configuration) {
Does anyone have an idea of what dependencies are missing. Note that this is a client side application only using this library to make a soap call. I don't want any server side dependencies of play framework.
My hope is that I will be able to use the play-soap as a standalone library in my console application to make soap calls.
Add dependency to build.sbt
libraryDependencies += "com.typesafe.play" %% "play" % "2.6.7" intransitive()
Then sbt compile should work (after sbt update).
Honestly, including the entire Play Framework into the application purely for the sake of WSDL client seems to be too much. All you need is to generate annotated Java beans and make a dependency just on them. And you can actually do that with common tools i.e. using Java's wsimport and sbt tasks to wrap it around.
Consider the following bootstrap template for this: https://github.com/sainnr/sbt-scala-wsdl-template. It generates all the boilerplate in-flight, compiles before the main sbt project and eliminates the need to commit this boilerplate Java code to your pristine Scala repo. If you notice, it doesn't even require a complete application server, just throw in some JavaEE-ish libs like rt.jar or its alternatives. Hope that helps someone.

read application.conf from build.sbt

I found a lot of similar questions, but some of them are unanswered and others don't work for me.
My task is to read application.conf from build.sbt, to keep all configuration in a single place. But my build.sbt with code
import com.typesafe.config._
val conf = com.typesafe.config.ConfigFactory.parseFile(new File("conf/application.conf")).resolve()
version := conf.getString("app.version")
don't compile with error
error: object typesafe is not a member of package com
How to fix this problem?
To make this work it is important to know that sbt projects have a recursive structure, so when you run sbt you actually start a project that has as root folder the project folder within your root project.
Given that, to fix your problem you need to add typesafe-config as a library dependency also for you sbt project; in order to do that add a build.sbt in the project folder of your root project and specify the dependency in there as you would normally do (i.e. libraryDependencies += ...).
Use this in your code
import com.typesafe.config.{Config, ConfigFactory}
private val config = ConfigFactory.load()
and in your build.sbt file
libraryDependencies += "com.typesafe" % "config" % "1.3.0"

Trouble adding Mapper as dependency in a Lift project

I am doing my first Lift project and want to add a database. Following a book, I added the following dependency to build.sbt:
"net.liftweb" %% "lift-mapper" % liftVersion % "compile",
And then, in Boot.scala, the import
import net.liftweb.mapper._
Now the project doesn't compile, with Boot.scala giving the error
object mapper is not a member of package net.liftweb
But other sources around the Internet seem to suggest that my imports are OK.
Where does the dependency problem come from?
It turned out that this is a problem of Eclipse, which cannot setup the dependencies correctly.
I had to close the project in Eclipse and delete the hidden files .classpath and .project. Then get into SBT and run
eclipse
(the project has to use the sbteclipse plugin as described in the Lift cookbook). This recreates the project files with the correct dependencies.
Afterwards, Eclipse compiled the code as expected. It seems this will be needed every time a change is made to build.sbt.

sbt direct git source dependency - not fetching transitive library dependencies?

I'm trying out sbt's direct dependsOn feature with a git repository ("project A") hosted at Github. I am using a stable tag reference, and in my test project ("project B"), sbt does clone project A from source and starts compiling. However compilation fails with project A's own dependencies seemingly missing (i.e. it doesn't seem to pick up anything defined in project A's build.sbt).
Is this a different from maven/ivy managed dependencies? Do I need to include all the transitive dependencies in my child project B? Sounds a bit weird to me. That would kind of kill off the whole effort, as I'm having like a dozen libraries on which project A depends.
To illustrate:
Project A (online on Github as source):
// build.sbt:
version := "1.2.3"
libraryDependencies += "org.foo" %% "bar" % "1.0"
Project B (local):
// project/Build.scala
import sbt._
import Keys._
object Build extends sbt.Build {
lazy val projA = RootProject(uri("git://github.com/me/projA.git#v1.2.3"))
lazy val projB = Project(id = "project-B", base = file(".").dependsOn(projA)
}
This goes:
[info] Compiling 678 Scala sources to /Users/me/.sbt/staging/
5666eafa865fdf605be3/target/scala-2.10/classes...
[error] /Users/me/.sbt/staging/5666eafa865fdf605be3/src/main/scala/com/me/
BarKeeper.scala:3: not found: object bar
[error] import org.foo.bar
[error] ^
So do I have to re-declare the library dependency on "org.foo" %% "bar" % "1.0"? I hope not!
This was purely my own fault, not sbt's. I had overseen an unmanaged library (folder lib) in project A. After exchanging it for a Maven managed version (folder lib_managed), project A now correctly compiles from source in the staging for project B.