如何从同一个包导入资源?

时间:2017-06-21 06:17:18

标签: jenkins groovy

大家早上好,

我一直在努力管理导入资源并失败。 我希望我做错了,你会得到一个答案: - )

我正在尝试在Jenkins中使用groovy共享库。 我遵循了文档中描述的目录结构:

(root)
+- src                     # Groovy source files
|   +- org
|       +- foo
|           +- Bar.groovy  # for org.foo.Bar class
+- vars
|   +- foo.groovy          # for global 'foo' variable
|   +- foo.txt             # help for 'foo' variable
+- resources               # resource files (external libraries only)
|   +- org
|       +- foo
|           +- bar.json    # static helper data for org.foo.Bar

就我而言,我有一个名为resultsProcessor.html的资源(位于bar.json所在的位置)。这是一个html模板。 我还有一个名为ResultsProcessor.groovy的课程(位于Bar.groovy所在的地方)。在这个类中,我想获取resultsProcessor.html的内容来呈现它。

到目前为止,已经有很多小时和很多挫折! 我尝试过使用不同的东西,包括

this.getClass().getResource("resultsProcessor.html")
this.getClass().getResource("/resultsProcessor.html")
this.getClass().getResource("org/foo/resultsProcessor.html")

使用getClassLoader等的一些变体。 每次我得到“null”。

你知道我在这里缺少什么吗? 我是否需要将资源添加到搜索路径?

总的来说,我在导入/类路径上苦苦挣扎。例如,为什么某些导入在本地使用groovy控制台失败,而在Jenkins上运行时它们没有问题。如果你有相关文件或文章建议我会很感激: - )

非常感谢!

1 个答案:

答案 0 :(得分:0)

首先感谢dagget在这里提供帮助。 我正在使用groovy直接使用groovy控制台。 最后我安装了IntelliJ IDE。一旦我将“resources”目录标记为根资源目录,我的代码就可以在本地运行。所以我想我在使用groovy控制台时没有正确设置类路径。

詹金斯仍然失败。 getResource返回null。但这是另一个故事。至少现在我已经畅通了。

谢谢!

相关问题