具有自定义 settings.xml 的 Jenkins 声明式管道 [英] Jenkins Declarative Pipeline with custom settings.xml
问题描述
我正在尝试使用 maven 设置 Jenkins 声明性管道.到目前为止,我可以让 maven 运行,但我无法让它使用我定义的 Maven Settings.xml.
I'm trying to set up a Jenkins Declarative Pipeline with maven. So far I can get maven to run, but I can't get it to use my defined Maven Settings.xml.
pipeline{
agent any
tools{
maven 'Apache Maven 3.3'
// without mavenSettingsConfig, my settings.xml is not used. With it, this blows up
mavenSettingsConfig: 'Global Maven Settings'
jdk 'jdk9
}
stages {
stage('Preparation'){
steps{
//code checkout stuff here--this works fine
}
}
stage('Build'){
steps{
sh "mvn clean install -P foo"
}
}
}
}
问题似乎是mavenSettingsConfig.如果没有该属性,我无法弄清楚如何设置 settings.xml,并且我的自定义 maven 东西不起作用.(例如,配置文件 foo.)使用 mavenSettingsConfig,它会爆炸:
The problem seems to be mavenSettingsConfig. Without that property, I can't figure out how to set the settings.xml, and my custom maven stuff doesn't work. (Profile foo, for example.) With the mavenSettingsConfig, it blows up:
错误!源单元WorkflowScript"中规范化"阶段的异常意外 NullpointerException....
BUG! exception in phase 'canonicalization' in source unit 'WorkflowScript' unexpected NullpointerException....
文档中有一个很大的 TODO,它将为此提供一个示例!那我该怎么做呢?
The documentation has a big TODO in it where it would provide an example for this! So how do I do it?
(文档 TODO 位于 https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin.它实际上是说TODO 提供一个带有 Jenkins 声明式管道的示例")
(Documentation TODO at https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin. It actually says "TODO provide a sample with Jenkins Declarative Pipeline")
推荐答案
我的建议是使用 Config File Provider 插件:https://wiki.jenkins.io/display/JENKINS/Config+File+Provider+Plugin
my advice is to use the Config File Provider plugin: https://wiki.jenkins.io/display/JENKINS/Config+File+Provider+Plugin
使用它,您可以在 Jenkins 的配置文件管理"屏幕中定义一次配置文件,然后在您的管道中使用如下代码:
With it, you define your config file once in Jenkins' "Config File Management" screen and then have code like this in your pipeline:
stage('Build'){
steps{
configFileProvider([configFile(fileId: 'my-maven-settings-dot-xml', variable: 'MAVEN_SETTINGS_XML')]) {
sh 'mvn -U --batch-mode -s $MAVEN_SETTINGS_XML clean install -P foo'
}
}
}
希望对你有帮助
这篇关于具有自定义 settings.xml 的 Jenkins 声明式管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!