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

时间:2015-08-31 14:46:41

标签: gradle build.gradle artifactory

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

到目前为止,我尝试使用apply from创建此公共gradle文件并将其应用于每个项目(顶层,而不是模块):

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.

如何让这个方案奏效?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

我终于开始工作了。

"对"这样做的方式可能正如JBaruch提到的那样,使用init脚本。问题是Gradle(在我的情况下是2.6版)无法在init脚本中通过其id添加插件。它是2012年6月(至少)已知的错误(见here)。我发现它归功于this SO answer from 2013

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

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
            }
        }
    }   
}

修改

另一个更简单的解决方案就是完全删除artifactory插件,并将其替换为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}"
            }
        }
    }
}