Setting javac options for SBT dependencies - scala

I am having problems compiling a Java dependency loaded via GIT:
object ApplicationBuild extends Build {
lazy val project = Project("root", file(".")).dependsOn(RootProject(riakJavaClient))
lazy val riakJavaClient = uri("git://github.com/basho/riak-java-client")
}
The error I am receiving from sbt compile is:
[info] Compiling 134 Java sources to /Users/lawrencewagerfield/.sbt/0.13/staging/da0e66c4764a467c8977/riak-java-client/target/scala-2.10/classes...
[error] /Users/lawrencewagerfield/.sbt/0.13/staging/da0e66c4764a467c8977/riak-java-client/src/main/java/com/basho/riak/client/cap/Quorum.java:22: error: unmappable character for encoding ASCII
[error] * Riak 0.12 introduced ???symbolic??? consistency options for R and W
SBT seems to be executing javac with an encoding that is incompatible with the source files in this dependency.
I have tried adding the following to build.sbt, but it is having no effect (error is the same):
javacOptions ++= Seq("-encoding", "UTF-16") // Note: I have tried with UTF-8 too
Does the above only apply to source files within my project? Any idea how to get pass this issue?
TL;DR How do I get my Java dependencies compiling with the correct encoding?

You are correct that the setting only applies to source files in your project. If the project part of the scope isn't specified, which is typical, it defaults to the enclosing project. To have a setting apply to another project, scope it to that project. For example,
javacOptions in riakJavaClient ++= Seq("-encoding", "UTF-8")
You can verify that your options are being used with last. For example,
sbt> last compile
To run commands like the above on a project from git, change to it using project (see help project for details).

Related

How to exclude classes from compiling when using executing sbt docker:publishLocal?

