How to prevent dependency resolution during configuration


(Jason Pickens) #1

Hi,

I am very new to Gradle. I am trying to write two tasks:

  1. Which will take some archives that have been downloaded manually and put in some directory and then publish them to a repository. This is only run occasionally when that third party archive is updated and we need to use that new version.
  2. Another copy tasks which will download the archives, unpack them, and then copy them to a local directory. This should be run at compile time.

I got part of the way there but it seems that surprisingly the dependencies are being downloaded at configuration time which seems rather odd. This means that nothing works because I can’t even run the first task as the second one cases the dependencies to be downloaded which fails.

Here is what I have got so far:

plugins {
    id 'maven-publish'
}

def mafftGroupId = 'jp.cbrc.mafft'
def mafftArtifactId = 'mafft'
def mafftVersion = '7.388'

def artifactsDir = 'artifacts'
def mafftResourcesDir = 'resources/m2'

// TODO: De-duplicate these
def linux64TarballFile = file("$artifactsDir/mafft-$mafftVersion-linux.tgz")
def linux64TarballArtifact = artifacts.add('archives', linux64TarballFile) {
    classifier = 'linux'
    type = 'tgz'
}
def macZipFile = file("$artifactsDir/mafft-$mafftVersion-mac.zip")
def macZipArtifact = artifacts.add('archives', macZipFile) {
    classifier = 'mac'
    type = 'zip'
}
def win64ZipFile = file("$artifactsDir/mafft-$mafftVersion-win64-signed.zip")
def win64ZipArtifact = artifacts.add('archives', win64ZipFile) {
    classifier = 'win64'
    type = 'zip'
}

publishing {
    publications {
        mafft(MavenPublication) {
            groupId mafftGroupId
            artifactId mafftArtifactId
            version mafftVersion
            artifact linux64TarballArtifact
            artifact macZipArtifact
            artifact win64ZipArtifact
        }
    }

    repositories {
        maven {
            name 'artifactory'
            url 'https://biomatters.jfrog.io/biomatters/libs-release-local'
            credentials {
                username = artifactoryUser
                password = artifactoryPassword
            }
        }
    }
}

// TODO: This should maybe have a type of PublishToMavenRepository but I couldn't figure it out.
task publishMafftToArtifactory {
    dependsOn 'publishMafftPublicationToArtifactoryRepository'
}

configurations {
    mafftArchives
}

dependencies {
    // TODO: Is there a way to just use the artifact files here?
    mafftArchives(group: mafftGroupId, name: mafftArtifactId, version: mafftVersion, classifier: 'linux', ext: 'tgz')
    mafftArchives(group: mafftGroupId, name: mafftArtifactId, version: mafftVersion, classifier: 'mac', ext: 'zip')
    mafftArchives(group: mafftGroupId, name: mafftArtifactId, version: mafftVersion, classifier: 'win64', ext: 'zip')
}

task unpackMafft(type: Copy) {
    // TODO: De-duplicate this too
    configurations.mafftArchives.asFileTree.filter { File f ->
        f.name.endsWith(".zip")
    }.each {
        from(zipTree(it))
    }
    configurations.mafftArchives.asFileTree.filter { File f ->
        f.name.endsWith(".tgz")
    }.each {
        from(tarTree(it))
    }
    rename "mafft-linux64", "Linux64"
    rename "mafft-mac", "OSX"
    rename "mafft-win", "W64"
    into "$mafftResourcesDir"
}

The error is:

FAILURE: Build failed with an exception.

* Where:
Build file '/source/trunk/plugins/MafftPlugin/build.gradle' line: 71

* What went wrong:
A problem occurred evaluating project ':plugins:MafftPlugin'.
> Could not resolve all files for configuration ':plugins:MafftPlugin:mafftArchives'.
   > Could not find mafft-win64.zip (jp.cbrc.mafft:mafft:7.388).

This has got me baffled. I have no idea what to do.

Cheers


(uklance) #2

My post here discusses unzipping via the Copy task and how it forces unzipping and dependency resolution in the configuration phase. See the alternative solution (unzip2) which uses Project.copy {...} in a doLast{...} closure