如何检查Maven项目依赖项的可用性

时间:2018-07-09 08:24:36

标签: maven jenkins

我需要检查Maven项目依赖项的可用性。如果有什么遗漏,我想制造那部分。我尝试将bash脚本与mvn -o dependency:list一起使用:

mvn -o dependency:list 
| grep ":.*:.*:.*" 
| cut -d] -f2- 
| sed 's/:[a-z]*$//g' 
| grep "xxx" 
| sort -u 
| awk -F: '$1 ~ /xxx/ {print "xxy/" $2, $4}; $1 ~ /yyy/ {print "yyx/" $2, $4}' 
| tr '\n' ' ' 

不幸的是,如果缺少依赖项(从.m2 / repository中删除存储库),则此操作无效,并且仅返回可用的存储库,因为命令首先在不存在依赖项时停止,并跳过下一个模块。

[INFO] user-panel .................................... SUCCESS [  0.613 s]
[INFO] db-commons .................................... SUCCESS [  0.165 s]
[INFO] process-scheduler ............................. FAILURE [  0.056 s]
[INFO] user-panel-main ............................... SKIPPED

接下来,我尝试使用dependency:analyzedependency:treedependency:resolve,但结果相同。我只需要检查所有依赖项的可用性而无需跳过。有什么办法可以存档吗?还是更简单的方法?那是我的完整代码:

import java.io.File 
def PATH='/app/jenkins/bin:/app/jenkins/java/bin:/app/jenkins/maven/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
//I use local repo for simulate offline/ no repo 


def BuildDep(repo, version) {
    node('master'){
        def folder = new File( 'deps' )
        if( !folder.exists() ) {folder.mkdirs()}
        dir('deps'){
            deleteDir()
            rp = "git@githost:" + repo + ".git"
            println "RP = " + rp
            TAG = sh(script: " git ls-remote --tags $rp | tail -1 | awk '{ print \$2}' " , returnStdout: true)
            println "TAG = " + TAG
            checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: rp]], branches: [[name: TAG ]]], poll: false
            sh "pwd && ls -l"
            sh(script: "export PATH=${PATH} && cd . && /app/jenkins/bin/build_deploy ", returnStdout: true)
        }
    }

}

node {
  stage('SCM') {
      deleteDir()
      if ( TAG == '') {
          git '${repoName}'
      } else {
          gitRef = (GIT_SELECTOR == 'TAG' ? "refs/tags/${TAG}" : "refs/heads/${TAG}")
          checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: repoName]], branches: [[name: gitRef ]]], poll: false
      }
  }
  stage('Check dependency'){
    dependency = sh(script: '''export PATH=${PATH} && ~/bin/mvn -o dependency:list | grep ":.*:.*:.*" | cut -d] -f2- | sed 's/:[a-z]*$//g' | grep "xxx" | sort -u | awk -F: '$1 ~ /xxx/ {print "xxy/" $2, $4}; $1 ~ /yyy/ {print "yyx/" $2, $4}' | tr '\n' ' ' ''', returnStdout: true)
    if (dependency.empty) {
            println "NO  DEPENDANCY IS NEEDED"
    } else {
        len = dependency.split(' ').size()
        for (i = 0; i <= len - 1; i += 2 ) {
        r = dependency.tokenize(' ')[i]
        rt = r.substring(r.indexOf("/") + 1);
        v = dependency.tokenize(' ')[i+1]
        isIn = sh(script: "find ~/.m2/repository/ -type d -name ${rt} | xargs ls | grep ${v}" ,returnStdout: true)
        if (isIn.trim() == v) {
            println r + " " +v + " - WE GOT THIS !"
        } else {
            println r + " " +v + " - MISSING - BUILD!",
            BuildDep(r, v);
        }
        }
    }
  }

  stage('Build'){
      sh """export PATH=${PATH} && 
        cd . && /app/jenkins/bin/build_withsonar """
  }

}  

0 个答案:

没有答案