11import ConditionalKeys ._
2+ import com .typesafe .tools .mima .plugin .MimaKeys .mimaPreviousArtifacts
3+ import explicitdeps .ExplicitDepsPlugin
24import explicitdeps .ExplicitDepsPlugin .autoImport ._
35import sbt .Keys ._
46import sbt .ScriptedPlugin .autoImport .scripted
7+ import sbt ._
58import sbt .plugins .SbtPlugin
6- import sbt .{ Def , _ }
9+ import sbtversionpolicy . SbtVersionPolicyMima
710
811// This is all the crazy hacks to get cross compiling working with an sub-project that is an sbt plugin.
912object SbtPluginSubProjectPlugin extends AutoPlugin {
1013
1114 override def trigger : PluginTrigger = allRequirements
12- override def requires : Plugins = SbtPlugin
15+ override def requires : Plugins = SbtPlugin && SbtVersionPolicyMima && ExplicitDepsPlugin
1316
1417 object autoImport {
15- val spspIsSbtCompatibleScalaVersion = settingKey[Boolean ](" Checks if the current Scala version is 2.13 " )
18+ val spspCanBuild = settingKey[Boolean ](" Checks if the project dependencies are using a compatible Scala version. " )
1619 }
1720
1821 import autoImport ._
@@ -21,29 +24,35 @@ object SbtPluginSubProjectPlugin extends AutoPlugin {
2124 List (
2225 crossScalaVersions := Nil ,
2326 // Remove all library dependencies for Scala 2.13 as they will not resolve when cross building.
24- libraryDependencies := settingDefaultIfSetting (libraryDependencies, scalaVersion, isScala213 , Nil ).value,
27+ // libraryDependencies := defaultIfCannotBuild (libraryDependencies, Nil).value,
2528 // Remove all project dependencies for Scala 2.13 as they will not resolve when cross building.
2629 projectDependencies := taskDefaultIfSkipped(projectDependencies, Nil ).value,
2730 scripted := inputDefaultIfSkipped(scripted, ()).evaluated,
28- spspIsSbtCompatibleScalaVersion := isSbtCompatibleScalaVersionSetting .value,
31+ spspCanBuild := canBuildSetting .value,
2932 // We can't skip this as it has to run at least once or sbt complains.
3033 update / skip := false ,
3134 // Skip everything else otherwise it will just fail.
32- skip := ! spspIsSbtCompatibleScalaVersion.value,
33- undeclaredCompileDependenciesFilter -= moduleFilter()
35+ skip := ! spspCanBuild.value,
36+ undeclaredCompileDependenciesFilter -= moduleFilter(),
37+ mimaPreviousArtifacts := defaultIfCannotBuild(mimaPreviousArtifacts, Set .empty[ModuleID ]).value
3438 )
3539
36- private def isSbtCompatibleScalaVersionSetting = Def .setting {
37- if (isScala213(scalaVersion.value))
38- throw new IllegalStateException (" sbt project must not use Scala 2.13. Did you force the version with '+'?" )
40+ def defaultIfCannotBuild [A ](setting : Def .Initialize [A ], default : => A ): Def .Initialize [A ] =
41+ settingDefaultIfSetting(setting, spspCanBuild, default)(! _)
42+
43+ private def canBuildSetting = Def .setting {
44+ if (! isScala212(scalaVersion.value))
45+ throw new IllegalStateException (
46+ " sbt project must use Scala 2.12. Check that you have not forced the version with '+'."
47+ )
3948 val versions =
4049 scalaVersion.all(ScopeFilter (inDependencies(ThisProject , transitive = true , includeRoot = false ))).value
41- ! versions.exists(isScala213 )
50+ versions.forall(isScala212 )
4251 }
4352
44- private def isScala213 (version : String ) =
53+ private def isScala212 (version : String ) =
4554 CrossVersion .partialVersion(version) match {
46- case Some ((2 , n)) if n == 13 => true
55+ case Some ((2 , n)) if n == 12 => true
4756 case _ => false
4857 }
4958}
0 commit comments