Maven项目中的getClass().getResourceAsStream() [英] getClass().getResourceAsStream() in maven project

查看:324
本文介绍了Maven项目中的getClass().getResourceAsStream()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的maven项目的pom.xml如下所示:

The pom.xml of my maven project looks as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>groupId</groupId>
  <artifactId>artifactId</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <build>
   <sourceDirectory>src/main/java</sourceDirectory>
   <resources>
    <resource>
     <directory>src/main/resources</directory>
    </resource>
   </resources>
  </build>
</project>

在src/main/resources目录中,我有一个名为test的文件. 在src/main/java目录中,我有一个包含以下行的类:

In the src/main/resources directory I have a file called test. In the src/main/java directory I have a class that contains the following line:

System.out.println(this.getClass().getResourceAsStream("test"));

当代码行在Eclipse中运行时,我将得到输出

When the line of code is run within Eclipse, I get as output

java.io.BufferedInputStream@1cd2e5f 

当我将项目导出为.jar并运行它时,我将得到输出

When I export the project as .jar and run it I get as output

 null

我配置错了吗?

推荐答案

<build><resources>下,请确保具有<include>,如下所示 这将明确告诉maven提取这些文件并将其包括在构建中.

Under <build><resources> make sure to have the <include> as below This will explicitly tell maven to fetch these files and include it in the build.

   <build>
     <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
     </resources>
     ...
  </build>

这篇关于Maven项目中的getClass().getResourceAsStream()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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