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'
            }
            name 'Nexus_Repo'
            def releasesRepoUrl = 'http://<hostname>/repository/<maven-releases>/'
            def snapshotsRepoUrl = 'http://<hostname>/repository/<maven-snapshots>/'
            url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }

The application Gradle ( build.gradle) 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 :'http://<hostaname>/repository/thirdparty/com/mf/nexusgradle/1.0.5/nexusgradle-1.0.5.gradle'






repositories {
        maven {
        url = 'http://<hostname>/repository/groupRepo/'
        }
    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 :

> 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 'http://<hostname>/repository/maven-releases/com/package/appname/1.0.9-SNAPSHOT/maven-metadata.xml'. 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.

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 :

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 Command-Line Interface

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 the parent gradle file properly, is there some scope problem? how to resolve it

If you apply a script plugin, it is executed where you apply it. So you first apply that script plugin that checks whether version ends in snapshot and after that set the version. Move your stuff, so that the version is set first and only after that the script plugin applied.

Hi

I didn’t understand it, On mean I made it work by adding **afterEvaluate ** in the gradle file ( parent gradel ) like the below and it started working
is it the ideal 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 = 'http://<hostname>/repository/<maven-releases>/'
            def snapshotsRepoUrl = 'http://<hostname>/repository/<maven-snapshots>/'
            url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}

}

afterEvaluate is never ideal, it is always just a symptom treatment and often just delays a problem and makes it harder to diagnose or reproduce.

What’s wrong with my suggestion to reorder your actual code?

Just move for example the apply from: ... below the version = ... or the other way around, the version = ... above the apply from: ..., whatever suits you better.

Sorry, I didn’t go it .
As mentioned initially if publishing task is moved from parent Gradle file ( by removing the apply from: URL of parent Gradle hosted in servers ) to application’s build. Gradle file the publish works fine.

But when publishing task is kept in parent Gradle and access it from apply from: URL, whether it is placed before

> project.version = scmVersion.version

or after it doesn’t matter it always binds with false condition (with Ternary operator ) and choose the releaseRepoUrl.

url = project.version.endsWith(‘SNAPSHOT’) ? snapshotsRepoUrl : releasesRepoUrl
`

In case I am missing some thing , let me know .

Oh, it does not work if you move the apply below? That’s right away a bit unexpected to me. Can you provide an MCVE?