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?
EDIT:
I tried with File("test.txt")
and File("../test.txt")
as well.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…