About ${packaging.type} error when sbt resolves dependencies

I have been using Swagger pages in Scala Maven project without any problem for a long time. Recently, I had to add a Swagger page to a Scala Sbt project and I faced with an unexpected problem. Besides I did the same as always I found myself struggling with some weird problem with the javax.ws.rs-api library used by Swagger.

This is the Sbt console when compiling:

[info] Updating {file:/home/null/code/me/packaging.type/packaging-type-workaround/}packaging-type-workaround...
[info] Resolving jline#jline;2.14.6 ...
[warn] [FAILED ] javax.ws.rs#javax.ws.rs-api;2.1.1!javax.ws.rs-api.${packaging.type}: (0ms)
[warn] ==== local: tried
[warn] /home/null/.ivy2/local/javax.ws.rs/javax.ws.rs-api/2.1.1/${packaging.type}s/javax.ws.rs-api.${packaging.type}
[warn] ==== activator-launcher-local: tried
[warn] /home/null/.activator/repository/javax.ws.rs/javax.ws.rs-api/2.1.1/${packaging.type}s/javax.ws.rs-api.${packaging.type}
[warn] ==== activator-local: tried
[warn] /home/null/programs/activator-dist-1.3.12/repository/javax.ws.rs/javax.ws.rs-api/2.1.1/${packaging.type}s/javax.ws.rs-api.${packaging.type}
[warn] ==== public: tried
[warn] https://repo1.maven.org/maven2/javax/ws/rs/javax.ws.rs-api/2.1.1/javax.ws.rs-api-2.1.1.${packaging.type}
[warn] ==== typesafe-releases: tried
[warn] http://repo.typesafe.com/typesafe/releases/javax/ws/rs/javax.ws.rs-api/2.1.1/javax.ws.rs-api-2.1.1.${packaging.type}
[warn] ==== typesafe-ivy-releasez: tried
[warn] http://repo.typesafe.com/typesafe/ivy-releases/javax.ws.rs/javax.ws.rs-api/2.1.1/${packaging.type}s/javax.ws.rs-api.${packaging.type}
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: ^ see resolution messages for details ^ ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: javax.ws.rs#javax.ws.rs-api;2.1.1!javax.ws.rs-api.${packaging.type}
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[trace] Stack trace suppressed: run last *:update for the full output.
[error] (*:update) sbt.ResolveException: download failed: javax.ws.rs#javax.ws.rs-api;2.1.1!javax.ws.rs-api.${packaging.type}
[error] Total time: 4 s, completed Jan 14, 2019 4:08:00 PM

When Sbt tries to resolve the javax.ws.rs-api dependency a weird ${packaging.type} shows up instead of the expecting jar.

I tried to compile the same code using Maven and I everything worked as expected. So I started looking in the internet about the problem and I found that this is known problem with some dependency libraries in Sbt. Basically, Sbt doesn’t handle the ${packaging.type} variable. For more information see: https://github.com/sbt/sbt/issues/3618

There are several workarounds but the one I like is to create an Sbt AutoPlugin for setting the jar package type in the build settings.

This is the vert simple workaround code:

import sbt._

object PackagingTypeWorkaround extends AutoPlugin {
  override val buildSettings = {
    sys.props += "packaging.type" -> "jar"

You should place this code into a PackagingTypeWorkaround.scala file in the project folder.

After that you can compile your Sbt project without any problem.

> compile
[info] Compiling 1 Scala source to /home/null/code/me/packaging.type/packaging-type-workaround/target/scala-2.12/classes...
[info] 'compiler-interface' not yet compiled for Scala 2.12.8. Compiling...
[info] Compilation completed in 15.15 s
[success] Total time: 17 s, completed Jan 14, 2019 4:15:55 PM

Final Comments:

When you build your project using Jenkins for the very first time after adding the workaround it will fail. You don’t have to do anything else, just run the Jenkins job again.


NOTE: You can find the code for this project at: https://github.com/Scalents/packaging-type-workaround

Leave a Reply

Your email address will not be published. Required fields are marked *