JSP debugging in IntelliJ with spring-boot - java

I want to be able to debug jsp templates in IntelliJ.
I'm using Spring-Boot
Project is without web.xml file. Made with Annotations.
I'm launching app via Spring Boot run Configuration in IntelliJ
When I launch the app, breakpoints become gray crossed circles, indicating that no class associated with it.
Jsp location is /webapp/WEB-INF/jsp
Now, before I get marked for duplicate:
This question is quite old and only suggest using web.xml + Tomcat and plugin. Pluggin doesn't seem to work and switching to web.xml is going to be a real pain in the bottom. Would be nice to stay with annotations and not include web.xml
Tried launching via maven command as suggested in this answer, but for unknown reason, after launching, the process detaches from intellij and I'm not able to launch it again (have to destroy process in the process task first) and it doesn't resolve the issue either.
Other answers, suggesting launching Tomcat directly fail as it won't launch without web.xml.
How can I keep an ease and speed of launching Spring-Boot application, have an ability to hot-swap the code, debug java and jsp templates while avoiding web.xml configuration all at the same time?

Related

Spring Boot with AspectJ and Aspectweaver not persisting singleton when started with IntelliJ

So, I have a really weird problem which did cost me hours to find.
I have a simple Spring Boot application which I start with a normal Spring Boot run configuration in IntelliJ.
I've added a special Aspect configuration (see https://callistaenterprise.se/blogg/teknik/2020/09/20/multi-tenancy-with-spring-boot-part2/) which requires two Java Agents (spring-instrument, aspectweaver).
I've added them separately to the run configuration "VM options" as following:
-javaagent:target/dependency/spring-instrument-5.3.8.jar -javaagent:target/dependency/aspectjweaver-1.9.6.jar
They successfully get loaded on start.
Wherever the Aspect function is called, a singleton that has been already used/instantiated before, will magically get reinstantiated and is then of course empty.
This does not happen / works as expected, when I use mvn spring-boot:run (or just java -jar -javaagent:.... app.jar) to run the application. In this case, the maven configuration in pom.xml takes care to add the needed agents.
What I think is that somehow IntelliJ adds special stuff to the run command or something else interferes with the application, so that the Aspect function is not called in the same context/instance.
It should not be related to threads, since it's prepared for it.
Does anybody have an idea, what's happening here? I'd like to keep the Spring Boot run configuration and thus the Debug option, since debugging does not seem to work out of the box with mvn spring-bootn:run.

Is a HotSwap possible with Spring MVC and Spring Security?

Currently I'm developing a Web App with Spring MVC and Spring Security using Maven to build the application and Tomcat as a webserver.
When I'm making changes in my JSP files, I can immediately see the changes (when the application is running) when I do a reload in my webbrowser.
However, when I make changes in my Java files (for instance in a Controller class), I have to redeploy the application (which takes 10 to 15 seconds) to get the changes working.
So my question is: Is there a way how to NOT do a redeploy every time when I perform changes in my Java classes?
Thank you very much!
We call what you would like to achive is Hot Deploy.
There are many different way to apply Hot Deploy, so the best way is to google for it.
If you use IntelliJ + Tomcat then this article can help you:
Intellij IDEA – Auto reload a web application (hot deploy)
If you use Eclipse IDE this can help: How to configure hot deploy in
Eclipse
You can find some info here as well: how to enable hot deploy in
tomcat
Thanks for your responses. I tried some more options and this is working fine for me:
Go to Run / Edit Configurations / “Deployment” tab, clicks + icon / select an “exploded artifact”
Select “Server” tab, update the following options :
On ‘Update’ action -> Update classes and resources
On frame deactivation -> Update classes and resources
Run web application in Debug mode. Try to modify some codes or resources (Java files, ...), the modified classes and resources will be reloaded automatically without any redeploy or something like that.
You can do a hot deploy using this plugin JRebel but if your build is only 10-15 seconds I think that a hot deploy is overkilling

Fastest way to toggle <secure> true or false inside web.xml

I'm developing a java base app which has to be deployed in PROD to Google App Engine. I need the set the <secure>true</secure> flag inside web.xml when comes time to deploy to PROD. However I'm testing with my appengine:devserver on my workstation and I'm using http (not https) so I have to set it to <secure>false</secure> or comment it out.
Question: what's the easiest way to automate this? I know it's possible with maven profiles but is there something Google plugin for Eclipse made or another way I'm missing? My project is Maven based and I'm starting the devserver either from command line or within the IDE...
Have you tried SessionCookieConfig and setting it programmatically? (e.g.: depending on environment variables)
I discovered that if I start only the webapp using appengine:devserver we don't need to care about true. So this is not a problem if I simply change the way I start the webapp. The problem occurs only if you start the webapp with Tomcat, jBoss or your own jetty server. However I consider a good practice to be able to run the application from something else than GAE just to be sure we aren't binded to a specific engine (server agnostic pattern)...

Web application deployment under embedded Tomcat 7.0.32

