How can I package a shared gradle configuration?

issue-resolved

(Steven Goris) #1

We want to share dependency versions across projects and repositories because we have two teams and one shared repository.

I created a dependencies.gradle file where I define a map of versions for the project, I got this from stackoverflow: http://stackoverflow.com/a/9547593/2166900. This works within the root project and its subprojects, but I also want to use this file in other projects. I need to be able to package this file and publish it to artifactory.

dependencies.gradle:

ext.libraries = [
    spring_core: "org.springframework:spring-core:3.1",
    junit: "junit:junit:4.10"
]

I tried to create a plugin that I can publish to Artifactory that wraps this file and applies it to the project.
Unfortunately I can’t find the right syntax to apply the file.

class DependencyPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.apply(getClass().getResource("/dependencies.groovy").toURI().toURL());
    }
}

Applying this plugin results in:

Could not find method apply() for arguments [jar:file:/C:/Users/.../.m2/repository/....jar!/dependencies.groovy] on root project 'company-parent'

I also looked at the Groovy ConfigSlurper, but I don’t know how to apply these properties as Gradle ExtraProperties.

Can someone help me with my plugin or learn me a better way to do this?


(Lance Java) #2

Is there a reason why you are not simply doing it in the plugin?

class DependencyPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.with {
           ext.libraries = [
             spring_core: "org.springframework:spring-core:3.1",
             junit: "junit:junit:4.10"
           ]
        }
    }
}

(Steven Goris) #3

Not really, I didn’t know about the with() syntax. This looks quite clean… I thought that I had to use ExternalProperties or apply, but couldn’t get it done. Thank you! This solves my problem.


(Lance Java) #4

Yes… the with { ... } syntax is reasonably new to me too. It’s a great way to start from inline scripts and refactor to a plugin leaving the scripts exactly as they are (rather than changing all x to project.x)