【问题标题】:Can't access to files in resources directory with Maven (with Eclipse IDE)无法使用 Maven 访问资源目录中的文件(使用 Eclipse IDE)
【发布时间】:2013-11-11 22:03:13
【问题描述】:

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

我有一个简单的 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>

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

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

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

  • 运行方式 > Maven 构建 > 流程资源
  • 运行方式 > Maven 构建 > process-test-ressources
  • 运行方式 > Maven 构建 > 编译

使用环境:本地

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

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

编辑:
我也试过File("test.txt")File("../test.txt")

【问题讨论】:

  • 欢迎来到 SO!在这种情况下,“失败”是什么意思?你得到一个错误?目录中没有文件?等等?
  • testf.exists() = false 在这种情况下,因此断言使测试失败。如果我尝试读取文件,我会收到 IOException。

标签: java eclipse maven junit


【解决方案1】:

在我看来你的问题是

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

这是在您计算机文件系统的根目录中查找名为 test.txt 的文件。你想要的是资源树的根,你可以通过getResource 方法得到它:

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

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

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

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

【讨论】:

  • @AHK 我添加了一个如何从静态上下文中执行此操作的示例。
  • @Sam Hanes 对我来说,当我通过系统输出打印文件时,文件位置会清楚地显示出来。但是当我执行以下命令时 File file = new File(this.getClass().getResource()("filename").getFile().exists () 它返回 false。知道为什么吗?我一直在努力解决这个问题过去 2 天。
【解决方案2】:

检查 pom.xml 中的 &lt;build&gt; 标记,它应该是这样的,遇到同样的问题,在 &lt;build&gt; 中添加这个 &lt;resources&gt; 标记对我有用。

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

【讨论】:

    【解决方案3】:

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

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

    如果这仍然不起作用,请尝试在您的pom.xml 中将useSystemClassLoader 中的maven-surefire-plugin 设置为false。

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

    【讨论】:

    • 对非文件资源使用URL.getFile() 会静默失败。最好将URL.toURI() 与采用URIFile 构造函数一起使用,因为如果资源不是本地文件路径,则会引发异常。
    【解决方案4】:

    我为这个工作做了这个方法:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-09
      • 2019-05-18
      • 2022-11-19
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多