Problem with Publishing Task: Cannot set the value of read-only property 'name' and Could not find method url()

Apologies, I am very new to this.

When I try to build with gradle 4.7 from my ./gradlew file, I get this error:

Execution failed for task ':tasks'.
Exception thrown while executing model rule: PublishingPlugin.Rules#publishing(ExtensionContainer)
Cannot set the value of read-only property 'name' for object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication.

When I try to build with gradle 5.2.1, I get this error:

A problem occurred evaluating project ':<foo>'.
Could not find method url() for arguments [<foo_github>] on object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPomScm.

I am following the example for creating a publish task as far as I can tell. What am I missing? What next steps can I take to debug

Here’s my build.gradle:

apply plugin: 'java-library'
apply plugin: 'java-library-distribution'
apply plugin: 'maven-publish'

description = '<foo_description>'
group = "foo.package"
version = "1.0.0-SNAPSHOT"
sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
  mavenCentral()
  mavenLocal()
}

dependencies {
  <foo dependencies>
}

sourceSets {
  main {
    java {
      srcDir 'src/main/java'
    }
  }
}

task sourcesJar(type: Jar) {
    from sourceSets.main.allJava
    classifier = 'sources'
}

task javadocJar(type: Jar) {
    from javadoc
    classifier = 'javadoc'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId = '<foo_maven_artifact_id>'
            from components.java
            artifact sourcesJar
            artifact javadocJar
            pom {
                name = "foo description"
                description = project.description
                url = 'foo url'
                scm {
                  url 'foo github'
                  connection 'foo scm git'
                }
                licenses {
                    license {
                        name = 'The Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id = 'p-garratt'
                        name = 'p-garratt'
                        organization = "my organization"
                        organizationUrl = "my organization url"
                    }
                }
                organization {
                    name = "my organization"
                    url = "my organization url"
                }
            }
        }
    }
    repositories {
        maven {
            name = "buildDir"
            def releasesRepoUrl = "$buildDir/repos/releases"
            def snapshotsRepoUrl = "$buildDir/repos/snapshots"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
        maven {
            name = "sonatype"
            def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
            def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
            credentials {
                username <sonatypeUsername>
                password <sonatypePassword>
            }
        }
    }
}

Hello! Special properties of pom, like ‘name’, ‘url’ and other have been added since Gradle 4.8.
Look docs for it:
https://docs.gradle.org/4.8/userguide/publishing_maven.html - already there
https://docs.gradle.org/4.7/userguide/publishing_maven.html - still not
Therefore, try to update gradle wrapper to 4.8.