【问题标题】:jenkinsfile - How do I access custom properties in my pom.xml?jenkinsfile - 如何访问我的 pom.xml 中的自定义属性?
【发布时间】:2018-04-29 15:09:14
【问题描述】:

假设我的 pom.xml 中有一个自定义属性,如下所示:

<properties>
 <app>com.myProject.app</app>
</properties>

如何在我的 jenkinsfile 中访问它?

这个:

def pom = readMavenPom file: 'pom.xml'
def appName = pom.app

返回

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field org.apache.maven.model.Model app

提前致谢!

【问题讨论】:

  • 试试def appName = pom.project.properties.app?
  • 我试过了,它不起作用(我认为)。

标签: maven jenkins groovy pom.xml


【解决方案1】:

我知道两种方法:

  1. 使用properties-maven-plugin 将属性写入文件。在 Jenkinsfile 中使用 readProperties 来读取属性。
    只有在 Maven 运行之后才需要属性时才有效。
    此外,在适当的情况下,属性文件可能是前一次运行的陈旧文件,这很隐蔽,因为无论如何 99.9% 的时间属性值都是正确的。
  2. 使用pom = readMavenPom 'path/to/pom.xml'。然后,像这样访问属性:pom.properties['com.myProject.app']

我更喜欢方法 2:在 POM 中没有额外的插件配置,没有写入文件,更少的排序约束,更少的脆弱性。

【讨论】:

    【解决方案2】:

    在管道样式中,在 Jenkinsfile 中,您可以按如下方式访问该值

    pipeline {
    
    environment {
          POM_APP = readMavenPom().getProperties().getProperty('app')
    }
    
    stages{
        stage('app stage'){
            steps{
                script{
                     sh """
                     echo ${POM_APP}
                     """
                }
            }
        }
    }
    

    Read a maven project file

    【讨论】:

      【解决方案3】:

      试试这个: IMAGE = readMavenPom().getArtifactId() VERSION = readMavenPom().getVersion()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-07
        • 1970-01-01
        • 1970-01-01
        • 2017-09-05
        相关资源
        最近更新 更多