0

I have an sbt project with custom env configuration.

  val Prod  = config("prod")  extend Compile describedAs "Scope to build production packages."
  val Stage = config("stage") extend Compile describedAs "Scope to build stage packages."
  val Local = config("local") extend Compile describedAs "Scope to build local packages."

lazy val root = (project in file("."))
  .configs(Prod, Stage, Local)
  .enablePlugins(WarPlugin)
  .settings(
    name := "pet",
    dependencySettings
  ).
  settings(inConfig(Prod)(Classpaths.configSettings ++ 
Defaults.configTasks ++ Defaults.resourceConfigPaths ++ Seq(
    unmanagedResourceDirectories += {baseDirectory.value / "src" / "main" / configuration.value.name / "resources"}
  )): _*).
  settings(inConfig(Stage)(Classpaths.configSettings ++ 
Defaults.configTasks ++ Defaults.resourceConfigPaths ++ Seq(
    unmanagedResourceDirectories += {baseDirectory.value / "src" / "main" / configuration.value.name / "resources"}
  )): _*).
  settings(inConfig(Local)(Classpaths.configSettings ++ 
Defaults.configTasks ++ Defaults.resourceConfigPaths ++ Seq(
    unmanagedResourceDirectories += {baseDirectory.value / "src" / "main" / configuration.value.name / "resources"}
  )): _*)

So when I run sbt Local/compile or sbt Local/run command I see in the target directory my compiled classes with my Local configuration.

Then I add xsbt-web-plugin for packaging .war file

addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "4.2.4")

And in build.sbt

enablePlugins(WarPlugin)

Now I try to run sbt Local/package command and get just .jar but not .war

If I run sbt package command I get .jar and .war files but without my Local configuration.

Help me, please. How can I make sbt Local/package work correct? Or maybe you know some other way to build .war file with sbt.

  • What is the output of `show Local/package` in sbt? – earldouglas Dec 09 '21 at 13:06
  • @earldouglas, sbt:pet> show Local/package [info] C:\Users\Aleksei_Eschenko\Documents\github\pet\target\scala-2.12\pet_2.12-0.1-local.jar [success] Total time: 0 s, completed Dec 10, 2021 5:04:40 PM – Alex Eshenko Dec 10 '21 at 14:06
  • Strange; I would have expected to see a *.war* file there. Is this part of a multi-module build? Could the `WarPlugin` seting be applied to a different module from `pet`? – earldouglas Dec 11 '21 at 16:29
  • @earldouglas It is a single module project. I added code to the question description. P.S. but in a multi-module project, where I applied `WarPlugin` setting for each separate module for web. I have the same problem. – Alex Eshenko Dec 13 '21 at 14:19
  • It looks like you just need to add the settings manually (rather than having `Project#enablePlugins` do it for you). Try adding `WarPlugin.projectSettings` to each of the three `inConfig` settings. E.g. `settings(inConfig(Local)(WarPlugin.projectSettings ++...` – earldouglas Dec 15 '21 at 03:48
  • @earldouglas Thank you! It builds .war, but doesn't apply config settings. So, If I run Local/package command I don't have my app.properties file from src/main/local/app.properties in the artifact. – Alex Eshenko Dec 15 '21 at 08:02
  • Hmm, it seems to work for me. Do you mean `src/main/local/app.properties` or `src/main/local/resources/app.properties`? – earldouglas Dec 15 '21 at 12:50
  • @earldouglas I don't have `src/main/resources` directory. Just `src/main/local/resources`, `src/main/stage/resources`, `src/main/prod/resources`. So If I run `sbt Local/package` I want to use `app.properties` file from `src/main/local/resources`, but I don't see any `app.properties` in the artifact, because it tries to get `app.properties` from `src/main/resources`, which doesn't exist. I finally have `.war` file, but without `app.properties`, so how can I make to work `unmanagedResourceDirectories` correct? – Alex Eshenko Dec 15 '21 at 13:29
  • I published the example to github: https://github.com/eshenko/pet maybe it helps – Alex Eshenko Dec 15 '21 at 13:59
  • Thanks for the example; that helps a lot! I think I found the problem -- we're always [pulling mappings from `Compile` configuration](https://github.com/earldouglas/xsbt-web-plugin/blob/72a564d/src/main/scala/com/earldouglas/xwp/WebappPlugin.scala#L140), regardless of which configuration we're packaging from. I think this will have to be changed in xsbt-web-plugin itself. I filed [a new issue](https://github.com/earldouglas/xsbt-web-plugin/issues/579) to track this. – earldouglas Dec 15 '21 at 14:48
  • 1
    Found workaround. My configuration is applied If I firstly run compile command then package one. So it looks like `sbt Local/compile Local/package`. – Alex Eshenko Dec 16 '21 at 18:29

0 Answers0