使用项目配置替换Gradle依赖项

时间:2016-03-18 16:09:43

标签: gradle dependencies

我正在尝试用本地子项目替换所有-SNAPSHOT依赖项。它工作正常,但我有一个问题:我无法定义我想要使用的项目配置(它需要default,它扩展runtime,我不希望这样做:< / p>

configurations.all {
    resolutionStrategy.dependencySubstitution {
        all { dependency ->
            if (!dependency.requested.version.endsWith('SNAPSHOT'))
                return
            def isRootProject = rootProject.name.equals(dependency.requested.module)
            def matching = rootProject.subprojects.find {
                it.name.equals(dependency.requested.module)
            }
            if (isRootProject)
                dependency.useTarget rootProject
            else if (matching)
                // The specified syntax with path: and configuration: does not work. How can I do ?
                dependency.useTarget project(path: ":" + dependency.requested.module, configuration: 'compile')
            else
                println 'Keep SNAPSHOT: ' + dependency.requested.module;
        }
    }
}

我知道它为什么不起作用,文档很清楚:https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/DependencySubstitutions.html#project%28java.lang.String%29它不支持配置名称。

此限制是否有已知的解决方法?

1 个答案:

答案 0 :(得分:0)

settings.gradle 文件中,您可以执行以下操作:

def myDeps = ['my-dependency', 'my-dependency-2']
for (String projectName : myDeps) {
  if (new File(rootDir, '../' + projectName).exists()) {
    def path = '../' + projectName
    def mod  = 'my.company:' + projectName
    System.out.println("Adding composite build for project: " + projectName + " using path: " + path + " replacing module: " + mod);
    includeBuild(path) {
      dependencySubstitution {
        substitute module(mod) with project(':')
      }
    }
   } else {
     System.out.println("Skipping composite build for project: " + projectName);
   }
}

如果你喜欢,你可以像我们一样做,而不是使用静态的依赖列表。相反,您可以自动发现当前项目。这要求您的公司在每个依赖项中遵循静态配置模板,因为您必须手动解析文件。