使用Gradle构建项目时如何获取实现版本

Mar*_*kus 2 gradle

当项目内置Maven有可能得到实现版本像这样。使用Gradle可以做到这一点吗?

我使用我的应用程序从Maven迁移到Gradle。随着Maven构建我能够显示应用程序的版本一样Application vX.X.X,其中X.X.X从实现版本被检索。Gradle有可能吗?
基本上,我希望该版本已自动解决。

rad*_*tao 5

jar {
    manifest {
        attributes('Main-Class':             'Your main class',
                   'Implementation-Title':   'Your title',
                   'Implementation-Version': 'You version')
    }
}
Run Code Online (Sandbox Code Playgroud)

或者如果您使用 shadowJar

shadowJar {
        manifest {
            attributes 'Main-Class':             'Your main class'
            attributes 'Implementation-Title':   'Your title'
            attributes 'Implementation-Version': 'You version'
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后可以在java源代码中使用:

getClass().getPackage().getImplementationVersion()
getClass().getPackage().getImplementationTitle()
Run Code Online (Sandbox Code Playgroud)

在Gradle文档中查看更多信息:https ://docs.gradle.org/current/userguide/java_plugin.html#sub: manifest