I have a question about seemingly unnecessary recompilations by SBT. I have the following scenario: I’m running SBT inside a docker container, by attaching the volume with my application source code to the container, and launching the container with sbt as entry point. If I continuously run SBT inside that container, it doesn’t recompile whole app, which is good.
However, if I start SBT natively on OS X, it does the full recompilation. If after that I start it again inside docker, it again does the full recompilation. It takes very long, and is really annoying. What could be the reason for such behavior?
Here's how I launch the SBT in the container:
docker run --name=bla -it --net=host -v /Users/me/.ivy2:/tmp/.ivy2 \
-v /Users/me/.aws/config:/root/.aws/config \
-v /Users/me/.sbt:/root/.sbt \
-v /Users/me/projects/myapp:/src 01ac0b888527 \
/bin/sh -c 'sbt -Dsbt.ivy.home=/tmp/.ivy2 -Divy.home=/tmp/.ivy2 -jvm-debug 5005 -mem 3072'
My Java, Scala and SBT versions are the same on the host and in the container. Concretely: Scala 2.11.8, Java 1.8.0_77, SBT 0.13.11
Okay, after a day of debugging I've found the way around this problem.
SBT invalidates the compiled classes mainly based on the following rules:
Canonical path
Last modification date
That is, paths and modification dates have to be exactly same for
Source code files
Ivy dependencies jars
JRE's jars
First 2 points are quite easy to achieve, because it's just mapping of docker volumes. The crucial thing is to map to exactly same path as on the host machine. For example, if you work on OS X as I do, path your project sources probably looks like this: /Users/<username>/projects/bla, so in your docker run command you have to do something like:
docker run ... -v /Users/<username>/projects/bla:/Users/<username>/projects/bla ...
You don't care about timestamps for sources and ivy jars, because they will be exactly same (it's the same files).
Where you have to care about timestamps is the JRE stuff. I build the docker image with JRE baked in (using sbt-docker plugin), so I ended up reading modification date of local JRE libs and setting same dates inside the image:
new mutable.Dockerfile {
...
val hostJreTimestamp = new Date(new File(javaHome + "/jre/lib/rt.jar").lastModified()).toString
val hostJceTimestamp = new Date(new File(javaHome + "/jre/lib/jce.jar").lastModified()).toString
runRaw(s"""touch -d "$hostJreTimestamp" $javaHome/jre/lib/rt.jar""")
runRaw(s"""touch -d "$hostJceTimestamp" $javaHome/jre/lib/jce.jar""")
...
}
And, of course, JRE should also be installed to exactly same path as on the host, which might be problematic if you used to install Java from RPM, for example. I ended up downloading server JRE (which is distributed as .tar.gz) and extracting it to the right path manually.
So, long story short, it worked in the end. No recompilation, no long waiting time. I was able to find the relevant information from 2 main sources: SBT source code, particularly this function: https://github.com/sbt/sbt/blob/0.13/compile/inc/src/main/scala/sbt/inc/IncrementalCommon.scala#L271, and enabling SBT debug output in build.sbt:
logLevel := Level.Debug
incOptions ~= { _.copy(apiDebug = true, relationsDebug = true) }
(prepare for a lot of output)
This is just a guess. As rumoku suggest it may be an issue with repositories but I think it's related with SBT itself. From SBT's point of view you are running two different machines and it must compile all when it thinks the files have changed.
I don't know how SBT or the compiler identifies the versions of Scala and Java but it may be the case that even though you have the exact same versions of Java and Scala in both environments SBT thinks they are different. Which makes sense as the are different OS.
Related
I'm building a debian package from scala project using the sbt-native-packager plugin. The package produced locally (macos) from command line has different size and of course md5sum than the one I produce in jenkins (debian) using the same sbt command:
sbt debian:packageBin
Also the jenkins produced debian package throws an error when using the included jar, like some classes were not included:
ERROR org.apache.spark.deploy.yarn.ApplicationMaster - User class threw exception: java.lang.NoClassDefFoundError: shadeshapless/Generic
java.lang.NoClassDefFoundError: shadeshapless/Generic
I thought has to do something with the class names on debian environment so added the compiler option:
scalacOptions ++= Seq("-Xmax-classfile-name","255")
I expect the same debian package to be produced on the local and jenkins environments.
The resulting package will always depend on the machine as the version of the native build tool, in your case deb-pkg, affects how the package is produced. Sometimes a plugin generates code which might have timestamps that change, leading to different outputs.
Having said that. I personally hit this classfile issue with shading shapeless, sbt assembly and docker images. This however has nothing to do with the packaging.
Cheers,
Muki
I'm trying to use the sbt-native-packager to produce a Docker image of my Scala play app, I followed the steps described at http://www.scala-sbt.org/sbt-native-packager/formats/docker.html
This is my configuration:
on my plugins.sbt I added the dependency for sbt native packager:
// SBT Native
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.2.1")
on my build.sbt I added the plugins for Universal and Docker:
.enablePlugins(PlayScala, JavaAppPackaging)
I also added some extra properties:
javaOptions in Universal ++= Seq(
// JVM memory tuning
"-J-Xmx1024m",
"-J-Xms512m",
// Since play uses separate pidfile we have to provide it with a proper path
// name of the pid file must be play.pid
s"-Dpidfile.path=/var/run/${packageName.value}/play.pid",
// Use separate configuration file for production environment
s"-Dconfig.file=/usr/share/${packageName.value}/conf/production.conf",
// Use separate logger configuration file for production environment
s"-Dlogger.file=/usr/share/${packageName.value}/conf/logback.xml"
)
// exposing the play ports
dockerExposedPorts in Docker := Seq(9000, 9443)
Then I generate the docker image using the plugin and SBT CLI:
docker:publishLocal
the dockerfile gets generated at ./target/docker/Dockerfile
when I inspect the file I see:
FROM openjdk:latest
WORKDIR /opt/docker
ADD opt /opt
RUN ["chown", "-R", "daemon:daemon", "."]
USER daemon
ENTRYPOINT ["bin/root"]
CMD []
which doesn't seem to contain all the necessary steps to run the app, when I use docker build . I get :
java.nio.file.NoSuchFileException: /var/run/root/play.pid
It seems like the Dockerfile is missing some steps where it should mkdir /var/run/{APP_NAME}/
(* creating folder inside docker container instance)
and chown that folder in order for play to create the PID file.
how to fix the above error ?
What's the error message when starting the docker image and how do you start it?
Then there are a couple of notable things.
play ships with native-packager
You shouldn't have to add any plugin, but only configure docker relevant stuff. You already linked the correct documentation for the package format ( docker ).
Archetypes Vs Formats
Your configuration won't work without the play plugin. Take a look at http://www.scala-sbt.org/sbt-native-packager/archetypes/java_app/index.html which explains how to configure a simple application build.
I'd also recommend to read the format and Archetypes section here:
http://www.scala-sbt.org/sbt-native-packager/introduction.html#archetype-plugins
Native docker build
Native-packager currently generates two docker files, which is confusing and not relevant. Sorry for that confusion. We plan to remove the redundant docker file.
Simply go one level deeper and run the docker build command.
Hope that helps,
Muki
I have this problem on an off since quite while without ever understanding what is going on: I import an sbt-based project in IntelliJ, and when running it from within IntelliJ, it seems to be missing project resources (src/main/resources) on the class path, resulting in .getClass.getResource(...) calls to return null.
I tend to delete .idea and create the project anew as I'm suspecting a caching problem from upgrading an existing .idea from previous IntelliJ version. But no luck today. Sometimes changing .getResource to .getResourceAsStream seems to solve it, but again no luck today.
Checking the project settings, the directory is correctly toggled as 'Resources':
And checking the run configuration, the module/class-path is correctly selected:
Needless to say, using sbt test:run from the terminal works as expected, resources are found.
FWIF, here is the run call from IDEA, where I cannot find the resources in the classpath at all:
/usr/lib/jvm/java-8-oracle/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:47427,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/hhrutz/Documents/devel/Wolkenpumpe/target/scala-2.11/test-classes:/home/hhrutz/Documents/devel/Wolkenpumpe/target/scala-2.11/classes:/home/hhrutz/.ivy2/cache/com.github.scopt/scopt_2.11/jars/scopt_2.11-3.4.0.jar:/home/hhrutz/.ivy2/cache/org.slf4j/slf4j-simple/jars/slf4j-simple-1.7.18.jar:/home/hhrutz/.ivy2/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.7.18.jar:/home/hhrutz/.ivy2/cache/org.scalatest/scalatest_2.11/bundles/scalatest_2.11-2.2.6.jar:/home/hhrutz/.ivy2/cache/org.scala-stm/scala-stm_2.11/jars/scala-stm_2.11-0.7.jar:/home/hhrutz/.ivy2/cache/org.scala-lang.modules/scala-xml_2.11/bundles/scala-xml_2.11-1.0.5.jar:/home/hhrutz/.ivy2/cache/org.scala-lang.modules/scala-swing_2.11/bundles/scala-swing_2.11-1.0.2.jar:/home/hhrutz/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.7.jar:/home/hhrutz/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.8.jar:/home/hhrutz/.ivy2/cache/net.sourceforge.jtransforms/jtransforms/jars/jtransforms-2.4.0.jar:/home/hhrutz/.ivy2/cache/net.htmlparser.jericho/jericho-html/jars/jericho-html-3.3.jar:/home/hhrutz/.ivy2/cache/lucene/lucene/jars/lucene-1.4.3.jar:/home/hhrutz/.ivy2/cache/junit/junit/jars/junit-4.8.2.jar:/home/hhrutz/.ivy2/local/de.sciss/weblaf-ui/2.1.0/jars/weblaf-ui.jar:/home/hhrutz/.ivy2/local/de.sciss/weblaf-core/2.1.0/jars/weblaf-core.jar:/home/hhrutz/.ivy2/local/de.sciss/treetable-scala_2.11/1.3.8/jars/treetable-scala_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/treetable-java/1.3.8/jars/treetable-java.jar:/home/hhrutz/.ivy2/cache/de.sciss/topology_2.11/jars/topology_2.11-1.0.0.jar:/home/hhrutz/.ivy2/local/de.sciss/swingplus_2.11/0.2.1/jars/swingplus_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/submin/0.2.0/jars/submin.jar:/home/hhrutz/.ivy2/cache/de.sciss/span_2.11/jars/span_2.11-1.3.1.jar:/home/hhrutz/.ivy2/local/de.sciss/soundprocesses-views_2.11/3.4.0/jars/soundprocesses-views_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/soundprocesses-core_2.11/3.4.0/jars/soundprocesses-core_2.11.jar:/home/hhrutz/.ivy2/cache/de.sciss/serial_2.11/jars/serial_2.11-1.0.2.jar:/home/hhrutz/.ivy2/cache/de.sciss/scissdsp_2.11/jars/scissdsp_2.11-1.2.2.jar:/home/hhrutz/.ivy2/local/de.sciss/scalaosc_2.11/1.1.5/jars/scalaosc_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/scalacolliderugens-plugins_2.11/1.14.1/jars/scalacolliderugens-plugins_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/scalacolliderugens-core_2.11/1.14.1/jars/scalacolliderugens-core_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/scalacolliderugens-api_2.11/1.14.1/jars/scalacolliderugens-api_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/scalacolliderswing-core_2.11/1.28.0/jars/scalacolliderswing-core_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/scalacollider_2.11/1.18.1/jars/scalacollider_2.11.jar:/home/hhrutz/.ivy2/cache/de.sciss/scalaaudiofile_2.11/jars/scalaaudiofile_2.11-1.4.5.jar:/home/hhrutz/.ivy2/local/de.sciss/raphael-icons_2.11/1.0.3/jars/raphael-icons_2.11.jar:/home/hhrutz/.ivy2/cache/de.sciss/processor_2.11/jars/processor_2.11-0.4.0.jar:/home/hhrutz/.ivy2/local/de.sciss/prefuse-core/1.0.1/jars/prefuse-core.jar:/home/hhrutz/.ivy2/cache/de.sciss/numbers_2.11/jars/numbers_2.11-0.1.1.jar:/home/hhrutz/.ivy2/cache/de.sciss/model_2.11/jars/model_2.11-0.3.2.jar:/home/hhrutz/.ivy2/local/de.sciss/lucresynth_2.11/3.4.0/jars/lucresynth_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/lucreswing_2.11/1.3.0/jars/lucreswing_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/lucre-expr_2.11/3.3.1/jars/lucre-expr_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/lucre-core_2.11/3.3.1/jars/lucre-core_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/lucre-confluent_2.11/3.3.1/jars/lucre-confluent_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/lucre-bdb_2.11/3.3.1/jars/lucre-bdb_2.11.jar:/home/hhrutz/.ivy2/cache/de.sciss/intensitypalette/jars/intensitypalette-1.0.0.jar:/home/hhrutz/.ivy2/cache/de.sciss/fingertree_2.11/jars/fingertree_2.11-1.5.2.jar:/home/hhrutz/.ivy2/cache/de.sciss/fileutil_2.11/jars/fileutil_2.11-1.1.1.jar:/home/hhrutz/.ivy2/local/de.sciss/desktop_2.11/0.7.2/jars/desktop_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/audiowidgets-swing_2.11/1.9.4/jars/audiowidgets-swing_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/audiowidgets-core_2.11/1.9.4/jars/audiowidgets-core_2.11.jar:/home/hhrutz/.ivy2/local/de.sciss/audiowidgets-app_2.11/1.9.2/jars/audiowidgets-app_2.11.jar:/home/hhrutz/.ivy2/cache/com.thoughtworks.xstream/xstream/jars/xstream-1.4.8.jar:/home/hhrutz/.ivy2/cache/com.sleepycat/je/jars/je-5.0.104.jar:/home/hhrutz/.ivy2/cache/com.mortennobel/java-image-scaling/jars/java-image-scaling-0.8.6.jar:/home/hhrutz/.ivy2/cache/com.jhlabs/filters/jars/filters-2.0.235.jar:/home/hhrutz/Applications/idea-IC-16/lib/idea_rt.jar de.sciss.nuages.Demo
Deleting the run configuration and creating it anew "fixed" the problem. It seems to be a bug in IntelliJ, perhaps related to caching or indexing.
The previous broken call was:
java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:47427,suspend=y,server=n \
-classpath ... \
de.sciss.nuages.Demo
The new correct call became:
java -Didea.launcher.port=7534 \
-Didea.launcher.bin.path=/home/hhrutz/Applications/idea-IC-16/bin \
-classpath ... \
com.intellij.rt.execution.application.AppMain de.sciss.nuages.Demo
So while the classpath is same, the launching mechanism is completely different.
I wish to create a docker image with several SBT components built in (docker part not relevant). If I just install sbt from deb or rpm and not do anything, the first time it is invoked it still has to download the internet before it's able to start.
Thus, I need to be able to specify the following:
1 SBT Versions - e.g. 0.12, 0.13.1, 0.13.2, ...
2 Scala Versions - 2.9.1, 2.10.4, 2.11.5, ...
3 Commonly used libraries - play framework, etc.
I'm wondering what's the best way to ensure those are already pre-cached.
Here is what I do in my Dockerfile to prefetch multiple scala versions :
RUN echo 'crossScalaVersions := Seq("2.10.6", "2.11.7")' > build.sbt \
&& echo 'object Hi { def main(args: Array[String]) = println("Done") }' > src.scala \
&& sbt "+run" \
&& rm build.sbt src.scala
You can easily customize this further by specifying the sbt version on cli ( sbt -Dsbt.version=0.x.y "+run" ), or add some libraryDependencies to the build.sbt.
HTH
Well, if you create one project which has everything you want listed as dependency -- including SBT and Scala versions, and then run update to retrieve all these dependencies, they'll all be cached on .ivy2\cache. This should serve for pretty much everything, but SBT itself seems to have tons of separate dependencies as seen here, or on your local disk at the ivy2 cache.
I know if I add withSources when I define one dependency, sbt can download that sources jar file automatically.
For example,
val specs = "org.scala-tools.testing" % "specs_2.8.1" % "1.6.6" % "test" withSources ()
But for the scala-library.jar and scala-compiler.jar, I don't need define them explicitly, how can I get sbt download their sources for me? So, I don't need config it manually after generate idea project using sbt-idea-plugin.
You have to change the boot properties. There is a nice description in the recent blog decodified from Mathias:
"How to make SBT download scala library sources" (started from #hseeberger key starting points)
Here is the relevant part (in case that link ever goes stale)
First, forget about trying to find some “hidden” setting in your SBT project definition enabling Scala library source download! It does not exist (at least not in SBT version 0.7.x).
Rather, there are these two things you need to do in order to whip SBT into submission:
Create an alternative configuration file for your SBT launcher.
Make the SBT launcher use it.
These are the steps in detail:
Find your sbt-launcher-0.7.x.jar file.
Since I’m on OS/X and use SBT via Homebrew mine lives at /usr/local/Cellar/sbt/0.7.5.RC0/libexec/sbt-launch-0.7.5.RC0.jar.
Extract the sbt.boot.properties from the sbt sub directory in the launcher jar
Fire up your favorite editor and change line 3 to classifiers: sources (uncomment the line)
Find the sbt script file you created during your SBT setup (e.g. ~/bin/sbt, or, when using Homebrew, /usr/local/Cellar/sbt/0.7.x/bin/sbt)
Add the path to your tweaked sbt.boot.properties file, prepended with an ’#’ character and in double quotes, as the second-to-last argument of the java call.
This is what my sbt script file looks like:
#!/bin/sh
java -Xmx768M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m \
-jar /usr/local/Cellar/sbt/0.7.5.RC0/libexec/sbt-launch-0.7.5.RC0.jar \
"#/usr/local/Cellar/sbt/0.7.5.RC0/libexec/sbt.boot.properties" \
"$#"
Once you have completed these steps SBT should happily download the scala-...-sources.jar files for the Scala compiler and standard library for any new project you create.
To have SBT do this for an existing project, you have to manually delete the project/boot/scala-{version} directory before performing an ‘sbt update’ (SBT does not fetch additional source artifacts if the main jar is already present).
Once you have a custom sbt.boot.properties file, there are also other ways to supply it to the SBT launcher.
See SO question "how do I get sbt to use a local maven proxy repository (Nexus)?"
Based on Michael Slinn comments:
If you are using sbt 0.11.x and above, use this command:
sbt update-sbt-classifiers
Two pieces of information.
(1) SBT Documentation
http://www.scala-sbt.org/0.13.5/docs/Detailed-Topics/Library-Management.html
and I quote:
"To obtain particular classifiers for all dependencies transitively, run the updateClassifiers task. By default, this resolves all artifacts with the sources or javadoc classifier."
This means you should not need to do anything, but you can make it explicit and put in you build.sbt:
transitiveClassifiers := Seq("sources", "javadoc")
To actually get the sources downloaded by SBT then you do:
"updateClassifiers"
(2) If you are working with Eclipse scala IDE - most likely you are as development of plugins for Eclipse/Netebeans is a lot more active for eclipse - then you should configure your ecplise to find out the sources if you do the following.
EclipseKeys.withSource := true
Here is the documentation you should read,
https://github.com/typesafehub/sbteclipse/wiki/Using-sbteclipse