简体   繁体   English

为什么 Jenkins 无法加载资源?

[英]Why Jenkins fails to load the resources?

Here is the layout of my project:这是我的项目的布局:

src/
  test/
    resources/
        ares/
          file1.xml
          file2.xml

Here is the layout of the Jenkins workspace:这是 Jenkins 工作区的布局:

 my-module/
   target/
     test-classes/
       ares/
         file1.xml
         file2.xml

Under eclipse the tests run without any error.在 Eclipse 下,测试运行没有任何错误。 On Jenkins, the tests just fail.在 Jenkins 上,测试失败了。 Jenkins is unable to locate the resources. Jenkins 无法找到资源。 Below are some output from the test execution:以下是测试执行的一些输出:

Eclipse

MyClass.class.getResourceAsStream(/ares/file1.xml) => java.io.BufferedInputStream@4f4b2f1a
MyClass.class.getResourceAsStream(ares/file1.xml) => null

Thread.currentThread().getContextClassLoader().getResourceAsStream(/ares/file1.xml) => null
Thread.currentThread().getContextClassLoader().getResourceAsStream(ares/file1.xml) => java.io.BufferedInputStream@5d402eeb

MyClass.class.getClassLoader().getResourceAsStream(/ares/file1.xml) => null
MyClass.class.getClassLoader().getResourceAsStream(ares/file1.xml) => java.io.BufferedInputStream@20c87621

Jenkins詹金斯

MyClass.class.getResourceAsStream(/ares/file1.xml) => null
MyClass.class.getResourceAsStream(ares/file1.xml) => null

Thread.currentThread().getContextClassLoader().getResourceAsStream(/ares/file1.xml) => null
Thread.currentThread().getContextClassLoader().getResourceAsStream(ares/file1.xml) => null

MyClass.class.getClassLoader().getResourceAsStream(/ares/file1.xml) => null
MyClass.class.getClassLoader().getResourceAsStream(ares/file1.xml) => null

As you can see Jenkins doesn't find my resource.如您所见,Jenkins 没有找到我的资源。

What am I missing?我错过了什么?

I finally solved my issue.我终于解决了我的问题。 On the classpath, the file is named /ares/file1.xml while in my code I was calling the file /ares/file1.XML .在类路径上,文件名为/ares/file1.xml而在我的代码中我调用文件/ares/file1.XML Did you notice the uppercased XML ?你注意到大写的XML吗?

On Windows, there is no difference since filenames are case insensitive.在 Windows 上,没有区别,因为文件名不区分大小写。 On Linux, it fails because filenames ARE case sensitive.在 Linux 上,它会失败,因为文件名区分大小写。

Final thought, when you code on a platform different from the target platform prefer lower case filenames .最后的想法,当您在与目标平台不同的平台上编码时,更喜欢小写文件名

I had this problem with similar symptoms but different cause and different solution.我遇到了类似症状但不同原因和不同解决方案的问题。

In my case the issue was that the Jenkins server was a Windows machine and the full path on the server to the location of the resources started with C:\\Program Files (x86)\\... with spaces.在我的情况下,问题是 Jenkins 服务器是一台 Windows 机器,服务器上到资源位置的完整路径以C:\\Program Files (x86)\\...开头,带有空格。 Those spaces get encoded to %20 if you need to get is as a File instead of a stream using new File(getClass().getResource(fileName).getFile()) .如果您需要使用new File(getClass().getResource(fileName).getFile())这些空间编码为%20作为File而不是流。 Here fileName is a string that contains the name of the resource.这里的fileName是一个包含资源名称的字符串。 I solved the problem by adding a call to URLDecoder.decode .我通过添加对URLDecoder.decode的调用解决了这个问题。 This creates no problems when there are no spaces or you're not on Windows (as far as I've seen) but it solves the problem if you get a space in the name somewhere along the line.当没有空格或您不在 Windows 上时(就我所见),这不会产生任何问题,但是如果您在名称中的某处有空格,则可以解决问题。 The full call is then:完整的调用是:

 new File(URLDecoder.decode(getClass().getResource(fileName).getFile(), "UTF-8"))

I pieced this together from several questions, but none put it all together for the Jenkins case, hence my answer here.我从几个问题中拼凑起来,但没有人将 Jenkins 案例放在一起,因此我在这里回答。 Other relevant Q&A:其他相关问答:

Since I got here twice before I solved my problem after half a day of confusion I thought I'd add what was my solution to help a future traveller.由于我在半天的困惑之后解决了我的问题之前来过这里两次,我想我会添加我的解决方案来帮助未来的旅行者。

Spaces in job names on Jenkins will break the loading of resource files in Java projects. Jenkins 作业名称中的空格会中断 Java 项目中资源文件的加载。 Unless you specifically write your loading code differently just to appease Jenkins.除非你为了安抚詹金斯而专门以不同的方式编写加载代码。

I have boiled this down to one very frustrated piece of advice: DO NOT PUT SPACES IN JENKINS JOB NAMES FOR JAVA PROJECTS.我把这归结为一个非常沮丧的建议:不要在 JENKINS 的 JAVA 项目的工作名称中放置空格。

You can put together a couple of points...你可以把几个点放在一起......

One, if you put a space in the name of a jenkins job then jenkins does no fangling to hide that space.一,如果您在 jenkins 工作的名称中放置一个空格,那么 jenkins 不会有意隐藏该空格。 So call your job "My Awesome New Job" and the path to the job becomes /home/jenkins/My%Awesome%New%20Job因此,将您的工作称为“我很棒的新工作”,该工作的路径变为/home/jenkins/My%Awesome%New%20Job

Two, if your resource path has %20 in it then your code will need to URL decode the resource path in order to be able to load the resource.二,如果您的资源路径中有 %20,那么您的代码将需要对资源路径进行 URL 解码,以便能够加载资源。 As in this other answer that got me there: https://stackoverflow.com/a/43269197/222163就像在另一个让我到达那里的答案中一样: https : //stackoverflow.com/a/43269197/222163

In my situation these resource files are only for tests so I just replaced the space in my job name with a hyphen.在我的情况下,这些资源文件仅用于测试,所以我只是用连字符替换了我的工作名称中的空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM