What is the correct way to depend on custom artifact dependencies?


(Nathan) #1

I have to download a zip file of jars, and extract them, and use them as dependencies for Project A. I extract them in a task of Project B. I think the right way to do this is to consider all of the jars as an artifact of Project B, so that project A can depend on it. I got this almost working, currently…

Project A has the dependency on Project B’s artifact as:

apply plugin: 'java'

dependencies {
    compile project(path: ':projectB', configuration: 'compile')    
}

And project B is fully custom, like:

def outputDir = file("SOME_PATH")

configurations {
    compile
}

task myTask {
    doLast {
        // in doLast because other stuff happens here
        copy {
            from { zipTree("A ZIP ARCHIVE") }
            into outputDir
        }
    }
}

artifacts {
    compile file: outputDir, name: 'myArtifact', type: 'directory', builtBy: myTask
}

Right now when I run assemble on Project A, gradle does know to run myTask, which means it sees the artifact, but the compilation fails to find any classes/jars in that directory.

My only thoughts on how to proceed:

  • Is there some magical type that needs to be defined in the artifact that will make this happen?
  • Do I need to define a separate artifact for each jar extracted?

My workaround that works, but not ideal, is to have the dependency in Project A defined as:

dependencies {
    def directory =
            project(":projectA").configurations.compile.artifacts.getFiles().getFiles().toArray()[0]
    compileOnly fileTree(directory) {
        builtBy ':projectB:myTask'
    }
}

But that is horrible. What should I do?


(Nathan) #2

Some suggests I have received but will not work:

Enumerate all the jars in the zip file as different artifacts
This is not possible, as there are hundreds of them, and I do not know them at configuration time.

Get rid of the project B, and make the unzipping a task of project A
This does simplify it, but the problem I had there (and the reason I want this change) is that there are multiple projects like project A, and so each one would have its own copy of the unzipped archive.

Unzip it manually, create a repo from the jars, and create a dummy jar with transitive dependencies on all of them
This is a huge amount of work and scripting to get right, and not worth my time

Have myTask merge all the jars into one
Since there are hundreds of jars, this could take a long time on a clean system, and doesn’t seem like the right solution