【问题标题】:How to access maven property from Jenkins?如何从 Jenkins 访问 Maven 属性?
【发布时间】:2016-08-15 21:41:39
【问题描述】:

我有一个 Maven 项目,其中我有一个属性。我想从 Jenkins 访问它。

POM sn-p:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <timestamp>${build.time}</timestamp>
    <outputFolder>C:/AutomationTestReports/${project.name}/ExecutionReport${timestamp}</outputFolder>
</properties>

此输出文件夹属性在运行时进行评估。我想在 Jenkins 中访问此属性以导航到根据时间戳生成的输出文件夹。为此,我需要在 Jenkins 中捕获此输出文件夹属性,但我无法做到。

仅使用此输出文件夹,我将能够导航到该文件夹​​以访问结果文件。

问题:如何从 Jenkins 作业中访问 Maven 属性?

【问题讨论】:

  • 根据这篇文章 - blog.codecentric.de/en/2014/07/…,看来我们可以在 Jenkins 作业中访问 pom.xml 的某些方面。如果这行得通,你可以试试吗?也许将此 作为 pom 中的唯一标记,并尝试如图所示访问它?只是一个试验,不知道这是否有效。我没有 Jenkins 可以尝试。
  • 我知道该链接提到了一些标准的 pom 变量,但值得一试 IMO。

标签: maven jenkins maven-3 pom.xml


【解决方案1】:

在当前版本的 Jenkins 中,有一种使用“readMavenPom()”来实现此目的的简单易用的方法。

例如,我想读取 pom.xml 文件的版本。但如果它使用较新的 maven “修订”实践,我需要从 pom.xml 中定义的属性中读取该“修订”值。

      def pomVersion = readMavenPom().version   // read version
      if (pomVersion.equals("\${revision}"))    // using revision strategy, read revision property
      {
        pomVersion = readMavenPom().properties['revision']
      }

所以最好的方法是使用

readMavenPom().properties['PROPETY_NAME']

【讨论】:

    【解决方案2】:

    快速而肮脏的解决方案。

    添加一个'Execute Shell'步骤,并将属性的值赋给一个变量:

    outputFolderVar=$(awk -F '[<>]' '/outputFolder/{print $3}' pom.xml)
    

    【讨论】:

      【解决方案3】:

      Jenkins Maven 作业默认提供一些 maven properties 以在作业配置期间使用,但它不提供对定义到 pom 文件中的更多属性的任何轻松访问。

      因此,需要自定义方法。这是一个使用 groovy 脚本的工作解决方案,可用于在构建时将任何已定义的 Maven 属性转换为新的 Jenkins 变量,然后在其他构建后步骤中进一步使用。

      此解决方案的先决条件是:

      • Groovy 安装在 Jenkins 服务器中(简单步骤,只需download it,解压,设置路径
      • Jenkins Groovy Plugin 安装在 Jenkins 中(简单步骤,Manage Jenkins > Manage Plugins, Available Plugins,安装它,然后在 Manage Jenkins > Configure System > Groovy 下配置它 em>,让它指向上面的安装步骤

      然后您可以将以下内容添加到项目 pom 到 build/plugins 部分:

      <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>properties-maven-plugin</artifactId>
          <version>1.0.0</version>
          <executions>
              <execution>
                  <phase>initialize</phase>
                  <goals>
                      <goal>write-project-properties</goal>
                  </goals>
                  <configuration>
                      <outputFile>${project.build.directory}/build.properties</outputFile>
                  </configuration>
              </execution>
          </executions>
      </plugin>
      

      我们在做什么? 只需使用 properties-maven-plugin 及其 write-project-properties 目标将项目属性写入目标文件夹 (target\build.properties) 下的新文件。注意:此文件中的属性将已被插值
      这个新文件将对您的构建和项目无害,被任何其他构建步骤忽略,作为mvn clean 调用的一部分被删除,但对 Jenkins确实有帮助

      然后,在相关的 Jenkins 作业中,您可以添加一个新的Post Steps > Execute system Groovy script(而不是Execute Groovy script,承担difference)指向一个新的 groovy 文件,您可以在需要的位置存储或在控制台中键入以下 groovy 代码:

      import hudson.model.*;
      import hudson.util.*;
      
      def thr = Thread.currentThread();
      def currentBuild = thr?.executable;
      
      def props = new Properties();
      new File(currentBuild.workspace.toString()+"\\target\\build.properties").withInputStream { 
        stream -> props.load(stream);
      }
      
      println "detected from properties: "+props.get("outputFolder");
      
      def newParamAction = new ParametersAction(new StringParameterValue("outputFolder", props.get("outputFolder")));
      currentBuild.addAction(newParamAction);
      

      此脚本在做什么? 只需读取属性文件,将outputFolder 属性作为日志打印到控制台(可以选择删除),然后将其设置为新的 Jenkins 作业具有相同名称的变量(如果需要可更改)。

      然后,您可以在后续构建步骤中使用 ${outputFolder} 新变量(或 Windows 命令中的 %outputFolder%),它将正确显示。

      例如,您可以通过新的 Post Steps > Execute Windows Batch 命令对其进行调试,然后简单地回显它。这是截图:

      作为示例 Jenkins 作业的输出,您将拥有:

      detected from properties: C:/AutomationTestReports/sample-project/Execution_(2016-04-24_12-11-13UTC)
      [workspace] $ cmd /c call C:\dev\tomcat-7\temp\hudson6024790907972070905.bat
      
      C:\Users\user-name\.jenkins\jobs\sample-maven\workspace>echo C:/AutomationTestReports/sample-project/Execution_(2016-04-24_12-11-13UTC) 
      C:/AutomationTestReports/sample-project/Execution_(2016-04-24_12-11-13UTC)
      
      C:\Users\user-name\.jenkins\jobs\sample-maven\workspace>exit 0 
      Finished: SUCCESS
      

      【讨论】:

      • 让 Jenkins 读取并从那里访问相关属性对 POM 的干扰要小得多。 def pom = readMavenPom file: 'pom.xml'; echo pom.properties['timestamp']
      猜你喜欢
      • 2013-06-07
      • 1970-01-01
      • 2016-02-13
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      相关资源
      最近更新 更多