Resources in Maven WAR are not being copied automatically

已回答

Hi:

I've created a new Maven project of type Web application (WAR), called "maven-war-sample". I've used the "org.codehaus.mojo.archetypes:webapp-javaee7" archetype.

I want that, as soon as I edit and save a resource, the IDE automatically copies/filters it to the "target/maven-war-sample-1.0-SNAPSHOT" directory. By resource I'm referring to any file in the "webapp" directory.

To do so, I've enabled the option "Settings → Build, Execution, Deployment → Compiler → Build project automatically". This compiles the classes to the "target" directory, but it doesn't copy the resources.

Later, I've enabled the option "Project Settings → Artifacts → maven-war-sample:war exploded → Include in project build". If I build the project manually, then the resources are effectively copied to the desired directory. However, the auto-build feature seems to ignore them.

It's really weird, because I have at least one Java EE project in which this feature worked as expected, without enabling any of the mentioned options (at least explicitly). But yesterday, I removed the ".idea" directory from that project, to try to detect why it worked there but not in other projects. And, suddenly, it stopped working, even restoring the original directory back. I've even deleted the "~/.config/JetBrains" and ".cache/JetBrains" directories, to start from scratch. Same result.

Can you help me, please? I'm using IntelliJ IDEA 2022.1 (221.5080.210), in Debian 11, with OpenJDK 17.0.2.

Thank you.

评论操作 固定链接

>To do so, I've enabled the option "Settings → Build, Execution, Deployment → Compiler → Build project automatically". This compiles the classes to the "target" directory, but it doesn't copy the resources.

This option does not build the war artifact it is expected. Otherwise it would lead to worse user experience problems caused by long delays and wait times of the artifact builds. IDE builds artifact only on explicit build artifact or project actions.

See a related request: https://youtrack.jetbrains.com/issue/IDEA-279885

Why do you need to build the artifact each time. Note that for updating the changed files for a running application there is a dedicated flow.

1
评论操作 固定链接

Andrey Dernov

Andrey Dernov Hi again.

I've followed the guide that you told me and it has helped me. Thank you. However, I've had to make an extra change to my project. I'll explain it in case that it's of help to other users.

So that Wildfly can reload the updated resources (HTML, JS, etc.) without re-deploying the app, all the artifacts (EAR, EJB and WAR) must be exploded, not only the EAR. To make that possible, I've had to do 2 things:

1. Configure the "maven-ear-plugin" with the option "<unpackTypes>ejb,war</unpackTypes>", so that it doesn't compress the resulting EJB and WAR artifacts.

2. Reload the Maven projects, so that IntelliJ detects the change and configures the "Artifacts" section of the project accordingly.

After that, I've been able to update the application successfully.

 

1
评论操作 固定链接

Andrey Dernov Thank you for your answer.

Otherwise it would lead to worse user experience problems caused by long delays and wait times of the artifact builds.

OK, that seems reasonable.

Why do you need to build the artifact each time.

I'm using Wildfly AS as my application server. In the "deployments" directory of it, I've an "exploded" EAR with multiple symbolic links that point to the "target" directories of my Maven projects. That way, I can modify any resource and make that my server detects it immediately.

Note that for updating the changed files for a running application there is a dedicated flow.

That sounds as what I need. I'm used to do it manually (with that symbolic links that I've commented), but if IntelliJ can do all this for me, it may be better. I'll give it a read. Thank you.

 

0
评论操作 固定链接

Thank you for the information!

0

请先登录再写评论。