2017-04-06 64 views
1

我使用的是ssh URI引用对待一个远程的Git仓库作为项目引用我build.sbt,像这样(用ssh允许我访问私有回购):确定本地目录的Git依赖

lazy val dep = RootProject(uri("ssh://[email protected]/...")) 

lazy val root = project(file.in(".").dependsOn(dep) 

如何在构建文件的任务或命令中确定SBT存储项目的本地目录?

回答

1

根据在How do I get SBT staging directory at build time?找到的信息,我能够想出一个可行的解决方案。 (SBT 0.13.13)

您需要定义一个任务(或命令),所以State是可用的,因为它允许您检索staging`目录:

lazy val printRepos = taskKey[Unit]("Print path to dependencies that are hosted in git repositories.") 
printRepos := { 
    import sbt.BuildPaths._ 

    val s = state.value 
    val staging = getStagingDirectory(s, getGlobalBase(s)) 
    // root is a reference to your top-level project, which has 
    // git-hosted dependencies. 

    val repos = gitRepos(staging, root.dependencies) 
    println("${repos.mkString(",")}") 
} 

gitRepos方法(下)发生staging目录和依赖关系,为那些看起来像git repos的过滤器,并返回一系列配对项目,它的原始URI和持有源的本地路径的元组。

用于在本地存储源的实际目录由Resolvers.git返回,这需要一个ResolveInfo对象。 gitRepos构建了一个畸形的ResolveInfo为了再利用Resolvers.git,但我不认为你可以得到周围:

def gitRepos(staging: File, cps: Seq[ClasspathDep[ProjectReference]]): Seq[(ProjectReference, URI, File)] = { 
    import sbt.BuildLoader._ 
    import sbt.RichURI._ 

    val x = cps.flatMap(cp => Reference.uri(cp.project).map(uri => (cp.project, uri))) 
    x.flatMap({ case (project, uri) => { 
    // Stolen from sbt.RetrieveUnit 
    if(uri.getScheme == "git" || uri.withoutMarkerScheme.getPath.endsWith(".git")) { 
     val y = Resolvers.git(new ResolveInfo(uri, staging, null, null)) 
     y.map(path => (project, uri, path())) 
    } 
    else 
     None 
    }}) 
} 

因为getRepos重复利用Resolvers.gitprintRepos将始终打印的确切目录sbt将用于存储项目参考。

1

该信息隐藏在构建单元的localBase字段中。你可以让一个任务来得到它为当前项目:

val localBase: TaskKey[File] = taskKey[File]("the local base of project") 
localBase := { 
    val extracted = Project.extract(state.value) 
    extracted.currentUnit.localBase 
} 

的项目你加为ProjectRef,它需要被添加作为该项目的设置:

localBase in yourProjectRef := ... 

只得到一本地所有基地图:

val allYourBase = taskKey[Map[URI,File]]("project bases") 
allYourBase := { 
    val extracted = Project extract state.value 
    extracted.structure.units.map { case (uri, unit) => (uri,unit.localBase)} 
}