wjtan / sbt-byte-buddy

Compile-time code generation via ByteBuddy in sbt

Version Matrix

Download

sbt-byte-buddy - compile-time code generation via ByteBuddy in sbt

This is an sbt plugin for compile-time code generation via ByteBuddy, based on the ByteBuddy maven plugin.

Install the plugin by adding the following to project/plugins.sbt:

addSbtPlugin("net.bytebuddy" % "sbt-byte-buddy" % "1.2.0")

And then modify your build.sbt to enable the SbtByteBuddy plugin:

lazy val myProject = (project in file("."))
  .enablePlugins(ByteBuddyPlugin)

Configurations

If you want to disable the ByteBuddy plugin, use the byteBuddyEnabled setting:

byteBuddyEnabled := false

byteBuddyPlugins is the list of transformations. A transformation must specify the ByteBuddy plugin (implementing net.bytebuddy.build.Plugin), containing the name of a class or classes to apply.

byteBuddyPlugins := Seq("com.package.SomeByteBuddyPlugin")

The byteBuddyInitialization setting defines the initializer used for creating a ByteBuddy instance and for applying a transformation. By default, a type is rebased. The setting can be set to any constant name of EntryPoint.Default or to a class name (extending net.bytebuddy.build.EntryPoint).

byteBuddyInitialization := "com.package.SomeEntryPoint"

byteBuddySuffix specifies the method name suffix that is used when type's method need to be rebased. If this setting is not set or is empty, a random suffix will be appended to any rebased method. If this setting is set, the supplied value is appended to the original method name.

byteBuddySuffix := "SomeSufffix"

byteBuddyPackages is an optional setting to set the filter to search for classes to transform.

It can be used to select all classes under the package:

byteBuddyPackages := Seq("com.sompackage.*")

Or to select a single class:

byteBuddyPackages := Seq("com.package.SomeClass")

Compile-Time Dependencies

Since the transformation plugins may be only used during the compile-only, there is no need to include the dependencies for the transformation plugins during runtime.

Based on this StackOverflow answer, you can create a custom dependency configuration for this.

In build.sbt:

// a 'compile-only' configuation
ivyConfigurations += config("compile-only").hide

// some compileonly dependency
libraryDependencies += "commons-io" % "commons-io" % "2.4" % "compile-only"

// appending everything from 'compileonly' to unmanagedClasspath
unmanagedClasspath in Compile ++= 
  update.value.select(configurationFilter("compile-only"))

That dependency will not appear in the pom.xml generated by publish and friends.

License

This project is released under terms of the Apache 2.0.