beat.roet
beat.roet

Reputation: 51

Gradle: Cannot configure artifactory from an external build script

I am new to gradle and would like to access my artifactory repository from it. If I put all configurations into one build script, the build succeeds. Here are the relevant parts of my build.gradle:

allprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'artifactory'
}

// ...

buildscript {
    repositories {
        maven { 
            url 'http://repo.jfrog.org/artifactory/gradle-plugins' 
        }

        maven {
            url artifactory_contextUrl + 'plugins-release'

            credentials {
                username = artifactory_user
                password = artifactory_password
            }
        }
    }

    dependencies {
        classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.16')
    }
}


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


dependencies {
    // My dependencies ...
}

// Rest of the build script ...

Now, I would like to pull out the artifactory part into a separate gradle script for better organization. This is where the build goes wrong. Quite surprisingly, I get the following error even if I copy my build.gradle to foo.gradle, and change build.gradle to just contain the single line

apply from: 'foo.gradle'

The error is

FAILURE: Build failed with an exception.

* Where:
Script '/path/to/my/project/foo.gradle' line: 5

* What went wrong:
A problem occurred evaluating script.
> Plugin with id 'artifactory' not found.

In case this is not a bug, can anyone please explain this behavior of gradle's apply from and propose a solution?

Thank you

Upvotes: 5

Views: 5869

Answers (2)

Matthias Braun
Matthias Braun

Reputation: 34363

You can also use the fully qualified class name to apply the plugins in your helper script:

buildscript {
    repositories {
         jcenter() 
         mavenCentral()
    }
    dependencies {
        classpath "com.adaptc.gradle:nexus-workflow:0.5"
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:2.2.4"
    }
}
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsPlugin
apply plugin: com.adaptc.gradle.nexusworkflow.NexusWorkflowPlugin

Note that Gradle won't find the plugins if you put quotes around the class name, as you would do normally with plugin names.

This is how I found the class name for the Artifactory plugin:

  1. I downloaded the plugin which was thankfully open source.

  2. I searched for the name of the plugin among the files and found artifactory-puplish.properties.

  3. It contained the following property: implementation-class=org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsPlugin

The source of nexus-workflow has no such properties file so I looked around until I found

plugins-gradle-master/nexus-workflow/src/main/groovy/com/adaptc/gradle/nexusworkflow/NexusWorkflowPlugin.groovy

Upvotes: 1

JBaruch
JBaruch

Reputation: 22893

The apply from part is parsed once the build script is already configured, so telling Gradle where to find the plugins with specific ID is too late. You'll have to keep the buildscript part in the script, or put it in the init script:

apply from : 'http://link.to/my/gradle.script'

Upvotes: 5

Related Questions