Gradle 带有快照的多项目依赖

Gradle multi-project dependencies with snapshots

我有一个带有平坦路径的多项目构建,例如:

settings.gradle:
includeFlat 'projA','projB','projC'

大约有 20 个不同的子项目,它们具有各种不同的相互依赖性。我想 gradle 来处理依赖关系,但我不想将整个项目作为一个整体捆绑在一起,我想将它们作为单独的上传保存到我们的工件存储库 (Nexus) 中,并带有它们各自的子组件-项目版本控制。每个子项目都在 gradle.properties 文件中指定了其组。

问题是,如果我通过编译依赖调用依赖,即:

projA-build.gradle:
compile project(":projB")

然后一切都编译好了,工件也上传得很好,但在运行时我收到一个错误,例如:

Could not resolve all dependencies for configuration ':runtime'.
Could not find master:restclient:4.0-SNAPSHOT.

上传在 master build.gradle 文件中。构建脚本的相关部分:

subprojects {
  afterEvaluate { Project proj ->
      def is_snap = false
      def reltype = "releases"
      def artifact_name = sprintf("%s-%s.jar" 
                               ,project['name']
                                 ,project['version'])
      def nexus_release_path = sprintf("%s/nexus/content/repositories/releases/%s/%s/%s/%s"
                               ,project['nexus_url']
                               ,project['groupId'].replaceAll("\.","/")
                               ,project['name']
                               ,project['version']
                               ,artifact_name
                               )

      if(project.version.contains("SNAPSHOT")){
          reltype = "snapshots"
          is_snap = true
      }

      uploadArchives {
        // only try the upload if it's not already there
        onlyIf {
          try {
            def artifact_exists = new URL(nexus_release_path).bytes
            // if we get here then the artifact existed and we only want to
            // build if this is a snapshot version
            is_snap || false
          } catch (FileNotFoundException e) {
            // this means we couldn't find the artifact in nexus
            if(!is_snap){
              println "NOTE ==> Don't forget to create new git tag for $artifact_name!"
            } 
            true
          }
        }

        repositories {
          mavenDeployer {
            repository(url: "${project.nexus_url}/nexus/content/repositories/$reltype") {
              authentication(userName: project.nexus_user, password: project.nexus_password)
            }
            pom.version = "${project['version']}"
            pom.artifactId = "${project.name}"
            pom.groupId = "${groupId}"
          }
        }
      }
...
}

虽然它以某种方式正确地上传到 Nexus,但依赖关系是在工件坐标中 "master" 内置的。

我是不是用错了方法或有错误的期望?

Gradle Project实例提供Maven坐标的默认属性:

  • Object group

    The group of this project. Gradle always uses the toString() value of the group. The group defaults to the path with dots as separators.

  • String name (read-only)

    The name of this project. The project's name is not necessarily unique within a project hierarchy. You should use the Project.getPath() method for a unique identifier for the project.

  • Object version

    The version of this project. Gradle always uses the toString() value of the version. The version defaults to unspecified.

您可以在 build.gradle 文件中使用这些属性来指定或使用相应的值,例如:

group = 'my.company.group'

请注意 name 属性 在 build.gradle 文件中是只读的,但您可以在 settings.gradle 文件中设置项目名称Project 实例将被创建。

Gradle 使用这些值将项目依赖项转换为模块依赖项,这是上传到存储库时所必需的。上传工件时,您手动设置项目的 Maven 坐标(通过设置 pom.versionpom.artifactIdpom.groupId)。对于 versionartifactId,请参考 Gradle 提供的默认属性。但是,对于 groupId,您使用自己的自定义 属性,称为 groupId。我找不到你对 groupId 的定义,但我确定你在某处指定了它,将默认值 属性 group 保留为默认值(属性 path 用点分隔)。作为一种解决方案,我建议您设置默认的 属性 group 和某处,如果需要,在您的代码中使用它:

pom.version = project.version
pom.artifactId = project.name
pom.groupId = project.group