代码之家  ›  专栏  ›  技术社区  ›  Luke

Eclipse向src/main/resources添加了**排除模式:如何读取资源文件?

  •  2
  • Luke  · 技术社区  · 7 年前

    我用Eclipse(Oxygen.2版本(4.7.2))和标准的src/main/resources文件夹创建了一个简单的Maven项目,并将其添加到类路径中。问题是Eclipse添加了 ** 到src/main/resources文件夹。以下是一些图片,可以更好地说明这种情况:

    enter image description here enter image description here

    您可以自己再现这种情况,只需记住运行Maven->更新项目。。。

    根据 this 回答这不是一个bug,但它是正确的行为。

    所以问题是: 如何从src/main/resources读取资源文件?

    我不能使用显式路径src/main/resources,因为在编译的结果和 我不能用。获取资源或。自Eclipse以来,getResourceAsStream将排除模式设置为 ** 在那条路上

    2 回复  |  直到 7 年前
        1
  •  5
  •   Luke    7 年前

    我写下这个答案,以防其他人也有同样的问题。

    我发现Eclipse中src/main/resource文件夹上的排除模式是正常的(参见上面链接的答案)。排除意味着不是Eclipse处理src/main/resources文件夹编译,而是Maven(准确地说,是Eclipse的Maven插件M2Eclipse)。在类路径中找不到这些资源的事实是由于pom中存在排除。xml:

    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <!-- these resources will be excluded from the classpath; they will not go in to the target/classes folder and will not be packaged into the artifact -->
            <exclude>**/*</exclude>
        </excludes>
    </resource>
    
        2
  •  -1
  •   Sujeet    6 年前

    第1步。从eclipse步骤2中删除项目。第3步导入项目。转到需要添加为源文件夹的文件夹。

    enter image description here