在jar依赖项中读取资源文件 [英] Reading resource file inside jar dependency

查看:197
本文介绍了在jar依赖项中读取资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我解释我的问题之前 - 我已经尝试在已经问过的问题中找到解决方案,但是没有一个能够工作;)

Before I explain my problem - I've tried to find solution in already asked questions, but none of them worked ;)

我正在尝试读取文件在jar中,这是另一个项目的依赖项。此文件位于路径下

I'm trying to read file inside jar, which is a dependency for another project. This file is located under path

/src/main/resources/driver

项目打包到jar文件中。来自资源的DRIVER使用pom包含在jar中:

Project is packaged into jar file. DRIVER from resources is included into jar using pom:

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>*</include>
            </includes>
        </resource>
    </resources>

另外,当我解压jar时,我可以看到它被添加到jar的根目录中。

Also, when I unpack jar, I can see that it's added to jar's root directory.

我需要做的是将此文件从jar内部复制到磁盘上的目录。只要项目通过IntelliJ作为依赖项目添加,一切正常。当我在POM中添加它作为依赖项时,我得到一个NullPointerException。

What I need to do, is copy this file from the inside of the jar, to a directory on a disk. It all works fine, as long as the project is added via IntelliJ as a dependent project. When I add it as a dependency in POM, I get a NullPointerException.

我用来加载这个文件的代码如下所示:

A code that I use to load this file look like this:

String sourceFilePath = this.getClass().getClassLoader().getResource(driverFileName).getPath();

我也尝试了不同的方法:

I also tried different approaches:

    String sourceFilePath = this.getClass().getResource("driver").getPath();
    String sourceFilePath = this.getClass().getResource("/driver").getPath();
    String sourceFilePath = this.getClass().getClassLoader().getResource("driver").getPath();
    String sourceFilePath = this.getClass().getClassLoader().getResource("/driver").getPath();

但每次尝试都以相同的结果结束:调用getPath()时出现NullPointerException。

But every try ends with the same result: NullPointerException when calling getPath().

我需要解决方案,因为作为依赖项附加的项目是UI测试的框架。到目前为止,我正在为每个需要它们的新项目添加驱动程序,但这需要时间,对于想要使用此框架的人来说更复杂,并且在发布新版本的驱动程序时会遇到很多麻烦。我希望能够简单地为此框架添加maven依赖项,并能够使用此框架内部的驱动程序。
这里的问题是我需要设置一个带有该驱动程序路径的系统变量。所以一旦需要驱动程序,我就试图将它(代码在框架内)复制到外部位置(某些tmp目录)。

I need solution for that, because the project that is attached as dependency is a framework for UI tests. Until now I was adding drivers to each new project that needed them, but it takes time, makes it more complicated for people who want to use this framework and makes a lot of trouble when new version of driver is released. I want to be able to simply add a maven dependency for this framework and be able to use drivers from the inside of this framework. The problem here is that I need to set a system variable with path to that driver. So once the driver is needed, I'm trying to copy it (code for that is inside framework) to external location (some tmp directory).

任何想法如何解决?

推荐答案

使用目标jar像这样的类加载器

Use target jar classloader like this

ClassInOtherJar.class.getClassLoader().getResourceAsStream("filename.xml") 

这篇关于在jar依赖项中读取资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