在哪里可以找到已下载的sbt库?

33

sbt把下载的jar包放在哪里?我想让sbt下载所有依赖项并将它们放在lib/目录下,以便我可以在ScalaIDE中使用它们。但是,在成功运行了sbt compile之后,我不知道在哪里找到这些下载的.jar文件。

4个回答

30

所有新版本的SBT(0.7.x之后)默认将下载的JAR文件放入您主目录下的.ivy2目录中。

如果您使用Linux,则通常为/home/<username>/.ivy2/cache

如果您使用Windows,则通常为c:\Users\<username>\.ivy2\cache

编辑:

以下是我的一个项目示例,其中定义了一个SBT任务,将依赖项复制到目标文件夹中。您可以将此代码放入project/Build.scala项目定义文件中。您的项目定义文件应该类似于以下内容(更多信息请参见www.scala-sbt.org):

import sbt._
import Keys._
import Process._

object MyProjectBuild extends Build {
以下代码将所有库复制到deploy/libz子目录中,通过定义一个deploy任务来捕获您的程序文件和所有类路径依赖项:
val deployKey = TaskKey[Unit](
  "deploy",
  "Deploys the project in the `deploy` subdirectory."
)

val deployTask = deployKey <<= (artifactPath in (Compile, packageBin), dependencyClasspath in Compile) map {
  (artifact, classpath) =>
  val deploydir = new File("deploy")
  val libzdir = new File("deploy%slib".format(File.separator))

  // clean old subdirectory
  deploydir.delete()

  // create subdirectory structure
  deploydir.mkdir()
  libzdir.mkdir()

  // copy deps and artifacts
  val fullcp = classpath.map(_.data) :+ artifact
  def lastName(file: File) = if (file.isFile) file.getName else file.getParentFile.getParentFile.getParentFile.getName
  for (file <- fullcp) {
    println("Copying: " + file + "; lastName: " + lastName(file))
    if (file.isFile) IO.copyFile(file, (libzdir / lastName(file)).asFile);
    else IO.copyDirectory(file, (libzdir / lastName(file)))
  }
} dependsOn (packageBin in Compile)

谢谢。但是有没有办法让SBT直接将依赖项下载到我的/lib目录中? - firstprayer
我添加了一个例子,希望它有所帮助。 - axel22

13

在更新版本(1.3+)中,sbt 已经使用 Coursier 代替 Ivy 来获取依赖。

根据链接文档,Coursier 缓存的位置 因操作系统而异。默认值如下:

  • Linux:~/.cache/coursier/v1
  • macOS:~/Library/Caches/Coursier/v1。
  • Windows:%LOCALAPPDATA%\Coursier\Cache\v1

您可以通过在 SBT 中运行 show csrCacheDirectory 命令来查找该值。


4
我从http://mvnrepository.com/找到了sbt依赖。
例如,你想找到MySQL Java Connector,可以在搜索框中搜索,选择一个喜欢的版本,然后你会看到sbt标签:
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.34"

如果您想找到已下载的JAR包,在Windows中路径为C:\Users\<用户名>\.ivy2\cache

在Linux中路径为~/.ivy2/cache

祝好运!


2
下面的参考资料对于sbt很有用。

https://www.scala-sbt.org/1.x/docs/Launcher-Configuration.html

您可以找到`sbt.ivy.home`这个参数,默认值是`${user.home}/.ivy2/`。

...

[repositories] 本地 typesafe-ivy-releases: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/[revision]/[type]s/artifact.[ext], 只用于启动 maven-central sonatype-snapshots: https://oss.sonatype.org/content/repositories/snapshots

[boot] 目录: ${sbt.boot.directory-${sbt.global.base-${user.home}/.sbt}/boot/}

[ivy] ivy-home: ${sbt.ivy.home-${user.home}/.ivy2/}

...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接