[英]Resource from src/main/resources not found after building with maven
Hello I'm using a configuration file from src/main/resources in my java application. 您好,我在Java应用程序中使用的是src / main / resources中的配置文件。 I'm reading it in my Class like this :
我在我的课堂上这样阅读:
new BufferedReader(new FileReader(new File("src/main/resources/config.txt")));
So now I'm building this with maven using mvn assembly:assembly
. 所以现在我要使用
mvn assembly:assembly
用maven构建它。 Here is the bit for that in my pom.xml : 这是我的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? 但是,当我右键单击组装好的罐子时,我可以在里面看到它,有人知道我做错了吗?
Resources from src/main/resources
will be put onto the root of the classpath, so you'll need to get the resource as: 来自
src/main/resources
将放入类路径的根目录,因此您需要以以下方式获取资源:
new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/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生成的JAR / WAR文件来进行验证,因为您会在档案的根目录中找到
config.txt
。
FileReader reads from files on the file system. FileReader从文件系统上的文件读取。
Perhaps you intended to use something like this to load a file from the class path 也许您打算使用类似的方法从类路径加载文件
// this will look in src/main/resources before building and myjar.jar! after building.
InputStream is = MyClass.class.getClassloader()
.getResourceAsStream("config.txt");
Or you could extract the file from the jar before reading it. 或者,您可以在读取文件之前从jar中提取文件。
您在src / main / resources中放入的资源将在构建过程中复制到目标/类,可以使用以下方式访问:
...this.getClass().getResourceAsStream("/config.txt");
Once after we build the jar will have the resource files under BOOT-INF/classes or target/classes folder, which is in classpath, use the below method and pass the file under the src/main/resources as method call getAbsolutePath("certs/uat_staging_private.ppk") , even we can place this method in Utility class and the calling Thread instance will be taken to load the ClassLoader to get the resource from class path. 一旦构建完毕,jar就会在类路径中的BOOT-INF / classes或target / classes文件夹下拥有资源文件,请使用以下方法,并将文件在src / main / resources下作为方法调用getAbsolutePath(“ certs /uat_staging_private.ppk“) ,即使我们可以将此方法放置在Utility类中,调用线程实例也将被加载以加载ClassLoader以从类路径获取资源。
public String getAbsolutePath(String fileName) throws IOException {
return Thread.currentThread().getContextClassLoader().getResource(fileName).getFile();
}
we can add the below tag to tag in pom.xml to include these resource files to build target/classes folder 我们可以在pom.xml中的标签中添加以下标签,以包含这些资源文件来构建target / classes文件夹
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.ppk</include>
</includes>
</resource>
</resources>
I think assembly plugin puts the file on class path. 我认为汇编插件会将文件放在类路径上。 The location will be different in in the JAR than you see on disk.
JAR中的位置将不同于磁盘上的位置。 Unpack the resulting JAR and look where the file is located there.
解压缩生成的JAR并查看文件在此处的位置。
You can replace the src/main/resources/
directly by classpath:
您可以直接通过
classpath:
替换src/main/resources/
classpath:
So for your example you will replace this line: 因此,对于您的示例,您将替换以下行:
new BufferedReader(new FileReader(new File("src/main/resources/config.txt")));
By this line: 通过此行:
new BufferedReader(new FileReader(new File("classpath:config.txt")));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.