Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-45041

Shared library pluging - can't use getResource to load a resource?

    Details

    • Similar Issues:

      Description

      Hi there,

      I love the idea of using shared libraries! I've been trying to use it - quite successfully - until I started using resources.

      I have a directory layout similar to the one described in the documentation.

      Inside my library I have a class rendering an html template (would be Bar.groovy). This template is in the resource directory (would be bar.json).

      Unfortunately, I just can't manage to retrieve the path of the resource using:

      this.class.getResource("org/foo/bar.json")

      This always returns "null" when executed on a Jenkins server while it does return the correct file path when running locally on my machine.

       

      However, the following works like a charm:

      def request = libraryResource 'org/foo/bar.json'

       

      Is this a bug or am I doing something wrong? Any help much appreciated

        Attachments

          Activity

          bernard87 Bernard Landon created issue -
          bernard87 Bernard Landon made changes -
          Field Original Value New Value
          Issue Type Bug [ 1 ] Story [ 10002 ]
          bernard87 Bernard Landon made changes -
          Issue Type Story [ 10002 ] Bug [ 1 ]
          Hide
          jglick Jesse Glick added a comment -

          You are doing something wrong. libraryResource is documented and works, so what is the issue?

          Show
          jglick Jesse Glick added a comment - You are doing something wrong. libraryResource is documented and works, so what is the issue?
          jglick Jesse Glick made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Resolution Not A Defect [ 7 ]
          Hide
          bernard87 Bernard Landon added a comment -

          Jesse Glick, the issue is the following: in a class, I cannot get a resource from the same package.

          Note: the code I am using works like a charm on my local computer. It only fails when using the shared library plugin on Jenkins (with implicit loading). So there are two possibilities:

          • Either it should also work using the plugin => in this case this a bug.
          • Or some extra configuration should be done => in this case it would be great to document it.

          Getting a resource using "this.class.getResource()" seems to be the standard way to retrieve a resource inside the same package.

          However this requires the "resources" directory to be added to the class path. Is it the case?

          Is the problem a bit clearer?

           

          Show
          bernard87 Bernard Landon added a comment - Jesse Glick , the issue is the following: in a class, I cannot get a resource from the same package. Note: the code I am using works like a charm on my local computer. It only fails when using the shared library plugin on Jenkins (with implicit loading). So there are two possibilities: Either it should also work using the plugin => in this case this a bug. Or some extra configuration should be done => in this case it would be great to document it. Getting a resource using "this.class.getResource()" seems to be the standard way to retrieve a resource inside the same package. However this requires the "resources" directory to be added to the class path. Is it the case? Is the problem a bit clearer?  
          Hide
          jglick Jesse Glick added a comment -

          this requires the "resources" directory to be added to the class path. Is it the case?

          No it is not the case. This resources directory has nothing to do with Java class loader resource loading. It is solely for use by the libraryResource step.

          Show
          jglick Jesse Glick added a comment - this requires the "resources" directory to be added to the class path. Is it the case? No it is not the case. This resources directory has nothing to do with Java class loader resource loading. It is solely for use by the libraryResource step.
          Hide
          bernard87 Bernard Landon added a comment -

          So, in a class, how do you get a resource from the same package then?

           

          Show
          bernard87 Bernard Landon added a comment - So, in a class, how do you get a resource from the same package then?  
          Hide
          jglick Jesse Glick added a comment -

          Put it next to the source file I suppose. Not a tested / supported use case.

          Show
          jglick Jesse Glick added a comment - Put it next to the source file I suppose. Not a tested / supported use case.
          Hide
          bernard87 Bernard Landon added a comment -

          Ok. Let's drop it. If I am the only affected it's not worth it. Thanks Jesse Glick.

          Cheers,

          Show
          bernard87 Bernard Landon added a comment - Ok. Let's drop it. If I am the only affected it's not worth it. Thanks Jesse Glick . Cheers,
          Hide
          jglick Jesse Glick added a comment -

          I mean you can try src/the/pkg/resource.json and the.pkg.SomeClass.getResource('resource.json') may work. But the tested and recommended way to package resources is libraryResource.

          Show
          jglick Jesse Glick added a comment - I mean you can try src/the/pkg/resource.json and the.pkg.SomeClass.getResource('resource.json') may work. But the tested and recommended way to package resources is libraryResource .
          Hide
          ilatypov Ilguiz Latypov added a comment -

          It's a pity libraryResource() is shy about selecting the currently active implicit library's resource.  Instead I get an error,

          hudson.AbortException: Library resource com/COMPANY/FILE.EXT ambiguous among libraries [VER1, VER2]
          

          If the pipeline plugin has a way to select the innermost folder's implicit library, it could apply the same algorithm (or stick to the active version) when resolving the library resource.

          Show
          ilatypov Ilguiz Latypov added a comment - It's a pity libraryResource() is shy about selecting the currently active implicit library's resource.  Instead I get an error, hudson.AbortException: Library resource com/COMPANY/FILE.EXT ambiguous among libraries [VER1, VER2] If the pipeline plugin has a way to select the innermost folder's implicit library, it could apply the same algorithm (or stick to the active version) when resolving the library resource.
          Hide
          jglick Jesse Glick added a comment -

          Ilguiz Latypov’s comment is unrelated to the reported issue, but at any rate: perhaps you are selecting distinct names for the “same” library, which is not wise since it would result in both versions of the library being loaded in one build. Please use the users’ list for advice.

          Show
          jglick Jesse Glick added a comment - Ilguiz Latypov ’s comment is unrelated to the reported issue, but at any rate: perhaps you are selecting distinct names for the “same” library, which is not wise since it would result in both versions of the library being loaded in one build. Please use the users’ list for advice.
          Hide
          gayanviranka Gayan Viranka added a comment - - edited

          I also get into this problem. In my scenario I don't know if the resource file exists and want to get a null value if not exist. this is exact behavior of  
          this.class.getResource("org/foo/bar.json")
          which is always returning null when executed with Jenkins pipeline even the file exists

          With libraryResource there will be an exception which not what I need . It is possible to catch the exception but it will show as an error in grafana. so I need to avoid exception.

          is there a way with libraryResource to return null if resource does not exists

          Show
          gayanviranka Gayan Viranka added a comment - - edited I also get into this problem. In my scenario I don't know if the resource file exists and want to get a null value if not exist. this is exact behavior of   this.class.getResource("org/foo/bar.json") which is always returning null when executed with Jenkins pipeline even the file exists With libraryResource there will be an exception which not what I need . It is possible to catch the exception but it will show as an error in grafana. so I need to avoid exception. is there a way with libraryResource to return null if resource does not exists

            People

            • Assignee:
              Unassigned
              Reporter:
              bernard87 Bernard Landon
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: