Reputation: 783
My company has elected to go with IntelliJ for development and deployment to a local Tomcat server. I have been doing my development on gVim, and have no desire to change.
When we were using eclipse I was able to have eclipse republish resources when the file was changed externally. ( I beleive the setting was Refresh using native hooks or polling) This was wonderful because I prefer to use command line tools to integrated ones.
We are using IntelliJ to manage the server, which I'm okay with, however I would like to not disrupt my current workflow.
I know IntelliJ stays in synch pretty well, and will update the files when I focus on it, and refresh them when I leave the frame.
Is there a way to have intelliJ update resources that have been changed externally ( Gvim, cli git... ext ) without moving to it?
Upvotes: 5
Views: 1150
Reputation: 13309
Note: solution provided works for Ubuntu (14.04) + Sublime Text 2/3
I've ended up with a very tricky trick but I'm very happy about it. IntelliJ Idea updates resources in two cases (if enabled in settings):
So I've started with the first case looking for an utility for my operation system (Ubuntu 14.04), which can focus IntelliJ Idea window and the un-focus it, to make it update the resources. I've stumbled upon xdotool, which allows to get the id of the window by it's title and to focus a window by it's id. I've created a simple .sh script:
current_id="$(xdotool getactivewindow)" # get id of current window
idea_id="$(xdotool search --name 'IntelliJ IDEA')"
xdotool windowfocus "${idea_id}" # switch to idea
xdotool windowfocus "${current_id}" # switch to current window
windowfocus
just focuses the window, which means if the window is on the background or on another workspace, you won't get switched to it, you'll stay where you were, but the window will just obtain a focus. But it did not work with IntelliJ Idea, resources were not updating, seems that it wants full attention.
So I've tried to replace windowfocus
with windowactivate
- it switches to the window whenever it is (even if it is on another workspace) so it becomes focused, active and visible. But in combination with switching back to the original process, it produces like a visible switch between two windows (like when you Alt+Tab between windows), which is pretty noticeable, but it actually does it's job - resources get updated.
There was also a second option left - using a shortcut. xdotool has an option to send a keystroke to a window by it's id as well:
xdotool key --window "{$idea_id}" --clearmodifiers CTRL+F10
but it did not work, even in combination with focusing the window, also leaving behind an after-effect of a pressed Ctrl key. There is a section Sendevent Notes in xdotool docs, telling that a key-event generated by xdotool sets a special flag, and as a result an application which receives the event may analyze it for a flag presence and ignore the event, which might be the case with IntelliJ Idea.
After some research I've found another key-sending tool - xvkdb - and it worked like a charm, because I guess it uses another way of sending/generating a key event:
xvkbd -window "*IntelliJ IDEA*" -text "\C\[F10]"
When run alone this command keeps the focus on a Intellij Idea window, I've used xdotool to bring the focus back to a current process. Here is a final version of the script:
current_id="$(xdotool getactivewindow)"
xvkbd -window "*IntelliJ IDEA*" -text "\C\[F10]"
xdotool windowfocus "${current_id}"
To integrate it with Sublime Text 3, which I use as a code editor, I've installed a plugin SublimeOnSaveBuild, which can run a custom build (terminal command or a script) when one saves a file in Sublime Text. Here is a simple build setup:
{
"shell_cmd": "/path/to/script/idea_update_resources.sh"
}
So now I have an Intellij Idea open on another workspace, and when I save a file in Sublime Text, resources get updated by sending a shortcut to Intellij Idea window. The time of focusing the windows using this implementation is not noticeable as I can say. As a result it is way more faster then Grunt or Gulp task, I would say instant, because IntelliJ Idea does an update by itself, and also it is safe to use in case if IntelliJ Idea does some extra operations when updating resources. Thanks @BenJamin for the question, hope it will help somebody.
Update:
After using this setup for a while it is not working as expected: if IntelliJ Idea is on another workspace or it's interface is fully covered with another window, the solution with xvkdb will unfortunately fail and won't execute resources update. I switched back to using windowactivate
.
Upvotes: 1
Reputation: 783
Sadly, I have been unable to find a way to monitor external changes and update a Tomcat server inside IntelliJ.
I have implemented a workaround using grunt. ( a node.js task runner )
The theory behind this workaround is simple:
Some benefits of doing it this way are:
Down side is
To learn more about grunt and get it set up you can visit gruntjs.com.
This workaround uses two plugins
First you set up a task that copies your files into your exploded war. This will use the grunt-contrib-copy plugin and will probably take some finagling, but it isn't too bad. Mine looks like this:
copy:{
webfilesToOutdir:{
files: [
{expand: true, src: ['WebContent/**'],
dest: '../out/artifacts/attensity-q/exploded/attensity-q.war',
rename: function(dest, src){
var ret = dest+"/"+ src.replace('WebContent/', '');
return ret;
}
}
]
}
}
Next you will need a task to watch your files and run the copy when something changes. This will use grunt-contrib-watch and again it is nothing to bad, you will just need to adjust the paths. Here is mine:
watch: {
web: {
files: ['WebContent/**', '!WebContent/less/**'],
tasks: 'copy:webfilesToOutdir',
interrupt: true
}
}
I hope this helps. I've been using this solution for some time now and it has done its job pretty well. Good luck, gentle men.
Upvotes: 2
Reputation: 196596
WebStorm — another IDE by IntelliJ — has something that looks a lot like what you are describing.
Maybe your IntelliJ IDE has it too.
Upvotes: 0