Gradle repositories as project field

2019-08-02 19:40发布

问题:

In gradle, I can specify my repositories with custom layout patterns as

repositories {
  ivy {
    url "http://repo.mycompany.com/repo"
    layout "pattern", {
        artifact "3rd-party-artifacts/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"
        artifact "company-artifacts/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"
        ivy "ivy-files/[organisation]/[module]/[revision]/ivy.xml"
    }
  }
}

Thats fine, but if I also want to use the uploadArchives and buildscript closures, I also need to specify the repositories. My idea was to break out the repositories as a field.

@Field def myRepos = {
  ivy {
    url "http://repo.mycompany.com/repo"
    layout "pattern", {
        artifact "3rd-party-artifacts/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"
        artifact "company-artifacts/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"
        ivy "ivy-files/[organisation]/[module]/[revision]/ivy.xml"
    }
  }
}

This works for

repositories myRepos

but for

buildscript {
  repositories myRepos
}

and

uploadArchives {
  repositories myRepos
}

this gives

No such property: myRepos for class: 'org.gradle.api.internal.initialization.DefaultScriptHandler'

My gradle version is 1.11.

回答1:

Instead of using @Field, you can just do def myRepos = .... The buildscript block is very special, and if you want to share between that and the rest of the build script, you'll have to do ext.myRepos = ... inside buildscript, and refer to it using buildscript.myRepos from the outside.



标签: groovy gradle