Accessing project properties from gradle.properties in buildscript block

In order to put external artifacts in the script classpath I need to configure a repository in the buildscript block. I have separated the repository config into separate gradle file and the base urls into gradle.properties:

build.gradle:

buildscript {scriptHandler->
    apply from: 'gradle/repositories.gradle', to: scriptHandler

repositories.gradle:

repositories{
  maven{
     name "release"
    url "${project.repositoryBaseUrl}/groups/release/"
    }
  maven{
     name "snapshot"
    url "${project.repositoryBaseUrl}/groups/snapshot/"
  }
}

gradle.properties:

repositoryBaseUrl: http://production:8081/nexus/content

Unfortunately this doesn’t work in the buildscript block because there is no project instance available and so also no project property. --David

Accessing project properties set in ‘gradle.properties’ from a ‘buildscript’ block works fine for me. What’s the exact error?

Now as you say it. Yes it works if I access the property directly in the buildscript block. But I have separated the repository config into a separate gradle file so I can apply it everywhere in the project.

the error message is than:

  > Could not find property 'repositoryBaseUrl' on org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepo  sitory_Decorated@60d4801c.  

Try without the ‘project.’.

If I remove the to: sciptHandler fragment from the code than there seems to be no problem with the property. but than there is no repository configured for the scriptHandler.

Property missing:

buildscript {scriptHandler->
    apply from: 'gradle/repositories.gradle', to: scriptHandler

Repositories not configured at scriptHandler, but no property issue:

buildscript {scriptHandler->
    apply from: 'gradle/repositories.gradle'

Try without the ‘project.’. I’m not sure if what you are trying to do is achievable.

I found some oddish workaround. I can configure the repositories in the repositories.gradle file if I do not apply it to the scriptHandler and if I add them as ext.extRepo property to something I don’t know what it is. but than I can use this ext.extRepo property in the buildscript block like so:

repositories.gradle:

repositories{
  maven{
     name "release"
    url "${repositoryBaseUrl}/groups/release/"
    }
  maven{
     name "snapshot"
    url "${repositoryBaseUrl}/groups/snapshot/"
  }
}
ext.extRepo = repositories

build.gradle:

buildscript {scriptHandler->
  apply from: 'gradle/repositories.gradle'
  repositories.addAll(extRepo)
1 Like