简体   繁体   English

使用maven构建后找不到来自src / main / resources的资源

[英]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 / classestarget / 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.

相关问题 Maven 在构建 jar 时损坏 src/main/resources 中的二进制文件 - Maven corrupting binary files in src/main/resources when building jar 如何在Maven中从src / main / resources复制文件? - How do I copy a file from src/main/resources in Maven? 无法从 src/main/resources Java 项目 Maven 获取文件夹 - cannot get folder from src/main/resources Java project Maven src / main / resources中的Maven Jar主类 - Maven Jar main class in src/main/resources 从Maven项目资源文件夹(src / main / resources)加载excel(.xlsx / .xls)文件 - Loading an excel(.xlsx/.xls) file from maven project resources folder (src/main/resources) 与Maven远程资源共享/ src / test / resources下的资源 - Sharing resource under /src/test/resources with maven remote resource Maven将applicationContext.xml从src / main / resources复制到target / myproject / WEB-INF - Maven copying applicationContext.xml from src/main/resources to target/myproject/WEB-INF Eclipse-Maven:更新项目从构建路径中排除 src/main/resources 文件夹 - Eclipse-Maven: Updating project excludes src/main/resources folder from build path Maven:不将文件从src / main / resources复制到类路径根目录-可执行Jar - Maven: Not copying files from src/main/resources to the classpath root - Executable Jar 为什么即使我的xml文件在maven项目的src / main / resources中,也找不到hibernate.cfg.xml? - Why I get hibernate.cfg.xml not found even if my xml file is in src/main/resources in maven project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM