IntelliJ 中的 "Update Resources" 从目标目录中删除更新的文件

"Update Resources" in IntelliJ deletes updated files from target directory

短版

当我更改我的应用程序中的资源,然后尝试热部署它们时,文件被删除而不是从 target/ 目录中更新,我不明白为什么。

长版

我有一个 Java 8 + Tomcat 8 + Spring Boot + Thymeleaf 项目,我 运行 正在退出 IntelliJ。当我更改文件时,例如 src/main/resources/static/css 目录中的 CSS 文件,以及 运行 Update ResourcesUpdate classes and resources,文件被 删除 来自 target/classes/static/css 而不是更新。 Tomcat 日志中未打印任何关于该文件的内容,IntelliJ 日志(~/Library/Logs/IntelliJIdea13/idea.log 中)中未打印任何关于删除文件的内容...它就消失了。

Tomcat 8 设置为外部应用程序服务器(不是内置的 Spring Boot 嵌入式服务器),配置如下。我在 IntelliJ 运行 配置设置中唯一自定义的是将 CATALINA_BASE 指定为与​​ "Tomcat Base" 相同的值,如下所示:

Tomcat Home: /usr/local/tomcat8
Tomcat Base: /path/to/my/catalina/base
Java Env Vars: CATALINA_BASE=/path/to/my/catalina/base

...如果我不这样做,CATALINA_BASE 设置为 /Users/me/Library/Caches/IntelliJIdea13/tomcat/Unnamed_demo_app,这似乎是我的 real 的非工作克隆catalina base 和我到处都是 404。这可能是一个转移注意力的问题,或者是解开谜团的关键。

额外的,可能没用的信息

这是 idea.log 中的相关(但无趣)输出:

INFO - ij.compiler.impl.CompileDriver - COMPILATION STARTED (BUILD PROCESS)
INFO - j.compiler.server.BuildManager - BUILDER_PROCESS [stdout]: Build process started. Classpath: /Applications/IntelliJ IDEA 13.app/lib/jps-launcher.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/lib/tools.jar:/Applications/IntelliJ IDEA 13.app/lib/optimizedFileManager.jar:/Applications/IntelliJ IDEA 13.app/lib/ecj-4.3.2.jar
INFO - lij.compiler.impl.CompilerUtil -   COMPILATION FINISHED (BUILD PROCESS); Errors: 0; warnings: 0 took 3709 ms: 0 min 3sec

该应用程序配置为执行 WAR 部署,自定义 catalina 基础包含股票 conf/server.xmlconf/web.xml,以及 lib 中提供的各种第 3 方库.

这是我的 POM:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
    <artifactId>myapp</artifactId>
    <version>0.1-BETA</version>
    <packaging>war</packaging>

    <properties>
        <spring-boot-version>1.2.1.RELEASE</spring-boot-version>
        <spring-version>4.1.4.RELEASE</spring-version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${spring-boot-version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>${spring-boot-version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${spring-boot-version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>dojo</artifactId>
            <version>1.10.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <!-- for equals/hash/toString builder -->
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <!-- Spring/Mail integration -->
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <!-- Spring/Mail integration -->
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring-version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <!-- connection pooling -->
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.3.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

我不确定关于这个问题还有什么值得一提的,尽管我觉得我提供的内容没有用。如果有任何我应该添加的信息,请添加评论。

我猜你正在使用 maven 插件。

By default, any src/main/resources folder will be added to the application classpath when you run the application and any duplicate found in target/classes will be removed. This allows hot refreshing of resources which can be very useful when developing web applications. For example, you can work on HTML, CSS or JavaScipt files and see your changes immediately without recompiling your application. It is also a helpful way of allowing your front end developers to work without needing to download and install a Java IDE.

如果您可以直接从 IDEA 启动应用程序(只是 运行 主程序 class),我会这样做。否则,通过配置插件禁用该功能:

<build>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>1.2.3.RELEASE</version>
      <configuration>
        <addResources>false</addResources>
      </configuration>
    </plugin>
    ...
  </plugins>
  ...
</build>

http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html

在对我的 Web 文件(Thymeleaf 模板、静态文件等)进行热交换之前,我必须在 IDEA 中明确提供 src/main/resources 作为运行时 Module Dependency

Update -- 虽然明确设置 src/main/resources 对我来说仍然感觉像是对错误配置的补偿控制,但 did让我更新我的文件。但是,我仍然发现另一个问题,我的 电子邮件模板 无法解析 当打包在 Spring-Boot jar 中时,给我像这样的错误:

org.thymeleaf.exceptions.TemplateInputException: 
    Error resolving template "foo", template might not exist 
    or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:924)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:898)
[...]

我有以下代码,其中有一个前导斜线。当我配置为使用外部 Tomcat 时,前导斜线并不重要,但是当打包在 jar 中时,前导斜线会破坏类路径搜索。

@Bean
public ClassLoaderTemplateResolver emailTemplateResolver(){
    ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
  //emailTemplateResolver.setPrefix("/templates/mail/");   // <-- The problem!
    emailTemplateResolver.setPrefix("templates/mail/");    // <-- The fix!
    emailTemplateResolver.setSuffix(".html");
    emailTemplateResolver.setTemplateMode("HTML5");
    emailTemplateResolver.setCharacterEncoding("UTF-8");
    emailTemplateResolver.setOrder(1);

    return emailTemplateResolver;
}