Unauthorized sbt download dependencies - scala

Trying to download dependencies from our dependencies site (JFrog) using sbt.
However, getting unauthorized errors. My machine is using sbt 1.3.3 and scala 2.13.1
name := """backend"""
version := "1.0-SNAPSHOT"
topLevelDirectory := Some(packageName.value)
scalaVersion := "2.12.9"
val anormVersion = "2.5.3"
val silhouetteVersion = "6.0.0-RC1"
val silencerVersion = "1.3.2"
libraryDependencies ++= Seq(
"com.jayway.jsonpath" % "json-path" % "2.4.0"
)
I tried logging into JFrog in the browser with the same credentials and it works so not sure why sbt is having permission issues. I am using environment variables for sbt rather than a credentials file.

Related

Using IntelliJ, how to add dependency in an sbt project

I'm new to sbt and I wanted to learn it with a small Scala project in IntelliJ.
I started with the official sbt Getting Started guide, to learn the sbt basics on the console (https://www.scala-sbt.org/1.x/docs/sbt-by-example.html). Following the guide, everything compiles fine.
Then I created an sbt project in IntelliJ, trying to do the same thing there. When I add the org.scalatest depenpency to the build.sbt file the project can no longer compile. The error message is:
module not found: org.scalatest#scalatest_2.13;3.0.5
When I created the fresh sbt project in IntelliJ, first the build.sbt looked something like this:
name := "sbtTest"
version := "1.0"
scalaVersion := "2.13.0"
Then I added the dependency:
name := "sbtTest"
version := "1.0"
scalaVersion := "2.13.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
After reloading the build file and getting the error when compiling, I tried to change the biuld.sbt according to the code which already worked in the sbt Getting Started guide:
ThisBuild / scalaVersion := "2.13.0"
ThisBuild / organization := "me"
ThisBuild / version := "1.0"
lazy val sbtTest = (project in file("."))
.settings(
name := "sbtTest",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
)
This has again the same error after reloading and compiling.
My sbt version is 1.2.8.
Is something wrong with my build.sbt? Or is the sbt version too new for IntelliJ? Is IntelliJ probably not the recommended IDE to create Sala sbt projects?
Structure of both your build.sbt is ok. The thing seems to be in versions.
Scalatest 3.0.5 is currently unavailable for Scala 2.13.0. It's available for Scala 2.13.0-M2.
It's Scalatest 3.0.8 that is available for Scala 2.13.0.
https://mvnrepository.com/artifact/org.scalatest/scalatest
After you fix versions in build.sbt re-import IntelliJ project in pop-up window
or in sbt tool window
or in sbt shell

SBT multi module project: how to make static files (resources) available in main module?

I develop multi module SBT project. In general it's an akka api. It works well, when I run it locally and when I package it in docker.
Recently I added a new one module for email templates generation. I decided to use scalate mustache for this purpose. For a testing reason I created a simple template hello.mustache in email/src/main/resources/templates.
Then I run code which uses the template from the class located in email/src/main/scala. Everything compiled ok (scalate templates & scala code).
After I add a dependency to the email module to the security module which is included in app module:
import sbt.Keys._
import NativePackagerHelper._
lazy val core = project.in(file("core"))
.settings(name := "core")
.settings(Common.settings)
.settings(libraryDependencies ++= Dependencies.commonDependencies)
.enablePlugins(JavaAppPackaging)
lazy val email = project.in(file("email"))
.settings(name := "email")
.settings(Common.settings)
.settings(libraryDependencies ++= Dependencies.emailDependencies)
.enablePlugins(JavaAppPackaging)
lazy val contacts = project.in(file("contacts"))
.settings(name := "contacts")
.settings(Common.settings)
.dependsOn(core % "test->test;compile->compile")
.enablePlugins(JavaAppPackaging)
lazy val security = project.in(file("security"))
.settings(name := "security")
.settings(Common.settings)
.dependsOn(email, core % "test->test;compile->compile")
.enablePlugins(JavaAppPackaging)
lazy val app = project.in(file("."))
.enablePlugins(JavaAppPackaging, AshScriptPlugin, DockerPlugin)
.settings(name := "app")
.settings(Common.settings)
.dependsOn(core, security, contacts)
.settings(
mainClass in Compile := Some("com.app.Main"),
packageName in Docker := "app-backend",
version in Docker := "latest",
dockerBaseImage := "openjdk:8-jre-alpine",
dockerExposedPorts := Seq(5000)
)
I see the following errors, while trying to run the email code:
Exception in thread "main" org.fusesource.scalate.TemplateException: scala.tools.nsc.symtab.classfile.ClassfileParser$unpickler$.unpickle([BILscala/reflect/internal/Symbols$Symbol;Lscala/reflect/internal/Symbols$Symbol;Ljava/lang/String;)V
at org.fusesource.scalate.TemplateEngine.compileAndLoad(TemplateEngine.scala:886)
at org.fusesource.scalate.TemplateEngine.compileAndLoadEntry(TemplateEngine.scala:745)
...
How to make email module code work in another modules?
Additional info:
I try to run the code directly from IDE by run of the Main class from the app module.
Scala version 2.12.2; Scalate version 1.8.0; sbt version 0.13.8;
I'm afraid that you encountered a bin compatibility issues among several scala compiler versions. Explicitly overriding the scala lang version like this is preferred to avoid such problems.
dependencyOverrides := Set(
"org.scala-lang" % "scala-library" % scalaVersion.value,
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
"org.scala-lang" % "scala-compiler" % scalaVersion.value
),
In my particular case there was a problem in a conflict of log4j version of scalate and one other scala lib. So the solution which works for me is:
"org.scalatra.scalate" %% "scalate-core" % "1.8.0" excludeAll(ExclusionRule(organization = "org.slf4j"))

Play framework's sbt import maven module error

I am using Playframework 2.5.4 to serve as REST API endpoint in default sbt build, compile & run with activator.
This play module imports a core module in local maven (3.9), where types were defined. Such as:
package com.myCompany
case class User(
id: Option[UUID] = None,
username: String
)
Then play controller package will import com.myCompany.User.
Issue is, when activator compiles, it gives unknown parameter name: username
If I placed case class in play's models package, it compiles without error.
Below is my build.sbt
name := """api"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
filters,
"com.myCompany" % "core" % "1.0-SNAPSHOT"
)
routesGenerator := InjectedRoutesGenerator
resolvers ++= Seq(
"scalaz-bintray" at "http://dl.bintray.com/scalaz/releases",
Resolver.mavenLocal
// "Local Maven Repository" at "file:///home/pocheng/.m2/repository"
)
I suspect build.sbt file is having issue. Would appreciate any pointer. Thanks.

Install scalatest in Scala IDE for Eclipse

I have installed Eclipse Luna. Then I installed Scala IDE via Help -> Install new software and adding software site with link from here. Then I installed sbt 0.13 and sbteclipse using this mini-guide and created eclipse project. Then I installed (kindda) scalatest by adding it to my build.sbt. Now it looks like this:
val scalaTest = "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
lazy val commonSettings = Seq(
scalaVersion := "2.11.6"
)
lazy val root = (project in file(".")).
settings(commonSettings: _*).
settings(
libraryDependencies += scalaTest
)
Then I created a test from this example. The file called FirstSpec.scala is located in testProject/src/test/scala-2.11/testProject/. So here is a problem: eclipse seems to not see scalaTest. The second line with import org.scalatest._ is underlined red with error description object scalatest is not a member of package org. And, following this guide, I don't see the option Run As -> ScalaTest - Suite when choosing my test class.
At the same time everything goes good and fine when I start sbt session in my test project and type test command. The tests launches and passes.
So my questions are:
why eclipse doesn't see the scalatest if I put it in build.sbt's libraryDependencies? What's the point of libraryDependencies then?
Why sbt test runs the tests without a problem? If sbt sees scalatest, why eclipse can't?
Whew, this one resolved my issue. So, build.sbt example might go something like:
import com.typesafe.sbteclipse.plugin.EclipsePlugin._
EclipseKeys.withSource := true
val scalaTest = "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
val jodaConvert = "org.joda" % "joda-convert" % "1.7"
val joda = "joda-time" % "joda-time" % "2.7"
lazy val commonSettings = Seq(
scalaVersion := "2.11.6"
)
lazy val root = (project in file(".")).
settings(commonSettings: _*).
settings(
libraryDependencies += scalaTest
).
settings(
libraryDependencies += jodaConvert
).
settings(
libraryDependencies += joda
)
Then do this:
rm -rf ~/.ivy2/cache/
sbt update-classifiers
sbt eclipse

How to run playframework sample in Intelllij 13?

I am using Intellij 13 Ultimate and want to create a Play Framework sample. But I am unable to build this project because it always throws this error:
object index is not a member of package views.html
Ok(views.html.index("Your new application is ready."))
I have tried this on both Mac and Windows platforms, but the error is always the same.
How can I solve this?
The generation works just fine and all the paths are correctly added to the build. However the routes are not (yet) compiled by the plugins (scala+playframework), thus the reverse routing classes generated by play are not available for intellij.
You will have the same problem with the templates.
If you run a play compile (or a sbt compile), the classes will be generated and intellij should be able to pick them up and compile your project.
I found a trick here.
If you generate the play-scala project using activator command line activator [new my_first_project play-scala]. You'll get the following sbt build file.
name := """my_first_project"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
)
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
But IF you create a project from intellj using NewProject->Scala-Play 2.x, you will get the following sbt.
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
after combine them. Ignore the name. And I set for in run to false
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
After doing that. Open the activator-generated project with Intellj, configure a run with Play 2.x run. Everything goes well.
By the way, if you open the activator-generated project before you change the sbt file. You might need to rm -r .idea
Hope that helps.
Running "play idea" in the new project root fixed it for me. The project should compile and run after reloading.
I only have this problem with projects created through Idea's New Project menu.
I have the same problem: in Idea 13 there are highlight errors there, but in Idea 12 everything is ok. The reason is that there is no Scala plugin for Idea 13, so for now it's impossible to get it to work in Idea 13.
The solution is to install Idea 12, go to Preferences -> Plugins, type "Scala" in find box, and install Scala plugin.