Add Multiple Maven Dependencies Dynamically


(Kevin O'Neal) #1

I have a dynamically generated list of dependencies.

def myRequirments = ['jar1', 'jar2', 'jar3']

I would like to create a maven dependencies on these jars, something like:

buildscript {
    repositories {
            myRequirments.each {
            maven { url it }
            }
    }
}

How would I do this?


(Peter Niederwieser) #2

I don’t understand what you are trying to achieve. Can you elaborate?


(Kevin O'Neal) #3

Peter, I had a bit of a brain glitch when I wrote this post. What I meant to say is that I want to add multiple repository urls.

def myRepos = ['http://repo1', 'http:repo2', 'http:repo3']
buildscript {
    repositories {
            myRepos.each {
               maven { url it }
            }
    }
}

(Peter Niederwieser) #4

This should work fine as long as ‘myRepos’ is declared within ‘buildscript { … }’. The ‘buildscript’ block is (very) special in that it cannot refer to anything that’s declared outside. A good mental model to have is to consider it a separate build script that gets evaluated before the remainder of the build script.


(Kevin O'Neal) #5

When I do the following I receive and warning and it seems that the repositories are not being added to the project.

buildscript {
    repositories {
            ['file:C:\project\subProjectOne\libs',
             'file:C:\project\subProjectTwo\libs',
             'file:C:\project\subProjectThree\libs',
            ].each {
               maven { url it }
            }
    }
}
Converting class org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated to File using toString() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use java.io.File, java.lang.String, java.net.URL, or java.net.URI instead.

(Peter Niederwieser) #6

The use of ‘it’ is wrong. You need to specify an explicit closure parameter after ‘each’.


(Kevin O'Neal) #7

Thanks for the help Peter!

The last problem I had was a simple coding error. There was a space in between the file and the url. The array I was passing to the each looked like:

['file: C:\project\subProjectOne\libs',
             'file: C:\project\subProjectTwo\libs',
             'file: C:\project\subProjectThree\libs',
            ]