读取并解析 Jelly 模板中的外部 XML 文件

发布于 2024-11-17 15:24:05 字数 318 浏览 3 评论 0原文

我正在使用 Jenkins 1.410 和 Email-Ext 2.14。我的项目是一个健全性检查,以验证许多其他构建,确保它们的工件正确交付,然后发送摘要电子邮件。没关系。

现在,我正在尝试解析父项目的 POM.xml 并提取一堆依赖项版本,并将它们包含在电子邮件中格式良好的部分中。

问题:在 Jelly 模板中,我如何读取外部 pom.xml(将其作为行集合/数组、xml dom 对象、大字符串等)并提取我需要的属性/属性。鉴于此,我可以将它们格式化为表格或类似的东西。

我需要创建自己的插件(这对我来说是新的)吗?或者这个功能已经存在了吗?

多谢。

I'm using Jenkins 1.410 and Email-Ext 2.14. My project is a sanity check to verify a number of other builds, ensuring that their artifacts are being delivered properly and then to send out a summary email. That's all fine.

Now I'm trying to parse the parent project's POM.xml and extract a bunch of dependency versions, and include them in a nicely formatted section in the email.

The Question: How, in the Jelly template, can I read the external pom.xml (grab it as a collection/array of lines, xml dom object, big string, whatever) and extract the properties/attributes that I need. Given that, I can format them into a table, or something like that.

