将通用配置的gradle插件应用于所有项目 [英] Apply a gradle plugin with a common configuration to all projects

查看:651
本文介绍了将通用配置的gradle插件应用于所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



到目前为止,我尝试创建这个通用的gradle文件,然后应用它使用应用于的每个项目(顶层,而非模块):

  buildscript {
repositories {
maven {
url'http://artifactory.mycompany.com/artifactory/plugins-release'
}
}

依赖关系{
classpathorg.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
}
}
解决{
知识库{
repoKey ='libs-release'
maven = true
}
}
}

但构建时出现以下错误:

 评估脚本时发生问题。 
>无法应用插件[id'com.jfrog.artifactory']
>找不到id为'com.jfrog.artifactory'的插件。

我怎样才能让这个方案奏效?

解决方案

我终于找到了它。



这样做的正确方式可能是 JBaruch 提到,使用初始化脚本。问题是Gradle(在我的情况下,版本2.6)不能通过它的id在init脚本中添加一个插件。这是自2012年6月以来(至少)已知的一个bug(请参阅 here )。我发现这要感谢 2013年的答案



话虽如此,OP的解决方案从2013年开始(发布在问题本身)之后再也不能工作了,原因在于artifactory插件本身。具体来说,插件的完全限定名不再是 org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin 。现在有(版本3.1.1)两个插件for Gradle 2: c $ c>

org.jfrog.gradle.plugin.artifactory.ArtifactoryConfigurationsGradle2Plugin



<所以这里有一个可用的init脚本:

  initscript {
repositories {
jcenter()
}

依赖关系{
classpath'org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1'
}
}

allprojects {
apply plugin:org.jfrog.gradle.plugin.artifactory.ArtifactoryConfigurationsGradle2Plugin //注意缺少引号
apply plugin:org.jfrog .gradle.plugin.artifactory.ArtifactoryPublicationsGradle2Plugin //注意缺少引号

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

编辑:

另一个更简单的解决方案就是完全删除artifactory插件并替换它与 maven-publish 。像这样:

allprojects {

apply plugin:'maven-发布'

发布{
版本库{
maven {
url$ {artifactory_contextUrl} /+(version.contains('SNAPSHOT')?'libs-
$ {artifactory_user}
密码$ {artifactory_password}
}
}


$ b存储库{
mavenLocal()

maven {
url$ {artifactory_contextUrl} / libs
凭证{
用户名$ {artifactory_user}
密码$ {artifactory_password}
}
}

maven {
url$ {artifactory_contextUrl} / libs-snapshot
凭证{
用户名$ {artifactory_user}
密码$ {artifactory_password}
}
}
}
}


I have a workspace with multiple unrelated gradle projects. I'm looking for a way to apply the artifactory plugin to all of them with a common configuration.

So far, I tried creating this common gradle file, and applying it to each project (top level, not module) using 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
        }
    }
}

But I'm getting the following error when building:

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

How can I get this scheme to work?

解决方案

I finally got it to work.

The "right" way of doing this is probably, as 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.

Having said that, OP's solution from 2013 (posted in the question itself) doesn't work anymore due to changes in the artifactory plugin itself. Specifically, the plugin's fully qualified name is no longer org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin. There are now (Version 3.1.1) two plugins for gradle 2:

org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsGradle2Plugin and org.jfrog.gradle.plugin.artifactory.ArtifactoryConfigurationsGradle2Plugin

So here's a working init script:

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

Edit:

Another, simpler, solution is just to remove the artifactory plugin entirely, and replace it with maven-publish. Like so:

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

这篇关于将通用配置的gradle插件应用于所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