无法使用Maven访问资源目录中的文件(使用Eclipse IDE) [英] Can't access to files in resources directory with Maven (with Eclipse IDE)

查看:121
本文介绍了无法使用Maven访问资源目录中的文件(使用Eclipse IDE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



< pre class =lang-xml prettyprint-override> < 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>

<个人资料>
<个人资料>
< id> LOCAL< / id>
< activate>
< activeByDefault> true< / activeByDefault>
< / activation>
<属性>
< env> local< / env>
< / properties>
< / profile>
< / profiles>

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

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

所以,在Eclipse中,我运行(在我的项目文件夹,在包中查看):




  • 运行为> Maven构建>进程资源

  • 运行为> Maven build > process-ressources

  • 运行为> Maven build> compile



使用env:本地



在测试中,我做:运行为> Junit测试案例。
但它失败...
我查看了Maven生成的目标/测试类目录,文件test.txt在那里。



我在项目编译期间错过了一些步骤,或者我的配置有问题吗?



感谢帮助。



EDIT



我尝试使用文件(test.txt)文件(../ test.txt)

解决方案

我喜欢你的问题是

 文件testf = new File(/test.txt); 

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

 文件testf = new File(this.getClass()。getResource(/test.txt).toURI()); 

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

 文件testf =新文件(MyClass.class.getResource(/test.txt).toURI()); 

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


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.

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>

And I made a simple test, in 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) :

  • Run as > Maven build > process-resources
  • Run as > Maven build > process-test-ressources
  • Run as > Maven build > compile

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.

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

Thank for help.

EDIT

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

解决方案

It looks to me like your problem is

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

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屋!

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