Peter
Peter

Reputation: 1021

Getting version info and build data from grails war file

In my application the user has the option to upload a war file to update the software.

I want to get some version information from the war file, before I deploy it to my server. How can I do this?

This information would be useful for me:

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"]

        ]

What information can I get from the war file and how?

Best regards, Peter

Upvotes: 1

Views: 1804

Answers (3)

Sudhir N
Sudhir N

Reputation: 4096

For grails 3 you can use buildProperties task to add any custom information to war such as build date, verion info, git revision etc.

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

See this article for how to do the same http://nimavat.me/blog/grails3-add-custom-build-info-to-war

Upvotes: 0

Arjang
Arjang

Reputation: 729

Grails 3.0.9, in GSP, you can get info from META-INF file. Try these

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

But i don't know how to get build date info.

Upvotes: 1

Sandeep Poonia
Sandeep Poonia

Reputation: 2188

For this purpose you can add a script to the grails application that adds this information to a file whenever the user builds the war. Create a new script file under ./scripts in grails app with name _Events.groovy. Here you can hook into different grails events that gets triggered when an app starts or war gets built.

You can use eventCreateWarStart event to log the information whenever war gets built. Below is some sample code that can help you get started. It fetches the current branch name and commit id from local git and stores the data to a file named 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'
}

There is a grails plugin also that claims to fetch build properties from Hudson/Jenkins, if they are being used for building the war.

Upvotes: 3

Related Questions