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

512 Views Asked by At

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;

2

There are 2 best solutions below

0
On

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"))
0
On

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
),