从 grails war 文件获取版本信息和构建数据

Getting version info and build data from grails war file

在我的应用程序中,用户可以选择上传 war 文件来更新软ware。

我想从 war 文件中获取一些版本信息,然后再将其部署到我的服务器。我该怎么做?

这些信息对我有用:

def jversion=[
                "buildDate": grailsApplication.metadata["build.date"],
                "version": grailsApplication.metadata["app.version"],
                "branch": grailsApplication.metadata["GIT_BRANCH"],
                "buildNumber": grailsApplication.metadata["build.number"],
                "gitCommit": grailsApplication.metadata["GIT_COMMIT"]

        ]

我可以从 war 文件中获取哪些信息以及如何获取?

此致, 彼得

为此目的,您可以向 grails 应用程序添加一个脚本,以便在用户构建 war 时将此信息添加到一个文件中。在 grails 应用程序的 ./scripts 下创建一个新的脚本文件,名称为 _Events.groovy。在这里,您可以连接到应用程序启动或 war 构建时触发的不同 grails 事件。

您可以使用 eventCreateWarStart 事件在构建 war 时记录信息。下面是一些示例代码,可以帮助您入门。它从本地 git 获取当前分支名称和提交 ID,并将数据存储到名为 application.properties.

的文件中
eventCreateWarStart = { warName, stagingDir ->
    addBuildInfo("${stagingDir}/application.properties")
}

private void addBuildInfo(String propertyFile) {
    def jVersion = [
            "appName"    : grailsApp.metadata['app.name'],
            "version"    : grailsApp.metadata["app.version"],
            "buildDate"  : new Date(),
            "branch"     : getBranch().trim(),
            "Commit"     : getRevision().trim(),
            "buildNumber": System.getProperty("build.number", "CUSTOM"),
    ]

    File file = new File(propertyFile)
    file.text = ""
    jVersion.each {
        key, value ->
            file.text += "${key}:\t${value}\n"
    }

}

def getBranch() {
    Process process = "git rev-parse --abbrev-ref HEAD".execute()
    process.waitFor()
    return process.text ?: 'UNKNOWN'
}

def getRevision() {
    Process process = "git log --oneline --no-abbrev-commit -1".execute()
    process.waitFor()
    return process.text ?: 'UNKNOWN'
}

还有一个 grails plugin 也声称从 Hudson/Jenkins 获取构建属性,如果它们被用于构建 war。

Grails 3.0.9,在 GSP 中,您可以从 META-INF 文件中获取信息。试试这些

${grails.util.Metadata.current.getApplicationVersion()}
${grails.util.Metadata.current.getEnvironment()}
${grails.util.Metadata.current.getApplicationName()}

但我不知道如何获取构建日期信息。

对于 grails 3,您可以使用 buildProperties 任务将任何自定义信息添加到 war,例如构建日期、版本信息、git 修订版等。

buildProperties {
    inputs.property("info.app.build.date", new Date().format('yyyy-MM-dd HH:mm:ss'))
}

有关如何执行相同操作的信息,请参阅这篇文章http://nimavat.me/blog/grails3-add-custom-build-info-to-war