Extract repositiories block

I have many separate projects that use the same set of repositiories.

  maven {
  credentials {
    username user
    password pass
  }
  url "url_1"
}

maven {
  credentials {
    username user
    password pass
  }
  url "url_2"
}

I wanted to extract it to the custom plugin in form of variables. The way i want it to work in my projects:

repositories {
    maven repos.repo_1
}

But I can’t get it to work. Error in my project when I want to use it:

Cannot invoke method password() on null object

Snippet from my custom plugin class

class Repos {
     def creds  =  {username "user" password "pass"}

public repo_1 = {credentials creds		url "url_to_my_repo"}

}

I don’t know groovy well. How should this be handled correctly?

All methods/properties in build.gradle are delegated to the Project instance

So repositories { ... } is really project.repositories { ... }

This can be annoying when moving code from build.gradle to a Plugin since it is broken initially

Option 1: Prefix everything with project. This kinda sicks but works
Option 2. Wrap everything in a project.with { ... } closure. This means you can easily copy between build.gradle and a plugin using the exact same code.

Thanks to your advice I managed to do sth like this. I post it as advice to others with similar problem

class myPlugin implements Plugin<Project> {
	void apply(Project project) {
		project.with{
			def creds =  {
				username 'user'
				password 'pass'
			}

			ext.repo_local = {url "myUrl"}

			ext.repo_releases =  {
				credentials creds
				url "myUrl2"
			}

			
		}
	}
}

And usage in projects:

  repositories{
      maven repo_local
      maven repo_releases
}

Second way. In general, defining repositories just as closures. without “maven” before closure solves the problem

class myPlugin implements Plugin<Project> {
        void apply(Project project) {
      
            project.extensions.create("repos", Repos)
      
        }
    }



    class Repos {
        def creds = {
            username 'user'
            password 'pass'
        }

     repo_releases =  {
    				credentials creds
    				url "myUrl2"
    			}

       

    }