我正在使用 IvyDE Eclipse 插件来解决我的项目(我们称之为 A)的依赖关系。其中一个依赖项是我使用 sbt 构建的另一个项目(称为 B)。构建的最后一部分是调用 sbt 的 publishLocal 任务将 B 项目的工件发布到本地 Ivy 存储库。
到目前为止一切正常。但是,当我尝试解析项目 A 依赖项时,IvyDE 会生成一条错误消息,指出它无法解析“doc”和“src”jar。我可以强制它最终工作,因为它不会无法解析包含实际代码的主 jar。不过,我觉得这有点烦人。
在花了相当多的时间寻找解决方案后,
import sbt.Build
import sbt.Project
import java.io.File
import sbt.PathExtra
import sbt.SettingKey
import sbt.Artifact
import sbt.Keys.{ artifact, artifactName, artifactPath, packageSrc, packageDoc, crossTarget, projectID, scalaVersion, scalaBinaryVersion, moduleName }
import sbt.ScalaVersion
import sbt.Configurations.{ Compile }
import sbt.Configurations
/**
* The objective here is to make the artefacts published to the local repository by the publishLocal task
* compatible with the local resolver used by IvyDE.
* This is achieved by dropping the classifiers from the "doc" and "source" artefacts, and by adding
* an extra level directory to their paths to avoid clashing with the "jar" main artefact.
*/
object SbtIvyFix extends Build with PathExtra {
lazy override val projects = Seq(root)
lazy val root: Project = Project("xlstocsv", new File(".")) settings (
artifact in (Compile, packageSrc) := {
(artifact in (Compile, packageSrc)).value.copy(classifier = None)
},
artifact in (Compile, packageDoc) := {
(artifact in (Compile, packageDoc)).value.copy(classifier = None)
},
artifactPath in (Compile, packageSrc) <<= myArtifactPathSetting(artifact in (Compile, packageSrc)),
artifactPath in (Compile, packageDoc) <<= myArtifactPathSetting(artifact in (Compile, packageDoc)))
// Lifted from the Sbt source artifactPathSetting() function in Defaults.scala
def myArtifactPathSetting(art: SettingKey[Artifact]) = (crossTarget, projectID, art, scalaVersion in artifactName, scalaBinaryVersion in artifactName, artifactName) {
(t, module, a, sv, sbv, toString) =>
{
t / a.`type` / toString(ScalaVersion(sv, sbv), module, a)
}
}
}
这工作得很好,但感觉有点过头了。有人可以提出一种更简单的方法来实现相同的结果吗?