资源在 Eclipse 中加载但不在 IntelliJ 中

Resource is loaded in Eclipse but not in IntelliJ

我知道,这里也有人问过类似的问题,但其中 none 似乎正好符合我的问题。我有以下(部分)目录结构:

src
  |__view
       |__TweetView.java
       |__TweetView.html

在TweetView.java中,我正在尝试获取TweetView.html的资源URL,如下所示:

String htmlDocName = "TweetView.html";
Class thisClass = this.getClass();
URL htmlResourceURL = thisClass.getResource(htmlDocName);

这在 Eclipse (Neon.2) 中运行良好,但在 IntelliJ (2017.2.3) 中,htmlResourceURL 始终是 null。我知道 HTML 文件的位置可能不理想(应该放在资源文件夹中),但出于各种原因,我现在不应该更改它。

我观察到 src 文件夹在 IntelliJ 的项目结构中被标记为 "Sources" 文件夹,其子文件夹未明确标记。项目设置不会在 Eclipse 和 IntelliJ 之间共享,因为该项目使用 Maven。注意:关于 运行 项目的问题是直接从各自的 IDE 中作为 Java 应用程序,而不是 JAR/WAR 导出。

我看到 IntelliJ 有这些 "Resource patterns" 定义了哪些文件类型应该被视为资源,但是我无法通过添加 .html 来让它工作,无论有没有通配符(根据 IntelliJ 网站,.html 默认情况下被视为资源)。

有人知道问题出在哪里吗?由于它在 Eclipse 中工作,我希望通过 IntelliJ 中的配置来解决问题,而不是更改代码。

编辑: 这是我的 POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/view</directory>
        </resource>
    </resources>
</build>
<dependencies>
    <dependency>
        <groupId>com.twitter</groupId>
        <artifactId>hbc-twitter4j</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.4.0.M1</version>
    </dependency>
</dependencies>

在 Eclipse 中,如果你只是做 "Run as Java Application",我不认为它实际上执行了 Maven。另一方面,Intellij 通常会检测到您有一个 Maven 项目,并将 运行 必要的任务来获取应用程序 运行ning。

至少,这是我使用两者的经验

There's target/classes/TweetView.html and target/classes/view/TweetView.java

您为源文件(Java 个文件)设置了 src 目录,但将资源设置在 src/view 文件夹中。

因此,视图文件夹中的所有 Java class 都保留在视图文件夹中。视图文件夹中的所有文件(Java 除外)都移动到根 class 路径,因此 class 加载程序不知道从给定的 class

我的建议是将 Java 文件移回 src/main/java 的 Mavens 首选位置,然后您可以将 HTML 文件留在原处