在 Gradle 中设置路径。何时使用斜杠“/”,何时使用冒号“:”

Setting path in Gradle. When to use slash '/' and when colon ':'

我正在学习 Gradle(现在是 4.10 版),我对使用分隔符“:”和“/”设置路径感到困惑。在什么情况下使用这种类型是合适的? 我不确定,但看起来冒号只能在设置依赖项时使用,包括项目,另一方面添加任务斜杠用于设置 ex:

的路径
// works                                                     
def webappDir = "$projectDir/src/main/webapp"   

// doesn't work output: home/projectName/:src:main:webapp                         
def webappDir = "$projectDir:src:main:webapp"

处理文件类型的资源时必须使用“/”字符(如您的示例):这是标准的文件分隔符

// path to the webapp directory
def webappDir = "$projectDir/src/main/webapp" 

主要有两种情况需要使用':'字符:

  1. 项目或任务路径

在多项目构建中工作时,字符“:”用于标识层次结构中的项目或任务:例如 :subProject1:subProject:taskA

A project path has the following pattern: It starts with an optional colon, which denotes the root project. The root project is the only project in a path that is not specified by its name. The rest of a project path is a colon-separated sequence of project names, where the next project is a subproject of the previous project.

这里有更多信息:https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:project_and_task_paths

  1. 依赖配置

当使用 "string notation" 声明依赖时,您将使用 ':' 作为 group/module/version 部分的分隔符,例如: runtime 'org.springframework:spring-core:2.5' 。有关依赖符号的更多信息,请参见:https://docs.gradle.org/current/userguide/dependency_types.html