How to set-up the sbt-proguard plugin in Build.scala - 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 = ....
}

Related

SBT Plugin Where is %%% defined?

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

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)

Why does sbt report "not found: value PlayScala" with Build.scala while build.sbt works?

I am creating a multi-module sbt project, with following structure:
<root>
----build.sbt
----project
----Build.scala
----plugins.sbt
----common
----LoggingModule
LoggingModule is a Play Framework project, while common is a simple Scala project.
In plugins.sbt:
resolvers += "Typesafe repo" at "http://repo.typesafe.com/typesafe/releases/"
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.3")
While I have this in build.sbt, all works fine and it recognises PlayScala:
name := "Multi-Build"
lazy val root = project.in(file(".")).aggregate(common, LoggingModule).dependsOn(common, LoggingModule)
lazy val common = project in file("common")
lazy val LoggingModule = (project in file("LoggingModule")).enablePlugins(PlayScala)
However as soon I put this in project/Build.scala instead of `build.sbt' as follows:
object RootBuild extends Build {
lazy val root = project.in(file("."))
.aggregate(common, LoggingModule)
.dependsOn(common, LoggingModule)
lazy val common = project in file("common")
lazy val LoggingModule = (project in file("LoggingModule")).enablePlugins(PlayScala)
...//other settings
}
it generates error as:
not found: value PlayScala
lazy val LoggingModule = (project in file("LoggingModule")).enablePlugins(PlayScala)
^
How to solve the issue?
It's just a missing import.
In .sbt files, some things are automatically imported by default: contents of objects extending Plugin, and (>= 0.13.5) autoImport fields in AutoPlugins. This is the case of PlayScala.
In a Build.scala file, normal Scala import rules apply. So you have to import things a bit more explicitly. In this case, you need to import play.PlayScala (or use .enabledPlugins(play.PlayScala) directly).

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?