My build.sbt file has some configuration to use AkkaGrpcPlugin and DockerPlugin because I am publishing the image at docker hub.
lazy val akkaGrpcVersion = "1.0.2"
lazy val protobufVersion = "3.11.4"
enablePlugins(JavaAppPackaging, JavaServerAppPackaging, AkkaGrpcPlugin, DockerPlugin)
akkaGrpcGeneratedLanguages := Seq(AkkaGrpc.Java)
libraryDependencies ++= Seq(
......
)
dockerUsername := Some("felipeogutierrez")
The sbt compile and sbt run work just fine, but the command sbt docker:publishLocal is not working because it tries to find some classes created by the gRPC in the target directory.
[error] /home/felipe/workspace-idea/explore-akka/target/scala-2.12/
akka-grpc/main/org/github/felipegutierrez/explore/akka/rpc/greeting/HelloRequest.java:29:7:
not found: type UnusedPrivateParameter
[error] UnusedPrivateParameter unused) {
[error] ^
these classes belong to the classes at package org.github.felipegutierrez.explore.akka.rpc.greeting and I would like to exclude them from the docker image when I am running sbt docker:publishLocal. I tried this solution but it didn't work. Or find some solution to make this work.
The problem was that I generated the Java files using AkkaGrpcPlugin and they were placed on the target directory of my project. So the docker compiler could not see it. I removed the AkkaGrpcPlugin and the akkaGrpcGeneratedLanguages := Seq(AkkaGrpc.Java) from the build.sbt and installed the protobuf compiler to generate the Java files by myself.
sudo apt install protobuf-compiler
protoc --java_out=main/java main/protobuf/helloworld.proto
Then the files are now generated on the src directory and Docker can see them. So, I don't have to exclude the files anymore in the build.sbt.

Idea SBT support: cannot resolve symbol in vanilla project

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

Play 2.3 run error

I'm experiencing a problem when running latest play framework 2.3.
It compiles just fine, although when I do activator run this error happens:
java.lang.NoSuchMethodError: scala.Predef$.ArrowAssoc(Ljava/lang/Object;)Ljava/lang/Object;
Full error log
I explicitly tried scalaVersion in every build.sbt file and it is the same.
I tried several things like activator clean, full removal os sbt caches and local repo sbt stuff, updating dependencies to latest version but no success.
I have scala version defined.
My current dependencies are:
I tried with both %% and force _2.11 in the name of the dependency.
Dependency List
Other important files
build.sbt
Common.scala
Dependencies.scala
When I fully clean caches it downloads scala 2.10.4 for no reason:
in this download log it says the sbt need the old scala.
Any idea why is this?
Any ideas?
Firstly, it does not matter which version of Scala is used to generate your build. The build system can use version 2.10.4, and that does not prevent your code from using version 2.11.1.
To look at the issue with your scala version, you should consider that settings added directly in build.sbt are added to the root project, but not to other projects. You can see this with a minimal project such as:
$ cat build.sbt
scalaVersion := "2.11.1"
lazy val subproj = project in (file("subproj"))
Then the output of sbt looks like this:
> show scalaVersion
[info] subproj/*:scalaVersion
[info] 2.10.4
[info] sbttest/*:scalaVersion
[info] 2.11.1
So, how can this be fixed?
We can create a lazy val containing a Seq[Setting[_]], and add it to the sub-project definition with the settings() method.
Here's an example build.sbt which adds the same settings to all projects:
$ cat build.sbt
lazy val root = project.in(file("."))
.aggregate(subproj)
.dependsOn(subproj)
.settings(commonSettings: _*)
lazy val subproj = project.in(file("subproj"))
.settings(commonSettings: _*)
lazy val commonSettings = Seq(
scalaVersion := "2.11.1"
)
The _* is required because settings() is defined as requiring Setting[_]* rather than Seq[Setting[_]], so _* converts between the two types. See also: What does param: _* mean in Scala?
And the output from sbt is:
> show scalaVersion
[info] subproj/*:scalaVersion
[info] 2.11.1
[info] root/*:scalaVersion
[info] 2.11.1
This approach can easily be applied to your build.
You're defining your dependencies in the wrong way.
When using SBT, don't use:
"org.mydep" % "mydep_2.11" % "1.0.0"
Instead use:
"org.mydep" %% "mydep" % "1.0.0"
The %% operator automatically appends the current Scala version of the current build. If you use dependencies built against other Scala versions, you're going to be getting the weird errors.
Also make sure you explicitly specify your Scala version somewhere:
scalaVersion := "2.11.1"
The problem was related to some manually added jars that I discovered in the lib folder, that were causing issues with the dependencies of the project defined in the build.sbt.
The only way to find out was to generate a activator dist and look for similar dependencies with different versions since in the dependency graph there were no conflicts

How to change Scala version for build definition?

I'm developing a simple SBT project that includes InputTasks for benchmarking Scala Parallel collections.
I have defined the InputKeys and started writing the tasks when I encountered a problem.
Since my benchmarks require Scala 2.10.0-M5, I tried doing this in my build.sbt:
name := "scala-parallel-collection-benchmark"
version := "1.0.0"
organization := "com.google.summer"
scalaVersion := "2.10.0-M5"
However, at compilation I get the following error:
[info] Loading project definition from C:\Users\Administrator\scala-parallel-collection-benchmark\project
[info] Compiling 1 Scala source to C:\Users\Administrator\scala-parallel-collection-benchmark\project\target\scala-2.9.1\sbt-0.11.3\classes...
[error] C:\Users\Administrator\scala-parallel-collection-benchmark\project\Build.scala:47: value tasksupport is not a member of scala.collection.parallel.mutable.ParArray[Int]
[error] collection.tasksupport = new ForkJoinTaskSupport(new ForkJoinPool(par))
[error] ^
[error] one error found
[error] {file:/C:/Users/Administrator/scala-parallel-collection-benchmark/project/}default-e0b2a2/compile:compile: Compilation failed
It appears that it still uses Scala 2.9.1 to compile it.
How can I set up SBT so it compiles my code using Scala 2.10.0-M5?
scalaVersion only impacts the version of Scala used to compile the "actual" source code (usually located in src/...). Your error comes from a part of the build definition (under project/), which is always compiled with the Scala version that sbt was built with.
You can't modify the version of Scala that is used to to compile the project definition, because it must be a version binary compatible with the version used to compile SBT itself. There's some flexibility being studied in this regard, but, right now, it's fixed.
The setting scalaVersion will change the version of Scala used to compile the project itself. The project can be compiled with completely different versions and, in fact, you can even have SBT compile your project with multiple Scala versions.
you can change the scala version in file "project/build.properties"
e.g. sbt.version=0.11.2

Is it possible to reference a project with a different SBT version?

I have my project (just for experiments with sbt) which is based on sbt 0.10. And another one which I want to use as a dependency. It is sbt 0.7 based.
Currently I'm trying to include the second it into my project project using uri reference. But the build is failing, probably because of incompatible versions of sbt in these projects. The error message is:
[info] Compiling 1 Scala source to /home/zan/.sbt/staging/113d72bca54918c1f033/project/plugins/target/scala-2.8.1.final/classes...
[error] /home/zan/.sbt/staging/113d72bca54918c1f033/project/plugins/Plugins.scala:1: not found: value sbt
[error] import sbt._
[error] ^
[error] /home/zan/.sbt/staging/113d72bca54918c1f033/project/plugins/Plugins.scala:3: too many arguments for constructor Object: ()java.lang.Object
[error] class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
[error] ^
... and so on.
Can I somehow reference the second project so it will be possible to bild and/or run my project with just one command?
SBT cross-project references are only supported if both projects use SBT 0.10. But you can use publish and artifact and depend on this from the downstream project through Ivy.
Issue the publish-local command in the first project to package the code in a JAR and write it to ~/.ivy2/local/org.abc.def/....
In the SBT 0.10 project, add this setting:
libraryDependencies += "org.abc" %% "def" % "0.1"