I would like to add custom “repository method” to my gradle configuration.
Context: We have our own copy of Maven local repo. But is is not rule. It depends on system environment. It means I can configure gradle repositories this way:
So is it possible to prepare altRepo method that is accessible from repositories section? Is it possible to place that logic and method definition somewhere to buildSrc?
Most of the time, an organization will put this sort of thing into a plug-in or init.gradle script (that’s bundled inside a custom Gradle distribution).
buildSrc is for a single repo usually, so if you were to put something in buildSrc, no other repo would be able to (easily) use it.
ALT_REPO is your organization wide artifact repository? You could put your first block of code into a plug-in, publish it to your ALT_REPO and then in projects that wanted to use your plug-in:
This is not organization wide repo. It is more our framework oriented. And the solution should work also outside of our company.
Each developer can have alternative repository in different director. That is the reason I need to use system environment (‘ALT_REPO’). - Alternative repository is optional and that’s the reason I need to use ‘IF’ statement.
So I’m looking for the how to “extend” somehow RepositoryHandler. There are methods like ‘mavenLocal()’, ‘mavenCentral()’, etc. And I would like to “add” my method ‘altRepo()’ that will return instance of MavenArtifactRepository.
So the question is: is it possible to extend ‘RepositoryHandler’?
The ‘buildscript’ block is very special, and cannot be configured from the outside (at least not from a build script). ‘buildscript { repositories.ext.altRepo = { repositories.maven { … } } }’ should work though.
But what dow you mean by “cannot be configured from the outside (at least not from a build script)”? I configured it in ‘build.gradle’. Does it mean ‘build.gradle’ is NOT build script? Thanks.
Now I would like to move ‘buildscript/repositories’ and ‘repositories’ with and without ‘ext’ block into ‘buildSrc/build.gradle’. But it seams it does not work. Should it work? It is necessary to say I try it on single (not multi) project.
That won’t work because ‘buildSrc/build.gradle’ is a separate build for ‘buildSrc’. You could move it into a separate build script applied with ‘apply from:’, or into a plugin class in ‘buildSrc/src/main/groovy’ applied with ‘apply plugin:’. Not sure if this can be made to work for buildscript repositories though (you’d probably need at least a separate ‘apply from:/plugin:’ within ‘buildscript {}’).
I tried to move the logic into separate build script applied with ‘apply from:’ but it looks like it is executed in different context than main build script. It really seams that ‘buildscript’ is processed different way and currently I did not find elegant way how to hide the ‘IF’ statement from my ‘buildscript/repositories’ and ‘repositories’ sections.
There shouldn’t be any problems to get this to work for the regular ‘repositories’ block. For the buildscript repositories, did you try in the way I mentioned (another ‘apply from:’ within the ‘buildscript’ block)? Not entirely sure if this will work though. The only alternative I can think of is to add both extra properties in ‘init.gradle’ (which, if necessary, could even be shipped as part of a custom Gradle distribution).