Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

Under eclipse the tests run without any error. On Jenkins, the tests just fail. Jenkins is unable to locate the resources. 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.

What am I missing?

like image 648
Stephan Avatar asked Nov 13 '15 11:11

Stephan


People also ask

How do I resolve out of memory error in Jenkins?

This error can be caused by including a lot of screenshots in test cases. To resolve this error we recommend increasing the heap size memory in the Jenkins job.

Where do you find errors in Jenkins?

In Jenkins, in the pipeline where failure occurred, in the pane, select the latest build, and click Console Output. On the Console Output page, check the logs to find the reason for the failure.


1 Answers

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. 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()). Here fileName is a string that contains the name of the resource. I solved the problem by adding a call to 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. 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. Other relevant Q&A:

  • Java: how to get a File from an escaped URL?
  • How do you unescape URLs in Java?
  • Resource files not found from JUnit test cases
like image 82
Brick Avatar answered Sep 21 '22 17:09

Brick