本文介绍了如何从freemarker调用带有枚举参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用freemarker模板自定义Teamcity通知电子邮件.

I want to customize Teamcity notification email with freemarker template.

从freemarker获取Teamcity数据模型是可行的.

It's feasible to get Teamcity data model from freemarker.

数据模型如下所示

<#-- @ftlvariable name="project" type="jetbrains.buildServer.serverSide.SProject" -->
<#-- @ftlvariable name="buildType" type="jetbrains.buildServer.serverSide.SBuildType" -->
<#-- @ftlvariable name="build" type="jetbrains.buildServer.serverSide.SBuild" -->
<#-- @ftlvariable name="agentName" type="java.lang.String" -->
<#-- @ftlvariable name="buildServer" type="jetbrains.buildServer.serverSide.SBuildServer" -->
<#-- @ftlvariable name="webLinks" type="jetbrains.buildServer.serverSide.WebLinks" -->

<#-- @ftlvariable name="var.buildFailedTestsErrors" type="java.lang.String" -->
<#-- @ftlvariable name="var.buildShortStatusDescription" type="java.lang.String" -->
<#-- @ftlvariable name="var.buildChanges" type="java.lang.String" -->
<#-- @ftlvariable name="var.buildCompilationErrors" type="java.lang.String" -->

<#-- @ftlvariable name="link.editNotificationsLink" type="java.lang.String" -->
<#-- @ftlvariable name="link.buildResultsLink" type="java.lang.String" -->
<#-- @ftlvariable name="link.buildChangesLink" type="java.lang.String" -->
<#-- @ftlvariable name="responsibility" type="jetbrains.buildServer.responsibility.ResponsibilityEntry" -->
<#-- @ftlvariable name="oldResponsibility" type="jetbrains.buildServer.responsibility.ResponsibilityEntry" -->

我知道可以直接通过名称获取bean属性,例如,在jetbrains.buildServer.serverSide.SBuildType中继承了getName()方法,因此我可以通过buildType.name从freemarker模板获取name属性.

I know it's possible to get bean property by its name directly, for example, there is inherited getName() method in jetbrains.buildServer.serverSide.SBuildType, so I can get the name property by buildType.name from freemarker template.

但是,对于下面的方法,我不知道如何从模板获取具有不同参数的buildArtifacts.

However, for method below, I have no idea how to get buildArtifacts with different parameters from template.

方法文档如下:

getArtifacts

@NotNull
BuildArtifacts getArtifacts(@NotNull
                                    BuildArtifactsViewMode mode)
Returns accessor for build artifacts. This accessor checks all necessary permissions for accessing files.
Parameters:
mode - view mode to see artifacts
Returns:
build artifacts viewer

BuildArtifactsViewMode的定义如下,

Enum BuildArtifactsViewMode

VIEW_ALL
          Will show all build artifacts i.e.
VIEW_ALL_WITH_ARCHIVES_CONTENT
          Will show all build artifacts and archives content
VIEW_DEFAULT
          Will show all user-published artifacts
VIEW_DEFAULT_WITH_ARCHIVES_CONTENT
          Will show all user-published artifacts and archives content
VIEW_HIDDEN_ONLY
          Will show only hidden build artifacts

谢谢您的帮助.

推荐答案

我对Teamcity一无所知,但作为模板作者,您依赖公开的内容,而类和枚举不公开在最小的FreeMarker设置中.如果您有能力在Teamcity中配置FreeMarker或添加填充数据模型的Java代码,则可以执行类似的操作(这里我使用基于Configuration的方法,但是您可以将其添加到数据模型也是如此)

I don't know anything about Teamcity, but as a template author, you are reliant on what was exposed to you, and classes and enums aren't exposed in a minimal FreeMarker setup. If you have the power to configurte FreeMarker in Teamcity, or to add Java code that populates the data-model, then you can do something like this (and here I use the Configuration-based approach, but you could add these to the data-model similarly as well):

cfg.setSharedVariable(
        "enums",
        ((BeansWrapper) cfg.getObjectWrapper()).getEnumModels());

,然后在模板中:

${someMethod(enums['com.example.MyEnum'].FOO)}

和/或您可以这样做:

cfg.setSharedVariable(
        "MyEnum",
        ((BeansWrapper) cfg.getObjectWrapper()).getEnumModels().get(MyEnum.class.getName()));

,然后在模板中:

${someMethod(MyEnum.FOO)}

如果Teamcity不允许您执行此类操作,则必须确保与您类似的便利,并希望其他人对此有所了解...

If Teamcity doesn't allow you to do such things, then it's on them to ensure some similar facility to you, and let's hope someone else knows about that...

这篇关于如何从freemarker调用带有枚举参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:33