Is it possible to add custom functionality to extensions?

Let me describe what I would like to archive, I might follow the wrong approach…

I am writing a custom gradle plugin for our enterprise environment. I would like to add an easy repository configuration option. for example with my plugin applied, you can do

repositories {
    aisRepo(artifactory_user,artifactory_password)
}

with user and password read in from ~/.gradle/gradle.properties

Now I would like to do this for the publishing extensions, so you can do the following:

publishing {
    repositories {
        aisPublish(artifactory_user,artifactory_password)
    }
    ....

My current approach doesn’t work. It looks like this:

if (!project.extensions.publishing.repositories.metaClass.respondsTo(project.extensions.publishing.repositories, AIS_FUNCTION_PUBLISH, String, String, String, String)) {
            project.logger.debug "Adding ${AIS_FUNCTION_PUBLISH}(String, String, String?, String?) to project RepositoryHandler"
              project.extensions.publishing.repositories.metaClass."${AIS_FUNCTION_PUBLISH}" = { String pusername,
                                                                                               String ppassword,
                                                                                               String snapshotRepo = DEFAULT_SNAPSHOT_REPO,
                                                                                               String releaseRepo = DEFAULT_RELEASE_REPO ->
                project.extensions.publishing.repositories.maven {
                    name RELEASE_REPO_NAME
                    url DEFAULT_REPO_BASE_URL + releaseRepo
                    credentials {
                        username pusername
                        password ppassword
                    }
                }
                project.extensions.publishing.repositories.maven {
                    name SNAPSHOT_REPO_NAME
                    url DEFAULT_REPO_BASE_URL + snapshotRepo
                    credentials {
                        username pusername
                        password ppassword
                    }
                }
            }
        }

It doesn’t work, because in the moment I access the extension, it is marked as configured and cannot be manipulated anymore in the build script. I always get this

org.gradle.api.InvalidUserDataException: Cannot configure the 'publishing' extension after it has been accessed.

when my build script reaches the publishing section. Is there a way to access the extension without triggering the configured state? (I am only adding a function to the object) Or any other possibility to archive my easy configuration goal?

Cheers,

Andreas

I solved the problem by manipulating the extension class instead of the extension instance.

Thanks for all your help…