无法使用 Maven 访问资源目录中的文件(使用 Eclipse IDE) [英] Can't access to files in resources directory with Maven (with Eclipse IDE)
问题描述
我遇到了几个小时的问题,我尝试了在教程中找到的所有解决方案.很简单:我无法访问资源文件.我尝试打开我放在 src/main/resources
和 src/test/resources
中的文件.
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/resources
and src/test/resources
.
我有一个简单的 Java 项目,我使用 Maven,Eclipse 作为 IDE,带有 m2e 插件.我想在 Maven 中使用资源过滤,使用不同的配置文件,还有我的 POM.xml
:
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
:
<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>
我在 src/java/test 中做了一个简单的测试:
And I made a simple test, in src/java/test :
@Test
public void testOpenResourceFile() {
File testf=new File("/test.txt");
assertTrue(testf.exists());
}
所以,在 Eclipse 中,我运行(在我的项目文件夹中,在包视图中):
So, in Eclipse, I run (on my project folder, in the package view) :
- 运行方式 > Maven 构建 > 流程资源
- 运行方式 > Maven 构建 > process-test-ressources
- 运行方式 > Maven 构建 > 编译
使用环境:本地
在测试中,我这样做:Run as > Junit Test Case.但是失败了...我查看了Maven生成的target/test-classes目录,里面有test.txt文件.
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.
我是否在项目编译过程中遗漏了一些步骤,或者我的配置有问题?
Did I missed some steps during my project's compilation, or is there a problem with my configuration?
我也尝试过 File("test.txt")
和 File("../test.txt")
.
推荐答案
在我看来你的问题是
File testf = new File( "/test.txt" );
那是在您的计算机文件系统的根目录中寻找一个名为 test.txt
的文件.您想要的是资源树的根,您可以通过 getResource
方法:
That's looking for a file called test.txt
at the root of your computer's filesystem. What you want is the root of the resource tree, which you get with the getResource
method:
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.
这篇关于无法使用 Maven 访问资源目录中的文件(使用 Eclipse IDE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!