extract pom version in a jenkins pipeline

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

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:

You can try readMavenPom function that is available.

More info is here: https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readmavenpom-read-a-maven-project-file



回答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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!