Hoobajoob
Hoobajoob

Reputation: 2878

How to use gradle Artifactory plugin in gradle subproject

I have a multi-project gradle build wherein one of the subprojects is applying the Artifactory plugin (version 4.2.0), and configuring the contextUrl and resolve repoKey.

It sets up a simple configuration and dependency entry, and then has a copy task to retrieve the dependency as a zip file and extract it into a directory.

However, when the copy task runs, I get the error below. What am I doing wrong? Is this a problem with the Artifactory plugin, or gradle, or...?

The problem does not appear to be related to whether or not this is a subproject. I get the same error if I remove the multiproject configuration and run the task from the subproject directory.

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\hoobajoob\project\subproject\package.gradle' line: 36

* What went wrong:
A problem occurred evaluating project ':subproject'.
> Could not resolve all dependencies for configuration ':subproject:runtimeDep'.
   > Cannot resolve external dependency company.com:artifact-id:1.0.0 because no repositories are defined.

Here are the contents of subproject/package.gradle (Artifactory url/user/password properties are in a gradle.properties file for the subproject):

plugins {
  id "com.jfrog.artifactory" version "4.2.0"
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    resolve {
        repository {
          username = "${artifactory_user}"
          password = "${artifactory_password}"
          repoKey = 'some-repo'
        }
    }
}

configurations {
    runtimeDep
}

dependencies {
    runtimeDep 'company.com:artifact-id:1.0.0@zip'
}

ext.destination = null
task getDependencies(type: Copy) {
    from zipTree { configurations.runtimeDep.singleFile }
    into ".artifacts/runtime"
}

The root project build script is empty except for the wrapper task. Below is the settings.gradle file:

include 'subproject'
rootProject.children.each { project -> project.buildFileName = "package.gradle" }

Upvotes: 1

Views: 859

Answers (2)

Hoobajoob
Hoobajoob

Reputation: 2878

While the task setup in my question is different, this appears to be the same symptom as described in this other SO question.

The problem seems to be related to the fact that the Artifactory plugin will not perform dependency resolution until gradle's execution phase. I had assumed that defining the argument to the zipTree step in the getDependencies task with a closure would have the effect of deferring the dependency resolution until that phase.

But, for this to be deferred by the copy task, I need to define the from configuration of the getDependencies task as a closure, and include the zipTree operation in that closure.

It's the difference between:

from zipTree { configurations.runtimeDep.singleFile } // doesn't work

...and

from { zipTree( configurations.runtimeDep.singleFile ) } // works

Making this change gets the resolve working (with no required maven repositories block).

Another solution is to drop the Artifactory configuration altogether (which I can do in this case because I do not need to utilize anything unique to Artifactory) and use the traditional gradle repositories block, as described in the other SO question and by crazyjavahacking. Doing this makes the build script shorter, and I can leave the zipTree step configured as it was originally written:

repositories {
   maven {
      url "${artifactory_contextUrl}/repo-key"
   }
}

configurations {
    runtimeDep
}

dependencies {
    runtimeDep 'company.com:artifact-id:1.0.0@zip'
}

ext.destination = null
task getDependencies(type: Copy) {
    from zipTree { configurations.runtimeDep.singleFile }
    into ".artifacts/runtime"
}

Upvotes: 1

Crazyjavahacking
Crazyjavahacking

Reputation: 9677

As the Gradle print to the console:

You did not define repositories{} block and so it does not know how to download declared dependency.

Upvotes: 0

Related Questions