【问题标题】:Modify the <display-name> in web.xml at build time with Jenkins在构建时使用 Jenkins 修改 web.xml 中的 <display-name>
【发布时间】:2015-04-01 21:37:39
【问题描述】:

我需要处理部署在多个独立环境中的多个服务器中的多个战争文件。我们使用 Jenkins 来构建战争,这些战争是由 Grails 基于从 git repo 下载的代码生成的。战争部署在tomcat中。 tomcat 管理页面显示每个战争的“显示名称”。我想在构建时使用构建的某些标识符修改该显示名称,因此我可以从所有 tomcat 服务器轮询显示名称并检测是否存在版本不同步的任何战争。

我知道“显示名称”设置在 WEB-INF/web.xml 文件中的 &lt;display-name&gt; 标记之间。

所以我的问题是:是否有任何 Jenkins 插件可以在生成战争后修改 web.xml &lt;display-name&gt; 值?或者我可以用来修改给定war文件的特定值的任何现有工具? (所以我可以从 Jenkins 运行它作为最后一个构建步骤,并将其传递给 BUILD_ID)。当然欢迎任何其他选择。

【问题讨论】:

    标签: java git maven grails jenkins


    【解决方案1】:

    显示名称是使用application.properties 中的信息和环境名称生成的。基本上它是这样生成的:"${app.name}-${env.name}-${app.version}"。您可以在编译前修改名称和版本,在 Jenkins 中添加构建步骤。

    例如我们使用 Gant 脚本添加从 Mercurial 提取的额外信息,以构建应用程序的名称和版本。这是我们的脚本:

    // File: scripts/PrepareApplicationProperties.groovy
    target(prepareApplicationProperties: "set application properties") {
        def buildNumber = metadata.'app.number' = System.getenv('BUILD_NUMBER') ?: "0"
        metadata.'app.mercurialRevision' = System.getenv('MERCURIAL_REVISION') ?: "0"
        def mercurialRevisionNumber = metadata.'app.mercurialRevisionNumber' = System.getenv('MERCURIAL_REVISION_NUMBER') ?: "0"
        metadata.'app.version' = metadata.'app.version' + ".$buildNumber.$mercurialRevisionNumber"
        metadata.persist()
    }
    setDefaultTarget(prepareApplicationProperties)
    

    Haki 先生提出了更通用的解决方案: http://mrhaki.blogspot.com.au/2011/02/grails-goodness-one-war-to-rule-them_4229.html 因为他直接操纵了web.xml

    // File: scripts/_Events.groovy
    eventWebXmlStart = { webXmlFile ->
        ant.echo message: "Change display-name for web.xml"
        def tmpWebXmlFile = new File(projectWorkDir, webXmlFile)
        ant.replace(file: tmpWebXmlFile, token: "@grails.app.name.version@",
                value: "${grailsAppName}-${grailsAppVersion}")
    }
    

    【讨论】:

    • 谢谢,我想我会尝试类似_Events.groovy钩子选项,也许将Jenkins环境变量添加到字符串中。
    【解决方案2】:

    按照文章中的步骤在 2.5.0 中对我来说效果并不好。所以我发现这个 _Events.groovy 效果很好:

    eventWebXmlStart = { webXmlFile ->
        ant.echo message: "Change display-name for web.xml"
        def tmpWebXmlFile = new File(projectWorkDir, webXmlFile)
        ant.replace(file: tmpWebXmlFile, token: "${grailsAppName}-${grailsEnv}-${grailsAppVersion}",
                    value: "${grailsAppName}")
    }
    

    并且不需要安装模板的步骤

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      相关资源
      最近更新 更多