Search code examples
sbtsbt-native-packager

sbt plugin that customize other plugin


I have many sbt projects that share common behaviour, adding customization to the sbt native packages.

I tried to push all common utilities functions and tasks in a custom plugin. In particular I have a custom task that needs to be executed before the universal packager zips all.

Here is the structure:

object MyPlugin extends AutoPlugin {

  // my custom task 
  val customTask = Def.task { ...}

  override def requires: Plugins = JavaAppPackaging

  object autoImport {
     addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.1.1")
  }

  import autoImport._


  override def projectSettings: Seq[Setting[_]] =
     Seq(
        packageZipTarball in Universal <<= (packageZipTarball in Universal) dependsOn customTask
     )

  }

the plugin compiles fine, but when I import it in my project like this:

lazy val myProj: Project = (project in file("."))
   .settings(MyPlugin.projectSettings: _*)

and I run:

  sbt universal:packageZipTarball

I get:

 References to undefined settings: 

     universal:packageZipTarball from universal:packageZipTarball       

What do I need to to to fix it?


Solution

  • I guess you are adding the plugin in the wrong place. To add sbt-native-packager to your sbt-plugin do the following

    build.sbt

    You need to add the plugin there

    sbtPlugin := true
    addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.1.1")
    

    AutoPlugin

    You can now access native-packager in your AutoPlugin.

    import com.typesafe.sbt.SbtNativePackager.Universal
    import com.typesafe.sbt.NativePackagerKeys.packageZipTarball
    
    object MyPlugin extends AutoPlugin {
       // ... your code
    }
    

    Debugging

    As a general hint: Debugging undefined settings and auto plugins should always start by looking at the enabled auto plugins. You can list all plugins in a project which are enabled by calling sbt plugins.

    Hope that helps, Muki