假设我在 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
提前致谢!
最佳答案
我知道两种方法:
properties-maven-plugin
将属性写入文件。使用 readProperties
在 Jenkinsfile 中读取属性。仅当在 Maven 运行之后才需要属性时才有效。
此外,在适当的情况下,属性文件可能是前一次运行中的陈旧文件,这很隐蔽,因为属性值在 99.9% 的时间内都是正确的。
pom = readMavenPom 'path/to/pom.xml'
.然后,像这样访问该属性:pom.properties['com.myProject.app']
. 我更喜欢方法 2:在 POM 中没有额外的插件配置,没有写入文件,更少的排序约束,更少的脆弱性。
关于maven - jenkinsfile - 如何访问我的 pom.xml 中的自定义属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47323558/