Vivek
Vivek

Reputation: 95

Reading pom version in a pipeline job if pom is not in project root

I have tried using readMavenPom like the following to get the pom version and so for this has been working very well as the pom.xml file has been present in the root of the project directory.

pom = readMavenPom file: 'pom.xml'

For some of our projects, this pom.xml won't be available in the root of the project repository instead it will be available inside the parent module so in that case, we modify the groovy script like the following.

pom = readMavenPom file: 'mtree-master/pom.xml'

There are only two possibilities, either the pom.xml file will present in the root or it will be inside the parent module. So is there a way to rule out this customization that we make every time?

Upvotes: 0

Views: 2502

Answers (1)

gmc
gmc

Reputation: 3990

I'd check if the file exists in a specific location with fileExisits:

def pomPath = 'pom.xml'
if (!(fileExists pomPath)) {
  pomPath = 'mtree-master/pom.xml'
}
pom = readMavenPom file: pomPath

Bonus: Check multiple paths

def pomPaths = ['pom.xml', 'mtree-master/pom.xml']
def pomPath = ''
for (def path : pomPaths) {
  if (fileExists path) {
    pomPath = path
    break
  }
}
// check that pomPath is not empty and carry on

Upvotes: 1

Related Questions