SBT Plugin Where is %%% defined? - scala

I have an SBT plugin which will auto-generate some Scala.js code just before compile time. This code depends on a library which I would like to automatically include when the plugin is enabled.
This compiles and runs, but does not get the Scala.js version of the library:
import sbt._
import Keys.libraryDependencies
object MyPlugin extends AutoPlugin {
object autoImport {
lazy val baseSettings: Seq[Def.Setting[_]] = Seq(
libraryDependencies += "my.lib" %% "library" % "0.1.0"
)
}
import autoImport._
override lazy val projectSettings = baseSettings
}
I when I try to use "my.lib" %%% "library" % "0.1.0", I get:
value %%% is not a member of String
I feel like I'm probably missing an import, but I can't find where this is supposed to be defined.

%%% is defined by the sbt-platformdeps plugin.
Unless your sbt plugin already depends on sbt-scalajs, you'll need to add a dependency to it in your plugin project's settings:
addSbtPlugin("org.portable-scala" % "sbt-platform-deps" % "1.0.0")
The following import will bring it in scope:
import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._

addSbtPlugin("com.lightbend.lagom" % "lagom-sbt-plugin" % "X.Y.Z") // replace 'X.Y.Z' with your preferred version (e.g. '1.2.0-RC2').
You can refer to this one
https://www.lagomframework.com/documentation/1.6.x/java/LagomBuild.html

Related

Need to provide a SettingKey from a plugin I use in my sbt plugin

I am using the s3 resolver plugin and would like to override it in my AutoPlugin.
I have tried added the value to projectSettings and globalSettings.
Error
not found: value s3CredentialsProvider
[error] s3CredentialsProvider := s3CredentialsProviderChain
Code
lazy val s3CredentialsProviderChain = {bucket: String =>
new AWSCredentialsProviderChain(
new EnvironmentVariableCredentialsProvider(),
CustomProvider.create(bucket)
)
}
override lazy val projectSettings = Seq(
publishTo := {
if (Keys.isSnapshot.value) {
Some("my-snapshots" at "s3://rest-of-stuff")
} else {
Some("my-releases" at "s3://rest-of-stuff")
}
},
s3CredentialsProvider := s3CredentialsProviderChain
)
The plugin code I'm working on does not define any custom settings of it's own thus has no autoImport of it's own.
Update
I have been unable to resolve the fm.sbt.S3ResolverPlugin in MyPlugin and the code won't compile.
I have tried adding it to enablePlugins on MyPlugin's build.sbt as well as adding it to the dependencies like this:
libraryDependencies ++= Seq(
"com.amazonaws" % "aws-java-sdk-sts" % amazonSDKVersion,
"com.frugalmechanic" % "fm-sbt-s3-resolver" % "0.17.0"
)
I get an error from sbt which I've asked below:
sbt fails to resolve a plugin as dependency
If you create an AutoPlugin in project directory. You need to add this to plugins.sbt.
addSbtPlugin("com.frugalmechanic" % "fm-sbt-s3-resolver" % "0.16.0")
If you create an independent plugin, add this to build.sbt of the plugin
sbtPlugin := true
addSbtPlugin("com.frugalmechanic" % "fm-sbt-s3-resolver" % "0.16.0")
autoImport does not work in scala files that are compiled for sbt, ie plugins for example. You have specify imports statements as in simple scala program. Something like this
import fm.sbt.S3ResolverPlugin
import sbt._
object TestPlugin extends AutoPlugin {
override def requires = S3ResolverPlugin
override def trigger = allRequirements
override def projectSettings: Seq[Def.Setting[_]] = Seq(
S3ResolverPlugin.autoImport.s3CredentialsProvider := ???
)
}
Note that to enable TestPlugin, you have to call enablePlugins(S3ResolverPlugin) in build.sbt

How to use SBT IntegrationTest configuration from Scala objects

To make our multi-project build more manageable we split up our Build.scala file into several files, e.g. Dependencies.scala contains all dependencies:
import sbt._
object Dependencies {
val slf4j_api = "org.slf4j" % "slf4j-api" % "1.7.7"
...
}
We want to add integration tests to our build. Following the SBT documentation we added
object Build extends sbt.Build {
import Dependencies._
import BuildSettings._
import Version._
import MergeStrategies.custom
lazy val root = Project(
id = "root",
base = file("."),
settings = buildSettings ++ Seq(Git.checkNoLocalChanges, TestReport.testReport)
).configs(IntegrationTest).settings(Defaults.itSettings: _*)
...
}
where Dependencies, BuildSettings, Version and MergeStrategies are custom Scala objects definied in their own files.
Following the documentation we want to add some dependencies for the IntegrationTest configuration in Dependencies.scala:
import sbt._
object Dependencies {
val slf4j_api = "org.slf4j" % "slf4j-api" % "1.7.7"
val junit = "junit" % "junit" % "4.11" % "test,it"
...
}
Unfortunately this breaks the build:
java.lang.IllegalArgumentException: Cannot add dependency
'junit#junit;4.11' to configuration 'it' of module ... because this configuration doesn't exist!
I guess I need to import the IntegrationTest configuration. I tried importing the IntegrationTest configuration in Dependencies.scala:
import sbt.Configurations.IntegrationTest
IntegrationTest is a lazy val defined in the Configurations object:
object Configurations {
...
lazy val IntegrationTest = config("it") extend (Runtime)
...
}
But that did not solve the problem.
Does someone has an idea how to solve this?
You need to add the config to the Project object before you add the dependency to the Project object.
Your code quotes show you doing the former, but you don't show where you are doing the latter in your quoted code.
Please could you post the full config, or try moving those two around each other?
Here is where the config is added to the Project object in the SBT docs you linked to:
lazy val root = (project in file(".")).
configs(IntegrationTest).
Your quoted code above which declares a lazy val but does not use it is not sufficient to get the "it" config into use:
lazy val IntegrationTest = config("it") extend (Runtime)

