webappResources in package := Seq(baseDirectory.value ....) throws Error parsing expression - scala

I try to configure sbt (version 0.9.0) to use webapp/dist as the webappResource directory when running package task in sbt, and webapp/app as the webappResource directory when running the container:start command, following this description:
How to have different webapp resources for container:start and package tasks in SBT
But it throws the following error:
error: eof expected but 'package' found.
webappResources in package := Seq(baseDirectory.value / "webapp" / "dist")
^
[error] Error parsing expression.
I guess that package is a reserved word also in sbt conf file, any other way to override setting in package task?
The reason for doing this is that I use gulp to manage webclient. Gulp runs the project from app folder, and compiles (minification etc.) the webclient project into dist folder. When I develop, I use the webapp/app folder as declared below:
webappResources in Compile := Seq(baseDirectory.value / "webapp" / "app")
When I create a release, I first build (minification etc.) the webapp client into webapp/dist using gulp. Then I want to package webapp/dist content into the final war.
But I am not able to override the setting above to use webapp/dist, when using the package task.
I have also tried to create my own configuration like this:
webappResources in Compile := Seq(baseDirectory.value / "src" / "main" / "webapp" / "app")
lazy val ReleaseWarConfig = config("release-war") extend (Compile)
val root = (project in file(".")).
configs(ReleaseWarConfig).
settings(inConfig(ReleaseWarConfig)(webSettings): _*).
settings(
webappResources in Compile := Seq(baseDirectory.value/"src"/"main"/"webapp"/"dist")
)
// I have also tried webappResources in ReleaseConfig instead of Compile ..
But it still uses the webapp/app directory instead of webapp/dist directory.
Any help would be very much appreciated !!!!

What is the directory layout of your project? It sounds like you have your Web application resources directory at [myproject]/webapp/dist/, meaning you have WEB-INF/ and WEB-INF/web.xml (and various other optional resources) under [myproject]/webapp/dist/ as well. Is this correct?
To set the location of your Web application resources directory to [myproject]/webapp/dist/, add the following setting to your sbt configuration:
build.sbt:
webappSrc in webapp <<= (baseDirectory in Compile) map { _ / "webapp" / "dist" }
You can read more about this setting in the readme.

Related

sbt plugin: add an unmanaged jar file

