I use STS(spring tool suite) + maven plugin.
Every time when I run my application using maven-clean
I see following error:
[INFO] Scanni
Summary : Use an external script (batch file) that a) stops the server and b) Unlocks the .jar file before running maven-clean.
Steps:
Bind the maven-antrun-plugin:run goal to the pre-clean phase of mvn clean. See how to do this here
See how to use the antrun plugin to run an external batch file in Windows here : sblundy's answer to "Starting external process". Let's call this file unlock_handles.bat
Use the Sysinternals handle utility download link in the pre_clean.bat to a) stop the Tomcat server and b) unlock the .jar file. handle -c
and handle -p
would be useful here.
Whew! That's a bit of work, but it will reliably automate the process for you so that you do need to do it manually yourself each time!
OK, here's a rough, proof-of-concept version of unlock_handles.bat for you to try out:
REM "Use handle.exe to figure out the process id and handle ids, parse the output, then close the handle (again using handle.exe)"
cd "C:\Users\Nikolay_Tkachev\workspace\HHSystem\UI\target"
"c:/Program Files/Process Explorer/handle.exe" -p java.exe "C:\Users\Nikolay_Tkachev\workspace\HHSystem\UI\target" > handles.txt
@echo "O====== Going to unlock all the below file handles! =======O"
FOR /F "tokens=1-26 delims=: " %a in (handles.txt) DO @echo %h
FOR /F "tokens=1-26 delims=: " %a in (handles.txt) DO handle -p %c -c %f -y
Of course, you have to change the path to Sysinternals' handle.exe before giving this a try.