How to set-up the sbt-proguard plugin in Build.scala

I want to use the sbt-proguard plugin in my project, but I need to set it up in a Build.scala file.
I read the documentation but there is just an example for the build.sbt file, which won't work in my case. I need to know how to configure the plugin for my Build.scala file.
Here's the link to the repo: https://github.com/sbt/sbt-proguard/blob/master/README.md#example
FYI: I'm using scala.version=2.11.4 and sbt.version=0.13.5
(Note: sbt currently recommends multi-project build.sbt instead of build.scala.)
Some of the sbt plugins use scripted to test itself, which sets up fake builds under src/sbt-test. If you find one it could contain nice samples on how to set up the plugin.
sbt-proguard created a sample called akka supposedly used by Akka project.
import sbt._
import sbt.Keys._
import com.typesafe.sbt.SbtProguard._
object SampleBuild extends Build {
import ProguardKeys.{ mergeStrategies, merge, options }
import ProguardOptions.keepMain
import ProguardMerge.append
lazy val proguardAkka = Project(
id = "proguard-akka",
base = file("."),
settings = Defaults.defaultSettings ++ proguardSettings ++ Seq(
scalaVersion := "2.10.1",
libraryDependencies += "com.typesafe.akka" %% "akka-remote" % "2.1.2",
merge in Proguard := true,
mergeStrategies in Proguard += append("reference.conf"),
options in Proguard += keepMain("A"),
options in Proguard += keepMain("B"),
options in Proguard += "-dontoptimize", // reduce time for proguard
options in Proguard += ProguardConf.akka
)
)
}
object ProguardConf {
val akka = ....
}

I can not import filters in playframework 2.3.0

I use playframework 2.3.0, recently I want to add the CSRFFilter
when I import csrf in global.scala:
import play.filters.csrf._
I get an error for this:
[error] G:\testprojects\app\Global.scala:7: object filters is not a member
of package play
[error] import play.filters.csrf._
My plugin.sbt is
...
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.0")
...
I use Build.scala instead of build.sbt
lazy val root = Project("root", base = file(".")).enablePlugins(PlayScala)
.settings(baseSettings: _*)
.settings(libraryDependencies++=appDependencies)
.settings(
scalaVersion := "2.11.1",
version := "1.0"
)
According to the documentation you have to add the filters dependency to your project:
libraryDependencies += filters
The documentation is for build.sbt but I guess it should work with Build.scala too.
Play Framework GzipFilter is working for me,
my build.sbt file
name := "GZIP"
version := "1.0-SNAPSHOT"
libraryDependencies ++= Seq(
javaJdbc,
javaEbean,
cache,
filters
)
play.Project.playJavaSettings
steps to get play.filters package
1. play
2. update //important
3. clean
4. eclipse
5. compile
6. run
finally it will work.... (update command is important)
if IDE not detecting play.filters
do the above steps one more time
finally copy paste below code
import play.GlobalSettings;
import play.api.mvc.EssentialFilter;
import play.filters.gzip.GzipFilter;
public class Global extends GlobalSettings {
public <T extends EssentialFilter> Class<T>[] filters() {
return new Class[]{GzipFilter.class};
}
}
In Play 2.4.3, the import is:
import play.filters.cors.CORSActionBuilder
It's no longer called csrf, but cors.

libraryDependencies on sbt Build.scala Full Configuration with sub-projects

I have a project foo with two children foo-core and foo-cli, foo-cli depends on foo-core
(I come from Java/Maven and tried to transpose the parent module with 2 submodules architecture).
Following https://github.com/harrah/xsbt/wiki/Full-Configuration, I wrote my project/Build.scala this way:
import sbt._
import Keys._
object MyBuild extends Build {
//Dependencies
val slf4s = "com.weiglewilczek.slf4s" %% "slf4s" % "1.0.6"
val slf4j = "org.slf4j" %% "slf4j-simple" % "1.5.6"
val grizzled = "org.clapper" %% "grizzled-slf4j" % "0.5"
val junit = "junit" % "junit" % "4.8" % "test"
//End dependencies
lazy val root : Project = Project("root", file(".")) aggregate(cli) settings(
mainClass:= Some("Main")
)
lazy val core : Project = Project("core", file("core"), delegates = root :: Nil) settings(
name := "foo-core",
libraryDependencies ++= Seq(grizzled)
)
lazy val cli: Project = Project("cli", file("cli")) dependsOn(core) settings(
name := "foo-cli",
libraryDependencies ++= Seq(grizzled)
)
}
This configuration does not work: grizzled library is not dowloaded when I run sbt reload;sbt +update (as indicated in http://software.clapper.org/grizzled-slf4j/) and thus the "import grizzli._" fail in my core and cli projects when I sbt compile.
Since I'm new to scala/sbt I imagine I'm doing something awful but can't figure why since I'm confused with all sbt 0.7/sbt0.10 conflicting configurations that were suggested
(like Subproject dependencies in SBT).
Any idea? Hint that could help me?
Thanks in advance
That's grizzled, not grizzli you are using as dependency. The import is:
import grizzled._
This works here from console on project cli and project core, with nothing more than the configuration file above.
Are you using SBT 0.10?