【问题标题】:Getting version info and build data from grails war file从 grails 战争文件中获取版本信息和构建数据
【发布时间】:2015-11-25 14:21:30
【问题描述】:

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

我想从 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


    【解决方案1】:

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

    您可以使用eventCreateWarStart 事件在战争发生时记录信息。下面是一些可以帮助您入门的示例代码。它从本地 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 获取构建属性,如果它们被用于构建战争。

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

        对于 grails 3,您可以使用 buildProperties 任务将任何自定义信息添加到战争中,例如构建日期、版本信息、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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-07-07
          • 2010-09-14
          • 1970-01-01
          • 2023-04-10
          • 2014-08-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多