152

I am running Intellij Ultimate with Tomcat and deploy a war. Everything deploys fine to the webapp directory of tomcat.

When I change a file like an xhtml file, is there a way for tomcat to automatically pick up that change? As of right now I need to copy the file from Intellij and copy it to the directory of where it lives in the tomcat web app directory. I know there are certain files that will need a recompile, but xhtmls are not one of them.

Peter
  • 1,657
  • 2
  • 11
  • 18

6 Answers6

305

This cannot be done if you deploy a war with IntelliJ IDEA. However, it can be if you deploy an exploded war. In IDEA:

  1. open your Tomcat Run/Debug configuration (Run > Edit Configurations)
  2. Go to the "Deployment" tab
  3. In the "Deploy at Server Startup" section, remove (if present) the artifact my-webapp-name:war
  4. Click the add icon, select 'artifact' and then select my-webapp-name:war exploded
  5. Click OK (on the "Select Artifacts to Deploy" dialog)
  6. Still in the Run/Debug Configuration Window, select the "Server" tab
  7. In the middle of that tab, change the "On frame Deactivation" setting to either "Update resources" or "Update Classes and Resources"
    • Update resources: All changed resources (that is, all application components other than the classes) will be updated.
    • Update classes and resources: All changed resources will be updated; changed classes will be recompiled. Note that whether the actual classes will be updated depends on the capabilities web server. If I recall, Tomcat will reload html/xhtml and jsp files, but not Servlets or classes that JSPs or Servlets use. You need to modified Tomcat to use a dynamic classloader for that.
  8. You can also set the "On 'update' " action to the one selected above as well.
    • This determined what happens when you hit the Update Update icon icon (or Ctrl+F10) in the Run window.
    • the "Show dialog" determines if IDEA prompts you each time you hit the update icon
  9. Click OK.

Now anytime you make a change, IDEA will redeploy the changed file(s) when IDEA's frame is deactivated (i.e. loses focus). It does take a second or two, You'll see it in the lower status bar in IDEA. Obviously. you'll still need to refresh your web browser so it fetches the new file (unless of course if the page has an auto refresh of ajax like fetch).

A good combination with Tomcat is to set "On frame deactivation" to "Update Resources" and the "On 'update' action to either "Redeploy" or "Restart Server". That allows static pages to be quickly updated via frame deactivation, and class updated via the 'update' action.

A company named ZeroTurnaround sells JRebel which is a dynamic classloader solution. They also have a five part series on the subject that's very good.

Sandro Marques
  • 89
  • 1
  • 2
  • 10
Javaru
  • 30,412
  • 11
  • 93
  • 70
  • I have error: `Error: Artifact 'my-webapp-name:war exploded' has invalid extension` how to fix ? – Anton Dozortsev Mar 01 '14 at 18:35
  • 3
    I also highly recommend using DCEVM for automatic classes reloading. You can then use "Update classes and resources" to apply any changes to the source code without redeploying the application. See: http://blog.jetbrains.com/idea/2013/07/get-true-hot-swap-in-java-with-dcevm-and-intellij-idea/ . In addition to what this tutorial explains it is required to set the JAVA_HOME in the Setup/Connection - Debug tab of the Tomcat Run Configuration to point to the DCEVM's JRE. – Alex Fedulov Mar 12 '14 at 16:17
  • Is there a way to make this ( redeploy ) happen when the file gets changed outside of intelij? – BenJamin Oct 31 '14 at 21:54
  • Hello. I deploy app as exploded artifact but jsp are not updated when I press Ctrl+F10, classes are hot swaped but not jsp. and in IDEA event log "20:17:09 Loaded classes are up to date. Nothing to reload." is displayed. Does target folder for exploded artifact should be direct in jboss. – yaroslav prokipchyn Nov 08 '14 at 18:19
  • @BenJamin - I do not believe there is. The closest thing would be to have the 'on frame deactivation' setting configured. Then after making external changes, give the IDEA window focus for a moment, and then switch away. – Javaru Nov 10 '14 at 20:42
  • 1
    @yaroslavprokipchyn - I'm not familiar with JBoss and any 'special' requirements it might have. But as far as I know, it should work the same. For tomcat, IDEA creates a Tomcat (context.xml) configuration that tells Tomcat to use the target/{contextName} directory as the webapp root directory. You may want to check the help guide for any JBOss specific information. – Javaru Nov 10 '14 at 20:48
  • @MarkVedder Yeah, that is the sad solution that I've come up with as well. – BenJamin Nov 11 '14 at 21:47
  • 3
    In step 7, you will not (at least in Intellij 14) get the appropriate dropdown-choices unless you have all your artifacts loaded as "exploded". – mortensi Aug 14 '15 at 12:34
  • thanks, I was wondering why reloading did not work anymore for JSPs - reason was I selected the war file in deployment, not the exploded war – Thomas Einwaller Aug 27 '15 at 12:07
  • I think (5) should be "Click Apply". I think OK closes the window. – ChiefTwoPencils Sep 20 '15 at 22:13
  • 1
    @ChiefTwoPencils - Step 5 is clicking OK on the "Select Artifacts to Deploy" dialog (for which there is no "apply"), not the Run/Debug configuration window. I added a clarifying statement to step 5. – Javaru Sep 22 '15 at 15:26
  • If you want to see static data change such as html, jsp, javascript, then use debug( Shift + F9) instead of run (Shift + F10). – Kyung Hwan Min Nov 05 '15 at 01:19
  • The frame deactivation thing makes it reload every time I alt-tab, which brings up that green notification. One's probably better off using "update" with a hotkey instead if they find the notification annoying as I do – Alkanshel Dec 18 '15 at 02:28
  • In my case (IDEA 14.1.4) it takes about 2 minutes to reflect changes made in a source JSP into a target JSP (which is located at 'target/myApp/WEB-INF/views') after pressing Ctrl+10 ('Update resources' action). It seems annoying. – beemaster Mar 24 '16 at 12:49
  • It helps very much! – Simon Wang Jun 02 '18 at 06:06