I'm trying to create a relatively simple sbt plugin to wrap grpc-swagger artifact.
Therefore, I've created a project with the following structure:
projectDir/
build.sbt
lib/grpc-swagger.jar <- the artifact I've downloaded
src/...
where build.sbt looks like the following:
ThisBuild / version := "0.0.1-SNAPSHOT"
ThisBuild / organization := "org.testPlugin"
ThisBuild / organizationName := "testPlugin"
lazy val root = (project in file("."))
.enable(SbtPlugin)
.settings(name := "grpc-swagger-test-plugin")
According to sbt docs, that's all I have to do in order to include an unmanaged dependecy, that is:
create a lib folder;
store the artifact in there;
However, when I do execute sbt compile publishLocal, the plugin published lacks of that external artifact.
So far I've tried to:
set exportJars := true flag
add Compile / unmanagedJars += file(lib/grpc-swagger.jar") (with also variations of the path)
manual fiddling to libraryDependecies using from file("lib/grpc-swagger.jar") specifier
but none so far seemed to work.
So how am I supposed to add an external artifact to a sbt plugin?
The proper solution to this problem is to publish the grpc-swagger library. If for whatever reason this can't be done from that library's build system, you can do it with sbt. Just add a simple subproject whose only job it is to publish that jar. It should work like so:
...
lazy val `grpc-swagger` = (project in file("."))
.settings(
name := "grpc-swagger",
Compile / packageBin := baseDirectory.value / "lib" / "grpc-swagger.jar",
// maybe other settings, such as grpc-swagger's libraryDependencies
)
lazy val root = (project in file("."))
.enable(SbtPlugin)
.settings(name := "grpc-swagger-test-plugin")
.dependsOn(`grpc-swagger`)
...
The pom file generated for the root project should now specify a dependency on grpc-swagger, and running the publish task in the grpc-swagger project will publish that jar along with a pom file.
That should be enough to make things work, but honestly, it's still a hack. The proper solution is to fix grpc-swagger's build system so you can publish an artifact from there and then just use it via libraryDependencies.

How do I make sbt include non-Java sources to published artifact?

How do I make sbt include non-Java sources to published artifact ?
I'm using Kotlin plugin and can't figure out how to force sbt to include .kt file into published source jar. It only includes .java files.
A lot of people online suggest adding following code to sbt script but it doesn't help
mappings in (Compile, packageSrc) ++= {
val base = (sourceManaged in Compile).value
val files = (managedSources in Compile).value
files.map { f => (f, f.relativeTo(base).get.getPath) }
},
I also tried
includeFilter in (Compile, packageSrc) := "*.scala" || "*.java" || "*.kt",
Here is output of some variables in sbt console
sbt:collections> show unmanagedSourceDirectories
[info] * /home/expert/work/sideprojects/unoexperto/extensions-collections/src/main/scala
[info] * /home/expert/work/sideprojects/unoexperto/extensions-collections/src/main/java
[info] * /home/expert/work/sideprojects/unoexperto/extensions-collections/src/main/kotlin
sbt:collections> show unmanagedSources
[info] * /home/expert/work/sideprojects/unoexperto/extensions-collections/src/main/java/com/walkmind/extensions/collections/TestSomething.java
which plugin you use for kotlin?
https://github.com/pfn/kotlin-plugin has the option kotlinSource to configure where the source directory is located.
sbt packageBin compiled kotlin files and include them to output jar.
build.sbt
// define kotlin source directory
kotlinSource in Compile := baseDirectory.value / "src/main/kotlin",
src/main/kotlin/org.test
package org.test
fun main(args: Array<String>) {
println("Hello World!")
}
console
sbt compile
sbt packageBin
target/scala-2.13
jar include MainKt.class
and folder org/test contains MainKt.class too.
would this solve your problem?
I found a workaround for this in my project https://github.com/makiftutuncu/e. I made following: https://github.com/makiftutuncu/e/blob/master/project/Settings.scala#L105
Basically, I added following setting in SBT to properly generate sources artifact:
// Include Kotlin files in sources
packageConfiguration in Compile := {
val old = (packageConfiguration in Compile in packageSrc).value
val newSources = (sourceDirectories in Compile).value.flatMap(_ ** "*.kt" get)
new Package.Configuration(
old.sources ++ newSources.map(f => f -> f.getName),
old.jar,
old.options
)
}
For the documentation artifact, I added Gradle build to my Kotlin module. I set it up as shown here https://github.com/makiftutuncu/e/blob/master/e-kotlin/build.gradle.kts. This way, I make Gradle build generate the Dokka documentation. And finally, added following setting in SBT to run Gradle while building docs:
// Delegate doc generation to Gradle and Dokka
doc in Compile := {
import sys.process._
Process(Seq("./gradlew", "dokkaJavadoc"), baseDirectory.value).!
target.value / "api"
}
I admit, this is a lot of work just to get 2 artifacts but it did the trick for me. 🤷🏻 Hope this helps.

Scala source external to SBT project folder

When creating an SBT scala project in ItelliJ, it wants me to put all source code in the src/main/scala folder of the project folder. However, I want to put the source code somewhere else independent or particular projects, build-tools, IDEs, etc.
How do I tell it to get the source from arbitrary locations?
You can use the following settings to change the location where SBT looks for your Scala sources:
// Change Scala source directory. Default is:
// scalaSource in Compile := baseDirectory.value / "src" / "main" / "scala"
scalaSource in Compile := file("<Path to your sources...>")
// Change Scala test source directory. Default is:
// scalaSource in Test := baseDirectory.value / "src" / "test" / "scala"
scalaSource in Test := file("<Path to your test sources...>")
For further information (including how to specify other source, library and resource directories), refer to the official SBT documentation for customizing paths.

How to add custom directory to Scala SBT project?

We have conf directory in our project which is fine, but one of the new components requires its own config to be placed into config directory and we can't change that requirement. In my build.scala I added the line:
unmanagedResourceDirectories in Compile += baseDirectory.value / "config",
In SBT when I run $ inspect tree package-src I can see the config directory under compile:unmanagedResourceDirectories section which is under compile:packageSrc::mappings section.
Then I try to check the deployment package locally by running $ publishLocal. The target config directory is missing in the result .zip package.
What did I miss / do wrong?
This is what I added to my Build.scala to finally solve the issue:
mappings in Universal ++= (baseDirectory.value / "config" * "*" get) map
(x => x -> ("config/" + x.getName)),

How to configure sbt to load resources when running application?

My code (Java) reads an image from jar:
Main.class.getResourceAsStream("/res/logo.png")
Everything runs fine (if I start the app after packaging it into a jar). But when I run it using sbt's run task, it returns me null instead of needed stream.
Running this from sbt console also gives null:
getClass.getResourceAsStream("/res/logo.png")
Is there a way to tell sbt to put my resources on classpath?
EDIT:
I set the resources dir to be same as source dir:
build.sbt:
resourceDirectory <<= baseDirectory { _ / "src" }
When I loaded sbt's `console' and ran the following:
classOf[Main].getProtectionDomain().getCodeSource()
I got the location of my classes, but it does not contain neither res folder nor any of my resource files.
Seems that sbt copies resources only to the resulting jar, and does not copy them to classes dir. Should I modify compile task to move these resources files to classes dir?
EDIT2:
Yes, when I manually copy the resource file to classes dir, I can easily access it from console. So, how should I automate this process?
EDIT3:
It seems that sbt is just unable to see my resource folder - it does not add files to resulting jar file, actually!
Solution:
resourceDirectory in Compile <<= baseDirectory { _ / "src" }
I can't give you a full solution right now, but there is a setting called resourceDirectories to which you could add the res folder.
[EDIT]
For me it didn't work also if the resource was in the standard resource folder. Please try it that way:
Main.class.getClassLoader().getResourceAsStream("icon.png")
[EDIT2] This is the full build script (build.scala) which works if your resource is in src/main/java:
import sbt._
import Keys._
object TestBuild extends Build {
lazy val buildSettings = Seq(
organization := "com.test",
version := "1.0-SNAPSHOT",
scalaVersion := "2.9.1"
)
lazy val test = Project(
id = "test",
base = file("test"),
settings = Defaults.defaultSettings ++ Seq(resourceDirectory in Compile <<= javaSource in Compile)
)
}