2017-04-06 69 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)} 
}