How to call methods with enum parameter from freemarker

ⅰ亾dé卋堺 提交于 2019-12-13 07:47:30

问题


I want to customize Teamcity notification email with freemarker template.

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

The data model is look like below

<#-- @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" -->

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.

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

The method doc is as below:

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

The BuildArtifactsViewMode's definition is as below,

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

Thanks for your help in advance.


回答1:


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());

and then in the template:

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

and/or you could do this:

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

and then in the template:

${someMethod(MyEnum.FOO)}

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...



来源:https://stackoverflow.com/questions/44712458/how-to-call-methods-with-enum-parameter-from-freemarker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!