Maven-publish publishing repository in init.gradle?

I followed the instructions at Maven Publish Plugin to enable publishing to my local Nexus server.

In the build.gradle this works:

apply plugin: 'maven-publish'
publishing {
    repositories {
        maven {
            url "localhost:8081/repository/myRepository/"
        }
    }
}

But I don’t want to put the reference to my local repository in project code that will be committed.
If I move the code into init.gradle I get this error:
A problem occurred evaluating initialization script.

Failed to apply plugin [id ‘org.gradle.maven-publish’]
Cannot apply model rules of plugin ‘org.gradle.api.publish.maven.plugins.MavenPublishPlugin’ as the target ‘build’ is not model rule aware

Should this work?
If so, what is missing from init.gradle?

Init scripts do not execute against a Project in the same way that a build script does, instead it delegates to a Gradle instance.

https://docs.gradle.org/current/userguide/init_scripts.html

Try wrapping your code with allprojects:

allprojects {
    // your code
}

Thanks Chris. Problem solved.