使用maven构建后找不到src / main / resources中的资源 [英] Resource from src/main/resources not found after building with maven

查看:2234
本文介绍了使用maven构建后找不到src / main / resources中的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在我的java应用程序中使用src / main / resources中的配置文件。我正在我的课堂上阅读它:

Hello I'm using a configuration file from src/main/resources in my java application. I'm reading it in my Class like this :

new BufferedReader(new FileReader(new File("src/main/resources/config.txt")));

所以现在我用maven使用 mvn assembly:assembly <构建它/ code>。以下是我的pom.xml中的内容:

So now I'm building this with maven using mvn assembly:assembly. Here is the bit for that in my pom.xml :

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <finalName>TestSuite</finalName>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.some.package.Test</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

因此,当我运行我的应用程序时出现此错误:

So when I run my app I get this error :

src\main\resources\config.txt (The system cannot find the path specified)

但是当我右键点击我装配的罐子时,我可以在里面看到它,任何人都知道我做错了什么?

But when I right click on my assembled jar I can see it inside, anyone knows what I'm doing wrong?

推荐答案

来自 src / main / resources 的资源将被放到类路径的根目录中,因此您需要获取资源为:

Resources from src/main/resources will be put onto the root of the classpath, so you'll need to get the resource as:

new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/config.txt")));

您可以通过查看maven生成的JAR / WAR文件进行验证,因为您会发现 config.txt 在档案的根目录中。

You can verify by looking at the JAR/WAR file produced by maven as you'll find config.txt in the root of your archive.

这篇关于使用maven构建后找不到src / main / resources中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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