通过 Jenkins CI 构建一个带有外部 jar 的 Maven 项目,而不是集成在 pom.xml 中



我从 bitbucket 中得到了一个遗留项目,它与 Docker 容器上的 Jenkins 挂钩。生成失败,因为外部 jar 只能通过链接使用。

我尝试通过 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 文件中解决这个问题。

编辑:来自詹金斯控制台的错误:

[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 删除了 \",所以你需要 \" 才能留下 "。

最新更新