2

我需要使用“maven-publish”插件发布工件的帮助。我使用它是因为我的下一步是为项目发布几个工件,而“maven”插件几乎无法用于这种情况。所以请不要建议使用它。如果我使用变量来设置版本然后 gradle 失败并出现错误的问题。如果我指定固定值,它工作正常但是如果我使用变量它不起作用。如何将版本从变量传递到出版物?

相关代码:

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
        } 
    }
}

输出:

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).
4

1 回答 1

3

感谢大家调查问题。我发现了问题并在此处发布结果,希望对某人有所帮助。版本不能包含空格的问题的原因。在我的情况下,版本包含我没有注意到的尾随空格。看起来 Gradle 修剪从生成的 pom 中读取的值,然后比较它以检查值是否更改。希望这可以帮助某人节省时间。

ext.artifactVersion = String.valueOf(project.version).trim();
于 2014-11-26T15:44:02.067 回答