diff --git a/main/src/main/scala/sbt/Defaults.scala b/main/src/main/scala/sbt/Defaults.scala index 031dd52a5b..df52c1cd68 100644 --- a/main/src/main/scala/sbt/Defaults.scala +++ b/main/src/main/scala/sbt/Defaults.scala @@ -2725,10 +2725,17 @@ object Defaults extends BuildCommon { } def sbtPluginExtra(m: ModuleID, sbtV: String, scalaV: String): ModuleID = - m.extra( - PomExtraDependencyAttributes.SbtVersionKey -> sbtV, - PomExtraDependencyAttributes.ScalaVersionKey -> scalaV - ).withCrossVersion(Disabled()) + partialVersion(sbtV) match + case Some((0, _)) | Some((1, _)) => + m.extra( + PomExtraDependencyAttributes.SbtVersionKey -> sbtV, + PomExtraDependencyAttributes.ScalaVersionKey -> scalaV + ).withCrossVersion(Disabled()) + case Some(_) => + // this produces a normal suffix like _sjs1_2.13 + val prefix = s"sbt${binarySbtVersion(sbtV)}_" + m.cross(CrossVersion.binaryWith(prefix, "")) + case None => sys.error(s"unknown sbt version $sbtV") def discoverSbtPluginNames: Initialize[Task[PluginDiscovery.DiscoveredNames]] = (Def.task { sbtPlugin.value }).flatMapTask { case p => @@ -3073,7 +3080,7 @@ object Classpaths { Defaults.globalDefaults( Seq( publishMavenStyle :== true, - sbtPluginPublishLegacyMavenStyle := true, + sbtPluginPublishLegacyMavenStyle :== true, publishArtifact :== true, (Test / publishArtifact) :== false ) @@ -3081,8 +3088,10 @@ object Classpaths { private lazy val publishSbtPluginMavenStyle = Def.task(sbtPlugin.value && publishMavenStyle.value) private lazy val packagedDefaultArtifacts = packaged(defaultArtifactTasks) - private lazy val emptyArtifacts = Def.task(Map.empty[Artifact, HashedVirtualFileRef]) - + private lazy val sbt2Plus: Def.Initialize[Boolean] = Def.setting { + val sbtV = (pluginCrossBuild / sbtBinaryVersion).value + sbtV != "1.0" && !sbtV.startsWith("0.") + } val jvmPublishSettings: Seq[Setting[_]] = Seq( artifacts := artifactDefs(defaultArtifactTasks).value, packagedArtifacts := Def @@ -3102,13 +3111,39 @@ object Classpaths { * valid POM file, that is a POM file that Maven can resolve. */ private def mavenArtifactsOfSbtPlugin: Def.Initialize[Task[Map[Artifact, HashedVirtualFileRef]]] = + Def.task { + // This is a conditional task. The top-level must be an if expression. + if (sbt2Plus.value) { + // Both POMs and JARs are Maven-compatible in sbt 2.x, so ignore the workarounds + packagedDefaultArtifacts.value + } else { + val crossVersion = sbtCrossVersion.value + val legacyPomArtifact = (makePom / artifact).value + val converter = fileConverter.value + def addSuffix(a: Artifact): Artifact = a.withName(crossVersion(a.name)) + Map( + addSuffix(legacyPomArtifact) -> converter.toVirtualFile( + makeMavenPomOfSbtPlugin.value.toPath() + ) + ) ++ + pomConsistentArtifactsForLegacySbt.value ++ + legacyPackagedArtifacts.value + } + } + + private def legacyPackagedArtifacts: Def.Initialize[Task[Map[Artifact, HashedVirtualFileRef]]] = + Def.task { + // This is a conditional task. The top-level must be an if expression. + if (sbtPluginPublishLegacyMavenStyle.value) packagedDefaultArtifacts.value + else Map.empty[Artifact, HashedVirtualFileRef] + } + + private def pomConsistentArtifactsForLegacySbt + : Def.Initialize[Task[Map[Artifact, HashedVirtualFileRef]]] = Def.task { val crossVersion = sbtCrossVersion.value - val legacyArtifact = (makePom / artifact).value - val converter = fileConverter.value - val pom = converter.toVirtualFile(makeMavenPomOfSbtPlugin.value.toPath) val legacyPackages = packaged(defaultPackages).value - def addSuffix(a: Artifact): Artifact = a.withName(crossVersion(a.name)) + val converter = fileConverter.value def copyArtifact( artifact: Artifact, fileRef: HashedVirtualFileRef @@ -3120,11 +3155,9 @@ object Classpaths { IO.copyFile(file, targetFile) artifact.withName(nameWithSuffix) -> converter.toVirtualFile(targetFile.toPath) } - val packages = legacyPackages.map { case (artifact, file) => copyArtifact(artifact, file) } - val legacyPackagedArtifacts = Def - .ifS(sbtPluginPublishLegacyMavenStyle.toTask)(packagedDefaultArtifacts)(emptyArtifacts) - .value - packages + (addSuffix(legacyArtifact) -> pom) ++ legacyPackagedArtifacts + legacyPackages.map { case (artifact, file) => + copyArtifact(artifact, file); + } } private def sbtCrossVersion: Def.Initialize[String => String] = Def.setting {