通过快照实现多项目依赖关系

时间:2018-07-20 18:02:15

标签: gradle multi-project

我有多个具有平坦路径的项目,例如:

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,但在工件坐标中以“主”身份内置了依赖关系。

我是用错误的方式还是有错误的期望?

1 个答案:

答案 0 :(得分:0)

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

  
      
  • Object group

         

    该项目的组。 Gradle始终使用组的toString()值。该组默认使用以点作为分隔符的路径。

  •   
  • String name(只读)

         

    该项目的名称。项目名称在项目层次结构中不一定是唯一的。您应该将Project.getPath()方法用于项目的唯一标识符。

  •   
  • Object version

         

    此项目的版本。 Gradle始终使用版本的toString()值。该版本默认为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