Gradle Error When Using Maven Publishing Task

I am using Gradle version 6.7.1, Currently, in my application I facing an issue with the maven publishing task.

We have kept the publishing task in the central location Gradle file named ( nexusgradle-1.0.5.gradle) and importing it via apply from

the content of the central location Gradle (nexusgradle-1.0.5.gradle) is the below which contain the information of nexus repo for snapshot and release along with user credentials for pushing artefacts to nexus.

apply plugin: 'maven-publish'

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.web
        }
   }
    repositories {
        maven {
            credentials {
                username 'uploader'
                password 'uploaderpassword'
            }
            println 'A message which is logged at QUIET level'
            name 'Nexus_Repo'
            def releasesRepoUrl = '
            def snapshotsRepoUrl = '
            url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}

The application Gradle ( child Gradle file) looks like the one below

import org.apache.tools.ant.filters.ReplaceTokens

plugins {
    id 'war'
    // Add git release plugin for versioning snaphots and release builds
    id 'pl.allegro.tech.build.axion-release' version '1.10.1'
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    // Add Git properties plugin.
    id 'com.gorylenko.gradle-git-properties' version '2.2.0'
    id 'jacoco'
 }

// apply from center location
apply from :'






repositories {
        maven {
        url = '
        }
    jcenter()
    }

test {
    testLogging.showStandardStreams = true
    maxParallelForks = 3

    ignoreFailures = true // to skip test Failures
    testLogging { //logging the test
        exceptionFormat = 'full'
        events "passed", "skipped", "failed"
    }
}

jacoco {
    toolVersion = '0.8.3'
}

jacocoTestReport {
    dependsOn test // tests are required to run before generating the report
    reports {
        xml.enabled true //enabling for generate xml for to capture data in sonarqube server
    }
}

// Customize Git properties plugin.
gitProperties {
    // Change date format in git.properties file.
    dateFormat = "yyyy-MM-dd HH:mm:ssZ"
    dateFormatTimeZone = 'GMT'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot:2.1.4.RELEASE'
    // mutliple import below 
}

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

scmVersion {
    repository {
        directory = project.rootProject.file('.')
    }
}

group = 'com.package'
description = 'appname'
project.version = scmVersion.version
project.ext.timestamp = new Date().format("dd/MM/yyyy HH:mm:ss")

processResources {
    filter ReplaceTokens, tokens:[BUILD_VERSION: project.version, BUILD_TIMESTAMP: project.ext.timestamp]
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

war {
    enabled = true
}



springBoot {
    buildInfo()
}

bootWar {
    archiveClassifier = 'boot'
    mainClassName = 'com.package.appname.SpringBootRunner'
}

when I run the Gradle command for publishing

gradlew clean build publish

The task will fail as the publishing task will try to push artefacts of the snapshot to the release repo instead of the snapshot repo.

  > Configure project :
A message which is logged at QUIET level

> Task :clean UP-TO-DATE
> Task :bootBuildInfo

> Task :compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :generateGitProperties
> Task :processResources
> Task :classes
> Task :bootWar
> Task :war
> Task :assemble
> Task :check
> Task :build
> Task :generateMetadataFileForMavenJavaPublication
> Task :generatePomFileForMavenJavaPublication
> Task :publishMavenJavaPublicationToNexus_RepoRepository FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':publishMavenJavaPublicationToNexus_RepoRepository'.
> Failed to publish publication 'mavenJava' to repository 'Nexus_Repo'
   > Could not PUT ' Received status code 400 from server: Repository version policy: RELEASE does not allow metadata in path: com/package/appname/1.0.9-SNAPSHOT/maven-metadata.xml

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at 

But if I remove the apply from: item and bring the Publishing task to the application Gradle ( child Gradle file) file it will work fine, the build artefact is pushed to snapshot repo without any issue.

> Configure project :
A message which is logged at the QUIET level

> Task :clean UP-TO-DATE
> Task :bootBuildInfo

> Task :compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :generateGitProperties
> Task :processResources
> Task :classes
> Task :bootWar
> Task :war
> Task :assemble
> Task :check
> Task :build
> Task :generateMetadataFileForMavenJavaPublication
> Task :generatePomFileForMavenJavaPublication
> Task :publishMavenJavaPublicationToNexus_RepoRepository
> Task :publish

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See 

BUILD SUCCESSFUL in 29s
10 actionable tasks: 9 executed, 1 up-to-date

Can someone guide me, what mistake I am making when putting the maven publishing task in a parent Gradle file? why child Gradle cant resolve values from parent properly

1 Answer

I found a way to fix it by updating the content of the central Gradle /parent file (nexusgradle-1.0.5.gradle) by adding

afterEvaluate

with the modification, it worked fine. Is it the correct approach? or is there any better way to do it?

apply plugin: 'maven-publish'


afterEvaluate {project ->
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.web
        }
   }
    repositories {
        maven {
            credentials {
                username 'uploader'
                password 'uploaderpassword!'
            }
            name 'Nexus_Repo'
              def releasesRepoUrl = '
            def snapshotsRepoUrl = '
            url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest