Java 无法使用 Maven 访问资源目录中的文件(使用 Eclipse IDE)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19916845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:17:08  来源:igfitidea点击:

Can't access to files in resources directory with Maven (with Eclipse IDE)

javaeclipsemavenjunit

提问by Iraes

I have a problem for a couple of hours, and I tried all the solutions I've found on tutorials. It's simple: I can't access the resource files. I try to open a file I've put in src/main/resourcesand src/test/resources.

我遇到了几个小时的问题,我尝试了在教程中找到的所有解决方案。很简单:我无法访问资源文件。我尝试打开一个我已经放入的文件src/main/resourcessrc/test/resources.

I have a simple Java project and I use Maven, with Eclipse as IDE, with the m2e plugin. I want to use resources filtering with Maven, with different profiles, and there's my POM.xml:

我有一个简单的 Java 项目,我使用 Maven,将 Eclipse 作为 IDE,使用 m2e 插件。我想在 Maven 中使用资源过滤,使用不同的配置文件,这是我的POM.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
    <source>1.5</source>
    <target>1.5</target>
    <debug>false</debug>
    <optimize>true</optimize>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.4</version>
      <configuration>
    <encoding>UTF-8</encoding>
      </configuration>
    </plugin>
  </plugins>

  <filters>
    <filter>src/main/filters/${env}.properties</filter>
  </filters>

  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
    <resource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

<profiles>
  <profile>
    <id>LOCAL</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <env>local</env>
    </properties>
      </profile>
</profiles>

And I made a simple test, in src/java/test :

我在 src/java/test 中做了一个简单的测试:

@Test
public void testOpenResourceFile() {
    File testf=new File("/test.txt");
    assertTrue(testf.exists());
}

So, in Eclipse, I run (on my project folder, in the package view) :

因此,在 Eclipse 中,我运行(在我的项目文件夹中,在包视图中):

  • Run as > Maven build > process-resources
  • Run as > Maven build > process-test-ressources
  • Run as > Maven build > compile
  • 运行方式 > Maven 构建 > 流程资源
  • 运行方式 > Maven build > process-test-ressources
  • 运行方式 > Maven 构建 > 编译

With env: LOCAL

使用环境:本地

In the test, I do: Run as > Junit Test Case. But it fail... I looked in target/test-classes directory generated by Maven, and the file test.txt is there.

在测试中,我这样做:Run as > Junit Test Case。但它失败了......我查看了Maven生成的target/test-classes目录,文件test.txt在那里。

Did I missed some steps during my project's compilation, or is there a problem with my configuration?

我在项目编译过程中是否遗漏了一些步骤,或者我的配置有问题?

EDIT:
I tried with File("test.txt")and File("../test.txt")as well.

编辑:
我试着用File("test.txt")File("../test.txt")为好。

采纳答案by Sam Hanes

It looks to me like your problem is

在我看来你的问题是

File testf = new File( "/test.txt" );

That's looking for a file called test.txtat the root of your computer's filesystem. What you want is the root of the resource tree, which you get with the getResourcemethod:

那是在寻找一个test.txt在你的计算机文件系统的根目录下调用的文件。您想要的是资源树的根,您可以通过以下getResource方法获得:

File testf = new File( this.getClass().getResource( "/test.txt" ).toURI() );

Or, in static context, use the name of the containing class:

或者,在静态上下文中,使用包含类的名称:

File testf = new File( MyClass.class.getResource( "/test.txt" ).toURI() );

Of course you'll need to add error handling to that example.

当然,您需要为该示例添加错误处理。

回答by Marco

I had the same problem, just make sure that the "text.txt" file is in the resources folder of the test project (src/test/resources/text.txt) and not in any other resources folder. Also, you should retrieve the file from the resources folder like this:

我遇到了同样的问题,只需确保“text.txt”文件位于测试项目的资源文件夹(src/test/resources/text.txt)中,而不是在任何其他资源文件夹中。此外,您应该像这样从资源文件夹中检索文件:

this.getClass().getClassLoader().getResource("text.txt").getFile();

If this still does not work then try to set the useSystemClassLoaderfrom the maven-surefire-pluginto false in your pom.xml.

如果仍然不工作,然后尝试设置useSystemClassLoadermaven-surefire-plugin假你pom.xml

http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html

http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html

回答by Abhimanyu Shekhawat

I made this method for this job:

我为这项工作制定了这种方法:

public static String getPropaccess(String key){
    String value="";
    Properties configFile = new Properties();
    try {
        configFile.load(Propaccess.class.getClassLoader().getResourceAsStream("test/resources/config.properties"));
        value = configFile.getProperty(key);
        return value;
    } catch (IOException e) { 
        e.printStackTrace();
    }
    return value;
}

回答by Vandit Upadhyay

Check your <build>tag in pom.xml it should be something like this, faced the same issue and adding this <resources>tag in the <build>worked for me.

检查你<build>在 pom.xml 中的标签,它应该是这样的,面临同样的问题,并在为我工作的时候添加这个<resources>标签<build>

<build>
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>