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

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

    XMLWordPrintable

    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

          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: