Java 使用 gradle bootJar 而不是 jar 任务并且在 Jenkins 中构建失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50636093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use the gradle bootJar instead of jar task and build fails in Jenkins
提问by Simo
After I started using the spring boot gradle plugin in my gradle.build file, the build fails on jenkins.
在我的 gradle.build 文件中开始使用 spring boot gradle 插件后,jenkins 上的构建失败。
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE")
Things work fine locally including build, test and webapp runs fine with Jetty. The only problem is the build fails on Jenkins in the task artifactoryPublish
. It says:
一切都在本地运行良好,包括构建、测试和 web 应用程序在 Jetty 上运行良好。唯一的问题是任务中 Jenkins 的构建失败artifactoryPublish
。它说:
File '/var/lib/jenkins/jobs/release-my-project/workspace/build/libs/workspace-0.2.1-SNAPSHOT.jar' does not exists, and need to be published!
Not sure what's going with the gradle artifactoryPublish
task. I think the task comes from Jenkins.
不确定 gradleartifactoryPublish
任务发生了什么。我认为任务来自詹金斯。
Before using the spring boot gradle plugin, my jar task in gradle.build is as follows:
在使用spring boot gradle插件之前,我在gradle.build中的jar任务如下:
jar {
baseName = 'my-project'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class':'com.example.Application'
}
// Exclude manifest signature files
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/LICENSE'
}
Since the spring boot gradle plugin disables the jar task, and replaces it with the bootJar task, so I configured the bootjar task as follows:
由于spring boot gradle插件禁用了jar任务,代之以bootJar任务,所以我配置bootjar任务如下:
bootJar {
baseName = 'my-project'
mainClassName = 'com.example.Application'
// Exclude manifest signature files
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/LICENSE'
}
One thing I noticed from jenkins log is that it says the file workspace-0.2.1-SNAPSHOT.jar
does not exist. Seems like it is looking for the wrong file. Previously, it looked for the correct file my-project-0.2.1-SNAPSHOT.jar
. When I built locally, this jar file was created. Not sure what made jenkins look for workspace-0.2.1-SNAPSHOT.jar
. It is supposed to be my-project
as I did define baseName inside the bootJar task.
我从 jenkins 日志中注意到的一件事是它说该文件workspace-0.2.1-SNAPSHOT.jar
不存在。似乎它正在寻找错误的文件。以前,它会查找正确的文件my-project-0.2.1-SNAPSHOT.jar
. 当我在本地构建时,创建了这个 jar 文件。不知道是什么让詹金斯寻找workspace-0.2.1-SNAPSHOT.jar
。它应该my-project
像我在 bootJar 任务中定义 baseName 一样。
Any idea what's wrong here? Thanks.
知道这里有什么问题吗?谢谢。
回答by Peter Ledbrook
Unless you explicitly define the name of the project, Gradle will use the directory name as the project name. On Jenkins, the project directory is called "workspace". artifactoryPublish
is presumably using the project name to determine the name of the JAR file to publish. That's not good practice if that's the case.
除非您明确定义项目名称,否则 Gradle 将使用目录名称作为项目名称。在 Jenkins 上,项目目录被称为“工作区”。artifactoryPublish
大概是使用项目名称来确定要发布的 JAR 文件的名称。如果是这种情况,那可不是好习惯。
Anyway, you really should set the name of your project. You won't have to explicitly set baseName
on the Jar
tasks then. Simply add a settings.gradlefile in the root of the project, i.e. next to the build.gradlefile, and set its content to:
无论如何,您确实应该设置项目的名称。你不会有明确设置baseName
的Jar
任务,然后。只需在项目的根目录,即build.gradle文件旁边添加一个settings.gradle文件,并将其内容设置为:
rootProject.name = "my-project"
That should hopefully fix the problem, although it really depends on what the artifactoryPublish
task is doing.
这应该有望解决问题,尽管这实际上取决于artifactoryPublish
任务在做什么。