简体   繁体   中英

Sbt autoplugin to override project settings

I am trying to develop a very small autoplugin to override the local configuration of all projects and subprojects. Specifically, I'd like my plugin to change the scalaVersion and add one option to the scalacOptions of the project that includes it. I have read in the documentation that one can alter the order in which settings are assigned, but it seems that I am not assigning them to the correct project. This is the code I have so far:

// build.sbt 
import AddSettings._ 

lazy val root = (project in file(".")) 
  .settingSets(userSettings, defaultSbtFiles, autoPlugins, buildScalaFiles)
  .settings( 
     sbtPlugin := true, 
     name := "sbt-scalafix-config",
     organization := "io.prl-prg", 
     version := "0.0.1", 
     addSbtPlugin("org.some" % "some-other-plugin" % "0.5.3")
   ) 

// PluginCode.scala 

package config 
import sbt._ 
import sbt.Keys._ 

object PluginCode extends AutoPlugin {
  override def trigger = allRequirements 
  override lazy val projectSettings = Seq(
    scalaVersion := "2.12.3", 
    scalacOptions := Seq( "-Yrangepos" )
  )
  println("[PluginCode Setup] Hello World!")
}

Any help is appreciated.

You really don't want to use addSettings , as

  1. It will probably destroy the behavior of all the other AutoPlugin s in your build, and,
  2. It's gone in sbt 1.x anyway (for the reason above, I wager)

AutoPlugin s are not supposed to overwrite settings in projects. Projects should always have the final say about their own settings, so that makes sense.

You can easily do the scalacOptions part by using += instead of := (in the plugin and in the projects). In general, you should virtually never use := with Seq-settings, and scalacOptions in particular.

override def projectSettings = Seq(
    scalacOptions += "-Yrangepos"
)

For scalaVersion , well, if the project explicitly sets scalaVersion , there's nothing your AutoPlugin can do to overrule it. And it shouldn't be able to. However, you could get the effect you want if the project does not define scalaVersion at the project-level (but maybe at the in ThisBuild level, for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM