Pttrt (/ˈpɪtrɪt/, or Pass Them To Run-time) is a sbt plugin, designed to pass data from compile-time to run-time.
Many times, my programs need some information, which is generated at compile-time and used at run-time. For example, Subversion's revision, JARs's dependencies, configuration files, and poor man's macro ;-), etc.
It is really heavyweight to deal with these situation in sbt.
You need to create a plugin for each type of files you generated,
to define many SettingKey
s in each plugin's .scala
files,
and to set values for each of these SettingKey
s in your .sbt
files.
There are some plugins, like xsbt-reflect,
also able to generate some infomation for run-time. But they cannot fit every variant cases I met.
Another plugin, sbt-buildinfo has similar feature as Pttrt
,
but sbt-buildinfo
cannot generate multiply files, preventing itself to be used for code generation from multiply
separate plugins.
I just want a lightweight and general-purpose way like Makefile
:
generated.properties:
echo my.base.dir=$(PWD) > $@
That's why I created Pttrt
.
Add the following line to your project/plugins.sbt
:
addSbtPlugin("com.dongxiguo" % "pttrt" % "0.1.2")
And add pttrtSettings
to your build.sbt
:
pttrtSettings
For example, if you want to know the building version on run-time,
you need to add following lines at build.sbt
:
pttrtSettings
version := "1.2.3-SNAPSHOT"
PttrtKeys.pttrtData <+= version map { v =>
"org.yourHost.yourProject.YourSingleton" -> Map("Version" -> TypedExpression(v))
}
Create PttrtExample.scala
:
object PttrtExample {
def main(args: Array[String]) {
println("Building version is " + org.yourHost.yourProject.YourSingleton.Version)
}
}
$ sbt
> run-main PttrtExample
You will see:
Building version is 1.2.3-SNAPSHOT
See https://github.com/Atry/pttrt/tree/master/pttrt-test for more example.
Pttrt
is for sbt 0.12- Any value being passed to run-time must be a primary type (
Int
,Double
,Boolean
, etc) orjava.io.Serializable
- The value's type must be found in the classpath for both compile-time and run-time.