通过 Jenkins CI 使用外部 jar 构建 Maven 项目,未集成到 pom.xml

Build a Maven project via Jenkins CI with external jar, not integrated in pom.xml

我从 bitbucket 得到了一个遗留项目,在 Docker 容器上连接了 Jenkins。构建失败,因为外部 jar 只能通过 link.

我尝试通过 Jenkins 文件中的 curl 下载 jar 并为其创建了自己的 my-jar-pom.xml:

pipeline {

    agent {docker {image: 'maven: 3.6.3'} }

    tools {
        jdk "jdk-1.8"
    }

    stages {
        stage('myStage') {
            steps {
                step('Get Library') {
                    >&2 echo 'Get Library'
                    sh 'curl -O link-to-my-jar.jar'
                }
                step('Create POM') {
                    sh 'echo "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n
                                 <groupId>my-group-id</groupId>\n
                                 <artifactId>my-artifact-id</artifactId>\n
                                 <version>1.0</version>\n
                             </project>" > my-jar.pom'
                    >&2 echo 'create POM'
                }
                step('Install POM') {
                    mvn install:install-file -Dfile=my-jar.jar -DpomFile=my-jar.pom
                    >&2 echo 'install POM'
                }
                step('Install') {
                    sh 'mvn -B clean install'
                    >&2 echo 'install mvn'
                }
            }
        }
    }
}

Jenkins 文件中发生的所有事情。现在构建仍然失败,我什至无法读取控制台命令之间的回声。 解决这个问题的方法是什么?手动下载文件并以某种方式将其放在 jenkins_home 卷本身?我更愿意在 Jenkins 文件中解决这个问题。

编辑:Jenkins 控制台出现错误:

    [ERROR] Failed to execute goal on project my-project: Could not
resolve dependencies for project my-project:jar:1.0.0: Failure to find my
jar in https://repository.jboss.org/nexus/content/repositories/thirdparty
releases was cached in the local repository, resolution will not be
reattempted until the update interval of thirdparty-releases has elapsed
or updates are forced

你的方法很好。它应该可以工作。

但是您不需要自己创建 POM。你可以让 install:install-file 创建它。只需添加 -DgeneratePom=true.

缺少以下内容: 1) 在我的 Jenkins 的工具部分安装 JDK 2) 在工具部分安装 Maven

然后我将代理更改为 agent any 并将 maven 添加到工具中:

agent any

tools {
    jdk "JDK-8"
    maven "Maven-3.6.3"
}

-DgeneratePom=true 并没有真正解决,可能是我的错,但我发现经过实验并发现这是一个可行的解决方案:

stages {
    stage('AXSUTILS') {
        steps {
            script {
                echo 'Get Library'
                sh 'curl -O http://link.to.my.jar/my-jar/1.0/my-jar.jar'

                echo 'create POM'
                sh 'echo "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xs\"> <groupId>com.xx.xml</groupId> <artifactId>my-artifact</artifactId> <version>1.0</version> </project>" > my-jar.pom'

                echo 'install POM'
                sh 'mvn install:install-file -Dfile=my-jar.jar -DpomFile=my-jar.pom'

                echo 'install mvn'
                sh 'mvn -B -U -X clean install'
            }
        }
    }
}

还要注意遗漏的引号,我花了很多时间意识到 Jenkins 删除了 \" 所以你需要 \" 来保留 "。