将具有通用配置的 gradle 插件应用于所有项目

Apply a gradle plugin with a common configuration to all projects

我有一个包含多个不相关 gradle 项目的工作区。我正在寻找一种方法,通过通用配置将 artifactory 插件应用于所有这些插件。

到目前为止,我尝试创建这个通用 gradle 文件,并使用 apply from:

将其应用于每个项目(顶层,而不是模块)
buildscript {
  repositories {
    maven {
            url 'http://artifactory.mycompany.com/artifactory/plugins-release'    
        } 
    }

    dependencies {
     classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
  }

}


if (!project.plugins.findPlugin("com.jfrog.artifactory"))
  project.apply(plugin: "com.jfrog.artifactory")

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = 'libs-release-local'
            maven = true
        }
    }
    resolve {
        repository {
            repoKey = 'libs-release'
            maven = true
        }
    }
}

但是我在构建时遇到了以下错误:

A problem occurred evaluating script.
> Failed to apply plugin [id 'com.jfrog.artifactory']
   > Plugin with id 'com.jfrog.artifactory' not found.

我怎样才能让这个方案起作用?

据我所知,为无关项目注入通用配置的最佳方法是使用 an init script。您可以在其中配置常见行为,包括应用 Artifactory 插件。

我终于让它工作了。

"right" 的方法可能是 JBaruch mentioned, using an init script. The problem is that Gradle (version 2.6 in my case) can't add a plugin by its id in an init script. It's a bug known (at least) from June 2012 (see here). I found it thanks to this SO answer from 2013

话虽如此,由于 artifactory 插件本身的变化,OP 从 2013 年开始的解决方案(发布在 question 本身)不再有效。具体来说,插件的完全限定名称不再是 org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin。 gradle 2:

现在有(版本 3.1.1)两个插件

org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsGradle2Pluginorg.jfrog.gradle.plugin.artifactory.ArtifactoryConfigurationsGradle2Plugin

所以这是一个有效的初始化脚本:

initscript {
    repositories {
       jcenter()
    }

    dependencies {
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1'
    }
}

allprojects {
    apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryConfigurationsGradle2Plugin //Note the lack of quotation marks
    apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsGradle2Plugin //Note the lack of quotation marks

    artifactory {
        contextUrl = "${artifactory_contextUrl}"
        publish {
            repository {
                repoKey = 'libs-release-local'
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
            }
        }
        resolve {
            repository {
                repoKey = 'libs-release'
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
            }
        }
    }   
}

编辑:

另一个更简单的解决方案是完全删除人工制品插件,并用 maven-publish 替换它。像这样:

allprojects {

    apply plugin: 'maven-publish'

    publishing {
        repositories {
            maven {
                url  "${artifactory_contextUrl}/"+ (version.contains('SNAPSHOT') ? 'libs-snapshot-local' : 'libs-release-local')
                credentials {
                    username "${artifactory_user}"
                    password "${artifactory_password}"
                }
            }
        }
    }

    repositories {
        mavenLocal()

        maven {
            url "${artifactory_contextUrl}/libs-release"
            credentials {
                username "${artifactory_user}"
                password "${artifactory_password}"
            }
        }

        maven {
            url "${artifactory_contextUrl}/libs-snapshot"
            credentials {
                username "${artifactory_user}"
                password "${artifactory_password}"
            }
        }
    }
}