28

Let me describe the sequence of actions using screenshots:

  1. Open your Tomcat Run/Debug configuration (Run > Edit Configurations)

enter image description here

  1. Click the add icon, select 'artifact' and then select pizza_mvc:war exploded: enter image description here

  2. Modify 'On update action' and 'On frame diactivation': enter image description here

  3. Everytime you want to refresh your resources, press <Ctrl> + F10

Lord Nighton
  • 1,670
  • 21
  • 15
12

If you want to update classes and resources, run Tomcat in Debug mode. In this mode IntelliJ update classes and resources without a problem (hot swap).

Got same problem. IntelliJ didn't update my project, because I run as "Run mode". When I run in debug mode and press Ctrl+F10, it updates classes and resources.

Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
3

I have found a better way.

In Eclipse we have automatically build, compile + reload static files. Actually static files are served directly from src/main/webapp.

In IntelliJ instead of serving directly from src/main/webapp, there is a folder created in target, in which you will have a copy of your src/main/webapp and also other context related .. web-inf/classes /lib.

If You put instead of the target the output of exploded artifact to src/main/webapp, it will not overwrite you source files, and it will serve directly, you won't have to hit - update resources / update resources and classes

So what you need to do in IntelliJ is

  1. modify exploded artifact output directory to src/main/webapp enter image description here
  2. make a macro for save and recompile / compile dirty classes (cmd+shift+f9) enter image description here enter image description here
  3. edit registry and preferences to build automatically and build while app running(cmd+shift+a - registry). enter image description here enter image description here

After this, if you have jobs that change static files from outside IntelliJ, you will see changes immediately without going to IntelliJ and Update Resources, and for the classes you have that macro that saves, and compile(Note: You could also just hit Compile/Recompile : cmd + shift + F9 / cmd + F9 - if you have multiple opened editors will see that your dirty * editors will be non dirty, and changes(classes) will be reloaded)

Radu Toader
  • 1,521
  • 16
  • 23
  • It seems that if you have the registry compiler.automake.. enabled it pushes your changes by only hit save too. – Radu Toader Oct 15 '16 at 18:48
0

That is possible only when you deploy the exploded war folder, enabled hot reloading and start tomcat in debug mode.

These are the steps to achieve this.

  1. open your Tomcat Run/Debug configuration (Run > Edit Configurations)
  2. Go to the "Deployment" tab 3.In the "Deploy at Server Startup" section, remove (if present) the artifact my-webapp-name:war
  3. Click the add icon, select 'artifact' and then select my-webapp-name:war exploded
  4. Click OK (on the "Select Artifacts to Deploy" dialog)
  5. Still in the Run/Debug Configuration Window, select the "Server" tab
  6. Change the "On frame Deactivation" and "On Update Action" settings to either "Update classes and resources"
  7. Apply and save
  8. Go to the normal projects settings by clicking File then Settings OR Pressing CTRL + ALT + S
  9. goto "Build, Execution, Deployment"
  10. Compiler
  11. Select Build project automatically
  12. save
  13. Start tomcat in debug mode
Adindu Stevens
  • 2,947
  • 3
  • 14
  • 20
0

if you have multipartfiles that you want to view/play right away, upload directly into the targetfolder. *(only change the write location to the target folder/static/)

String yourpath = C\\andSoForth
    yourPath + "\\target\\classes\\static\\uploads\\audiofiles\\"

youll be able to view your file immediately without having to restart your IDE

Esther
  • 287
  • 5
  • 11