I am trying to add a library dependency through the sbt plugin. A dependency must be added to each subproject on its binary version of scala, so I repeat every subproject.
private def inject(): State => State = { state => val extracted: Extracted = Project.extract(state) val enrichedLibDepSettings = extracted.structure.allProjectRefs map { projRef => val projectScalaVersion = (scalaBinaryVersion in projRef) libraryDependencies in projRef += compilerPluginOrg % (compilerPluginArtifact + "_" + projectScalaVersion.value) % compilerPluginVersion % "provided" } val newState = extracted.append(enrichedLibDepSettings, state) val updateAfterLibAppend = extracted.structure.allProjectRefs map { projRef => println("running update: " + EvaluateTask(extracted.structure, update, newState, projRef)) } state }
However, this does not work. The printed output does not display the library dependency trace added using libraryDependencies in projRef += , and does not display any error, leaving the following steps to discard the missing dependency. What could be wrong with this technique?
You ask why this is necessary in the first place? why add library dependency via sbt plugin?
Although we have addCompilerPlugin in addCompilerPlugin , it cannot be used for compiler plugins that have arguments ( -Xplugin with a jar outline should be specified in scalac, since it takes compiler plugin arguments, as far as experimentation shows). Therefore, we need to implement the compiler plugin through -Xplugin after it is resolved as a library dependency (then script its file path, checking the update result). Therefore, we need to add the library dependency through the sbt plugin. And we also have to do this in every subproject, since a multi-project assembly can contain subprojects with different versions of scala - each of them must have a built-in binary compatible compiler plug-in to support binary compatibility.
By the way, and this may illuminate something, I am in the dark: When adding a library dependency in the projectSettings override for the root project - as shown below - the dependency seems to be fixed, but it is useless since it will apply the same binary version to all subprojects, which contradicts the nature (of course, some subprojects will be divided into binary incompatibilities). I also think that this will override the root settings, while the goal here is to add a parameter so as not to override the existing settings.
object Plugin extends AutoPlugin { override lazy val projectSettings = Seq( ... }
A couple of tips?
Adding scalacOptions attributes for each subproject - using the same method - just works.
Applying += to libraryDepenencies above does not even affect the output of inspect libraryDependencies , unlike using the same idiom inside the override lazy val projectSettings AutoPlugin .