Gradle maven-publish plugin doesn't allow to setup version

4k views Asked by At

I need help with publishing an artifact using 'maven-publish' plugin. I use it since my next step is to publish several artifact for projects and 'maven' plugin hardly usable for that case. So please don't suggest to use it. The problem that if I use variable to setup version then gradle fails with error. It works fine if I specify fixed value however If I use variable It doesn't work. How I can pass version from variable to the publication?

Related code:

println "project.version = $project.version"
ext.artifactVersion = String.valueOf(project.version);

println "artifactVersion = $artifactVersion" // to check that value is correct

publishing {
    publications {
        //publishArtifact(project)  
        maven(MavenPublication) {
            groupId project.group
            artifactId project.ext.artifactId

            println artifactVersion
            String artifactVersion2 = artifactVersion
            //version "1248"             // works
            //version artifactVersion    // doesn't work 
            //version "$artifactVersion" // doesn't work
            //version ''+artifactVersion // doesn't work
            //version '1248'             // works
            //version
            version "${artifactVersion}" // doesn't work 
            from project.components.java
        } 
    }
}

Output:

project.version = 1248
artifactVersion = 1248
1248
:prj:generatePomFileForMavenPublication
:prj:compileJava UP-TO-DATE
:prj:processResources UP-TO-DATE
:prj:classes UP-TO-DATE
:prj:jar UP-TO-DATE
:prj:publishMavenPublicationToMavenLocal FAILED
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':prj:publishMavenPublicationToMavenLocal'.
> Failed to publish publication 'maven' to repository 'MavenLocal'
    > Invalid publication 'maven': supplied version does not match POM file (cannot edit version directly in the POM file).
1

There are 1 answers

0
wolfroma On

Thanks all for looking into issue. I found the problem and post here result to hope that it will help someone. The reason of the issue that version cannot contain spaces. In my case version contained trailing space which I didn't noticed. It looks like Gradle trim the value reading it from generated pom and then compare it to check if value changed. Hope this help someone to save the time.

ext.artifactVersion = String.valueOf(project.version).trim();