이것은 원래 Gitter의 @ daniel-c-sobral에 의해 요청되었습니다.
현재 프로젝트가 동적 작업 내에서 루트 프로젝트인지 어떻게 감지 할 수 있습니까?
업데이트 :
Daniel에게 조금 더 이야기하면서 그는 다음과 같이 명확히했습니다.
test
작업을 실행하기 위해 변경되는 플러그인 이지만 루트 프로젝트에서만 가능합니다.
따라서 특정 작업의 범위가 루트 프로젝트인지 여부를 감지하려는 것 같습니다.
몇 가지 도우미 정의가 제공됩니다.
val rootProjectRef: Def.Initialize[ProjectRef] = Def.setting {
val rootBuildUri = loadedBuild.value.root
val rootProjectId = loadedBuild.value.units(rootBuildUri).root
ProjectRef(rootBuildUri, rootProjectId)
}
val currentProjectRef: Def.Initialize[Task[ProjectRef]] =
Def.task(Project.session(state.value).current)
현재 선택한 프로젝트가 (루트) 빌드의 루트 프로젝트 (동적 여부에 관계없이 다른 작업에서 호출 할 수 있음)인지 확인하는 작업과보다 일반적인 "이 프로젝트가 루트인지 여부를 결정하는 작업을 상당히 깔끔하게 구현할 수 있습니다. 계획?" 및 "이 프로젝트가 현재 선택한 프로젝트입니까?" 질문 :
val isRootProject = settingKey[Boolean]("Is this the root project?")
val isCurrentProject = taskKey[Boolean]("Is this the currently selected project?")
val isCurrentProjectRoot = taskKey[Boolean]("Is the currently select project the root project?")
inScope(Global)(Def.settings(
isCurrentProjectRoot := currentProjectRef.value == rootProjectRef.value,
))
val commonSettings = Def.settings(
isRootProject := thisProjectRef.value == rootProjectRef.value,
isCurrentProject := thisProjectRef.value == currentProjectRef.value,
)
lazy val root = project in file(".") settings commonSettings aggregate app
lazy val app = project settings commonSettings
실행 중 :
> show root / isRootProject
[info] true
> show app / isRootProject
[info] false
>
> show root / isCurrentProject
[info] true
[success] Total time: 0 s, completed 23 Jan 2020, 00:02:58
> show app / isCurrentProject
[info] false
[success] Total time: 0 s, completed 23 Jan 2020, 00:03:04
>
> project
[info] root (in build file:/s/t-sbt-amIRoot/)
>
> show isCurrentProjectRoot
[info] true
[success] Total time: 0 s, completed 23 Jan 2020, 00:03:11
>
>
>
> project app
[info] Set current project to app (in build file:/s/t-sbt-amIRoot/)
>
> show root / isCurrentProject
[info] false
[success] Total time: 0 s, completed 23 Jan 2020, 00:03:34
> show app / isCurrentProject
[info] true
[success] Total time: 0 s, completed 23 Jan 2020, 00:03:37
>
> show isCurrentProjectRoot
[info] false
[success] Total time: 0 s, completed 23 Jan 2020, 00:03:42
[success] Total time: 0 s, completed 22 Jan 2020, 22:04:43
몇 가지 유의할 사항 :
먼저 isRootProject
로드 된 빌드의 변경 불가능한 값이기 때문에 설정으로 구현할 수 있습니다.
둘째, 범위 지정 isCurrentProjectRoot
은 무의미하지만 (프로젝트 내에서 어떤 것에 의존하지 않기 때문에 전역적인 이유입니다) 불법이 아니므로 오해의 소지가있을 수 있습니다. 예를 들면 다음과 같습니다.
> project /
[info] Set current project to root (in build file:/s/t-sbt-amIRoot/)
>
> show isCurrentProjectRoot
[info] true
[success] Total time: 0 s, completed 23 Jan 2020, 00:06:23
>
> show app / isCurrentProjectRoot
[info] true
[success] Total time: 0 s, completed 23 Jan 2020, 00:06:26
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다