如果pom不在项目根目录中,则在管道作业中读取pom版本

时间:2019-02-11 18:39:31

标签: maven groovy jenkins-pipeline

我曾尝试像下面那样使用readMavenPom来获取pom版本,因此,由于pom.xml文件已存在于项目目录的根目录中,因此该命令运行良好。

pom = readMavenPom file: 'pom.xml'

对于我们的某些项目,此pom.xml在项目存储库的根目录中将不可用,而是在父模块中可用,因此在这种情况下,我们将修改groovy脚本,如下所示。 >

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

只有两种可能性,即pom.xml文件将出现在根目录中或位于父模块内部。那么,有没有一种方法可以排除我们每次进行的这种定制?

1 个答案:

答案 0 :(得分:0)

我将使用fileExisits检查文件是否存在于特定位置:

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

奖励:检查多个路径

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
相关问题