We have been using tomcat 7.0.19 successfully in embedded mode. However recently due to some fixes in our area of concern we decided to move to tomcat 7.0.32. Most things work as expected with same code and newer version, however the war deployment for some reason has'nt worked well. I have a couple of servlets registered with my tomcat. Facing below 2 issues,
Has something changed from 7.0.19 to 7.0.32 from embedded tomcat behavior. To detail this out let me explain the behavior difference, with 7.0.19, i could deploy my application and when i hit the "host:port/contextpath" it loaded the applications start page (i.e. welcome page, this page is UI centric and does not need a server intervention, so none of my servlets get called). However with 7.0.32 the same url results in my servlet being called.
So to debug the problem, i commented most of my code so that i have a vanilla tomcat implementation, just the very basic stuff, i.e. setting the engine name, default host, setting host properties, adding a connector (nio, with default properties) and deploying a war. No servlets and other things, just to check if the very basic stuff works. To my surprise when i ran this code it still failed with the same problem within my servlet, how did that happen, now that my code is commented it does not register any servlets, still where does it find it from? Does embedded tomcat store some old references, which are not getting cleaned on subsequent runs? I tried changing the port, but that too didn't help.
I am hitting the wall here, not able to understand this wierd behavior, if i figure out #2, only then can i make some progress on #1.
Thanks in advance,
Vikram
Figured out what the problems were.
In reverse order,
2 - This actually was a weird behavior with the vanilla embedded tomcat code too invoking the servlets which never were registered in the first place. The problem here was with eclipse, for some reason it picked up the old reference of my class. The moment i ran the same code from outside of eclipse i.e. via command prompt, things were back to normal.
1 - This problem was related to web deployment, in my code i was additionally setting my classloader into WebappLoader and eventually adding my application jars into it. This for whatever reasons worked fine with 7.0.19, however did not with 7.0.32, the moment i externalized all my jars to be loaded during application startup via classpath this problem too was resolved.
Thanks,
Vicky

How to properly manage Tomcat web apps inside Eclipse?

I used to run Tomcat separately on my machine. I had an Ant script that would rebuild my project, deploy it locally, and restart Tomcat. That all worked ok, but I wasn't able to debug the web app inside Eclipse.
So I learned how to setup Tomcat inside Eclipse and got my web app running. Now the problem is that I don't understand fully how to manage it this way. Eclipse is set to automatically build my project on changes, but those changes don't seem to always be reflected in the web app. Sometimes I have to manually build the project and manually "clean" the server for the changes to be reflected.
Are there rules somewhere about how to manage this setup? For instance, if I only change a JSP then will it automatically be synchronized? If I change a servlet class, then I need to manually rebuild the project? Are these rules consistent, or should I just manually rebuild and clean every time?
I would really appreciate it if someone could give me the best practice rules or point me to a good resource to learn how to manage this environment.
PS. I am using Eclipse 3.4.1 Java EE package and Tomcat v5.5
You can use Eclipse and Tomcat in the way you mention. First the basics of how to set it up:
In the Servers view setup a new Tomcat server pointing to your TOMCAT_HOME
Make sure your project is an Eclipse "web project". You may need to create a dummy one and copy over some of the files in .settings (look at the wst files).
Deploy your project to Tomcat by right clicking on the server in the Servers view and "Add and Remove Projects..." to add your project to the server.
You can run your server and test it out just like you were running Tomcat outside of Eclipse. If you run the server in Debug mode you can set breakpoints and step through the code.
As for when you will need to restart the server Eclipse is usually pretty good about auto-deploying the changes. You will pretty much never need to restart for changes to jsp pages. If you change a class it will auto-deploy the change (usually) if you change the body of a method. If you change the signature of a class (add or remove a method or change args for it) you will almost always need to restart. Any changes to configuration files (web.xml or similar) will also almost always require a restart.
To restart just click on the "Debug" or "Run" button in the Server view. All your changes will be redeployed into Tomcat.
One thing to watch out for is that in the default configuration your "webapp" directory in TOMCAT_HOME will not be used. Instead it will use a folder under your Eclipse workspace directory (WORKSPACE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0).
Normally you should republish the application to get latest changes, don't forget to synchronize with file system first in case your files modified extrernally.
You can specify that your application is automatically reloaded when modified, look for Auto-reload attribute in server.xml of your server configuration project. When set to true, tomcat will automatically reload your application. It's not always a good idea by the way.
Modified JSP's should work automatically, no need to restart the applciation.
If you change the structure of a class that has already been loaded and used (add/remove members, change method signature etc.) your code changes will not be reflected. This is not an eclipse issue but a JVM issue. If you make simple code changes, like logic changes inside an existing method, your changes will take effect after the class is compiled and re-deployed.
Regardless of that, if you change a public constant, you have to rebuild your project(s).
I found two things important to understand:
Eclipse does not automatically realize if files were changed outside of Eclipse. clicking Refresh on a project does that, so does F5. You can also change a setting to refresh automatically, which, however, does not detect changes instantly (my gut feeling says up to 10 secs delay)
Working with servers has the concept of "Publishing" files to Tomcat. This normally happens automatically within a second after any change. Changing many classes can cause many server reloads, which can be a drag if a context reload takes some time (as complex Spring apps certainly do). So I changed a setting to not publish automatically (double-click Server instance)

Categories

Resources