Apply a gradle plugin with a common configuration

2019-07-17 21:13发布

问题:

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?

回答1:

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


回答2:

From what I know, the best way to inject common configuration for unrelated projects is by using an init script. In it you can configure the common behavior, including applying the Artifactory plugin.