可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've created a pipeline and using the embedded groovy pipeline script definition and can't seem to get the pom version. I tried this which works in a groovy console but on in the Jenkins build pipeline script:
def project = new XmlSlurper().parse(new File("pom.xml")) def pomv = project.version.toString()
According to the documentation Jenkins has a $POM_VERSION
but the value doesn't have anything in it when I assign it to a variable and echo it out.
def pomv = "$POM_VERSION"
OR
def pomv = '$POM_VERSION"
Does anyone have an idea?
回答1:
Use readmavenPom
like this:
pom = readMavenPom file: 'pom.xml' pom.version
See Model reference for properties (like the above version).
For this to work, one has to install Pipeline Utility Steps plugin
回答2:
The Getting Started with Pipeline page showed yet another option. It works without additional plugins.
A slightly more robust version can be found in the shared library ces-build-lib (it also has some basic unit tests):
def version() { def matcher = readFile('pom.xml') =~ '(.+?)' matcher ? matcher[0][1] : null }
This always matches the first
tag to be found in the pom.xml
. This should be the version of the maven module or its parent in most cases.
回答3:
I usually use readFile
step instead of new File()
wherever possible.
So it would look like this:
def project = new XmlSlurper().parseText(readFile('pom.xml')) def pomv = project.version.text()
Also make sure the version is not inherited from parent. In that case you would need:
def pomv = project.parent.version.text()
Last but not least be carefull XmlSlurper
is not serializable. You generally want use
@com.cloudbees.groovy.cps.NonCPS
in those cases. I highly recommend reading trough Github tutorial - Serializing Local Variables.
回答4:
回答5:
I usually use the map to do this.
def pomFile = readFile(pomName) def pom = new XmlParser().parseText(pomFile) def gavMap = [:] gavMap['groupId'] = pom['groupId'].text().trim() gavMap['artifactId'] = pom['artifactId'].text().trim() gavMap['version'] = pom['version'].text().trim()
回答6:
I'm just starting with Jenkisfile
and I had the same problem as you. Since XmlSlurper
and XmlParser
are forbidden by default configuration i have crated below function to extract maven version:
String readProjectVersion(String pom) { //Take value of the tag def matcher = pom.trim() =~ /(?s)]*>(.*)/ def pomWithoutProject = matcher[0][1].trim() //remove every tag except , since only project version is not encapsulated in the other tag e.g. dependency, parent, plugin def nonVersionMatcher = pomWithoutProject =~ /(?s)\s*(?!)]*>(.*?)\s*/ def versionTag = nonVersionMatcher.replaceAll("").trim() //Take value of the tag def versionTagMatcher = versionTag =~ /(.*)/ if (versionTagMatcher.matches()) { return versionTagMatcher[0][1] } //no version tag, version should be inherited from parent version def parentVersionMatcher = pomWithoutProject =~ /(?s)\s*.*(.*).*\s*/ return parentVersionMatcher[0][1] }
I have tested this solution against files where parent was declared, version was first statement, version was last statement and with presence of version in nested tags like dependency
, plugin
etc.