Do I need to create my own plugin (that'll be new for me)? Or does this functionality already exist?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海之角 2024-11-24 15:24:05

此功能已存在于 Email-ext 插件中。您可以使用 xml 标签库从该插件中配置的 Jelly 脚本读取 XML 文件: http://commons.apache.org/proper/commons-jelly/libs/xml/tags.html

下面是如何解析位于 jenkins 作业工作区中的 pom.xml 文件的示例。一旦文件被读取到 {myxmldoc},它就会成为一个 dom4j 文档,可以与标准 XPATH 表达式一起使用来查询或循环所需的属性和元素:

<j:set var="WORK_SPACE" value="${buildenv.get('WORKSPACE')}"/>
<x:parse xml="${WORK_SPACE}/pom.xml" var="myxmldoc"/> 

<!--select element with particular attribute-->
<x:set var="myvar" select="$myxmldoc/project/build/plugins/plugin[@attribute='abc']"/>

<!--Loop through elements-->
<x:forEach var="myloopvar" select="$myxmldoc/project/build/plugins/plugin">
.....
</x:forEach>

This functionality already exists in Email-ext plugin. You can read XML files from the Jelly script configured in that plugin using xml tag library: http://commons.apache.org/proper/commons-jelly/libs/xml/tags.html

Below is an example how to parse a pom.xml file located in the jenkins job workspace. Once file is read to {myxmldoc} it becomes a dom4j document which can be used with standard XPATH expressions to query or loop through needed attributes and elements:

<j:set var="WORK_SPACE" value="${buildenv.get('WORKSPACE')}"/>
<x:parse xml="${WORK_SPACE}/pom.xml" var="myxmldoc"/> 

<!--select element with particular attribute-->
<x:set var="myvar" select="$myxmldoc/project/build/plugins/plugin[@attribute='abc']"/>

<!--Loop through elements-->
<x:forEach var="myloopvar" select="$myxmldoc/project/build/plugins/plugin">
.....
</x:forEach>
爱的那么颓废 2024-11-24 15:24:05

预备步骤

1. Download common-jelly package from: http://redrockdigimark.com/apachemirror/commons/jelly/binaries/commons-jelly-1.0.zip
2. Extract the files from the zip
3. Copy the following files: commons-jelly-tags-util-1.1.1.jar, commons-jelly-tags-xml-1.1.jar, commons-jelly-tags-fmt-1.0.jar
4. Paste the above files at: <Jenkins server path>\war\WEB-INF\lib
5. Restart Jenkins

XML IS:abc.xml

<sites>
 <site>
  <URL>http://www.google.com</URL>
  <STATUS>200</STATUS>
 </site>
 <site>
  <URL>http://www.yahoo.com</URL>
  <STATUS>200</STATUS>
 </site>
</sites>

读取上面 XML 的 Jelly 代码

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:x="jelly:xml" xmlns:html="jelly:html" xmlns:util="jelly:util">

<j:set var="xmlFilePath" value="${build.getWorkspace().child('abc.xml')}"/>
<util:file name="${xmlFilePath}" var="xmlFileContent" />
<x:parse var="myxmldoc" xml="${xmlFileContent}"/>
<x:set var="allSites" select="$myxmldoc/sites"/>
<table class="border">
 <tr>
  <th class="border1"><b>URL</b></th>
  <th class="border1" width="140px"><b>HTTP Code</b></th>
  <th class="border1" width="140px"><b>Status</b></th>
 </tr>
 <x:forEach var="mysite" select="$allSites/site">
 <j:set var="myURL"><x:expr select='$mysite/URL' /></j:set>
 <j:set var="myStatus"><x:expr select='$mysite/STATUS' /></j:set>
 <tr>
  <td class="border_test_passed">
   <a href="${myURL}">${myURL}</a>
  </td>
  <td class="border_test_total">
   <b>${myStatus}</b>
  </td>
  <td class="border_test_total">
   <j:choose>
    <j:when test="${myStatus=='200'}"><img src="${rooturl}userContent/BSGreen.png" width="15px" /></j:when>
    <j:otherwise><img src="${rooturl}userContent/BSRed.png" width="15px" /></j:otherwise>
   </j:choose>
  </td>                     
 </tr>
 </x:forEach>
</table>

Pre-Steps

1. Download common-jelly package from: http://redrockdigimark.com/apachemirror/commons/jelly/binaries/commons-jelly-1.0.zip
2. Extract the files from the zip
3. Copy the following files: commons-jelly-tags-util-1.1.1.jar, commons-jelly-tags-xml-1.1.jar, commons-jelly-tags-fmt-1.0.jar
4. Paste the above files at: <Jenkins server path>\war\WEB-INF\lib
5. Restart Jenkins

XML IS: abc.xml

<sites>
 <site>
  <URL>http://www.google.com</URL>
  <STATUS>200</STATUS>
 </site>
 <site>
  <URL>http://www.yahoo.com</URL>
  <STATUS>200</STATUS>
 </site>
</sites>

Jelly Code to read above XML

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:x="jelly:xml" xmlns:html="jelly:html" xmlns:util="jelly:util">

<j:set var="xmlFilePath" value="${build.getWorkspace().child('abc.xml')}"/>
<util:file name="${xmlFilePath}" var="xmlFileContent" />
<x:parse var="myxmldoc" xml="${xmlFileContent}"/>
<x:set var="allSites" select="$myxmldoc/sites"/>
<table class="border">
 <tr>
  <th class="border1"><b>URL</b></th>
  <th class="border1" width="140px"><b>HTTP Code</b></th>
  <th class="border1" width="140px"><b>Status</b></th>
 </tr>
 <x:forEach var="mysite" select="$allSites/site">
 <j:set var="myURL"><x:expr select='$mysite/URL' /></j:set>
 <j:set var="myStatus"><x:expr select='$mysite/STATUS' /></j:set>
 <tr>
  <td class="border_test_passed">
   <a href="${myURL}">${myURL}</a>
  </td>
  <td class="border_test_total">
   <b>${myStatus}</b>
  </td>
  <td class="border_test_total">
   <j:choose>
    <j:when test="${myStatus=='200'}"><img src="${rooturl}userContent/BSGreen.png" width="15px" /></j:when>
    <j:otherwise><img src="${rooturl}userContent/BSRed.png" width="15px" /></j:otherwise>
   </j:choose>
  </td>                     
 </tr>
 </x:forEach>
</table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文