IntelliJ + Tomcat + Spring-Loaded - spring

I would like to try out Spring Source's "Spring Loaded" class reloading agent, with Tomcat run via IntelliJ.
https://github.com/SpringSource/spring-loaded
I've added the JVM arguments to my Tomcat run configuration, and my webapp starts up without errors and seems to behave normally.
I'm not really sure how to trigger the class reloading though. Do I just need to compile the classes that I modify? I've tried that and that didn't seem to work. Do I need to update Tomcat and deploy classes and resources? That doesn't seem to work either...?
Any specific configuration details would be greatly appreciated.
EDIT: More info, I think my problem may have to do with using two modules, one a core code library, and one the webapp. I use Maven for both, one configured as a jar project and the other as a war project. IntelliJ sets up the Artifact for the exploded war to use the jar module's jar file. I've tried switching the Artifact config to incorporate the jar module's compiled output into WEB-INF/classes instead. Now I see the .class files in target/webapp/WEB-INF/class/etc. But I'm not sure how to get IntelliJ to update a specific class file when I edit it.

If you added the following jvm parameters (as described at the springloaded page) to your tomcat
-javaagent:<pathTo>/springloaded-{VERSION}.jar -noverify SomeJavaClass
you should be fine. It may help to disable auto reloading in tomcat for the webapp you are testing. For exmaple in your server.xml:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Context docBase="projekt" path="/projekt" reloadable="false" ...
This ensures that only the springloaded classloader loads changed classes.

spring loaded cannot hotswap jars.
Put web fragment output into webapp/WEB-INF/classes
Also spring loaded support only spring 4.1
https://github.com/spring-projects/spring-loaded/issues/139

Related

How to bundle war file containing rest code with jax-rs dependencies so that it runs on Tomcat?

I'm using a Gradle based-setup to build a REST web service (with Jax-Rs and glassfish.jersey dependencies). I'm using the Gradle-tomcat plugin (bmuschko-gradle-tomcat-plugin) to deploy it on tomcat during runtime. I'm pretty new to this, and I've read that tomcat doesn't support Jax-Rs features on its own. So how do I make my REST code work on tomcat? Should I bundle Jax-rs/jersey jars along with my war? I won't be able to copy-paste those jars anywhere (few tutorials have the jars pasted under web-inf/libs module), I want it to be automated during the build with Gradle.
PS: I'm using #ApplicationPath (mandated by my company) in my code, which overrides servlet-mapping in the web.xml, so I won't be able to modify that.

JSPs, static content, SpringBoot jars and IntelliJ

So I'm developing in IntelliJ a spring boot app. Using Gradle, I'm creating the sprint boot jar file.
I'm having problems figuring out where to put the jsps and static content such as .js files in such a way that running the jar AND running from within IntelliJ works!
It seems that in order to get SpringBoot to find jsps in a jar file I need to put the jsps inside a src/main/resources/META-INF/resources directory. For example, META-INF/resources/WEB-INF/index.jsp. I'm pretty sure the WEB-INF is now meaningless.
However, if I try to run this spring boot app from within IntelliJ, it cannot find the jsp. 404, blah blah blah. I actually have to put the jsps in the war-style webapp directory in src/main. However that directory is totally ignored during the spring boot jar build.
So.. how do developers set up their development environments that is both IntelliJ and, say, gradle bootRun friendly?
There is a guide here which should work both in IntelliJ IDEA and in the command line via the war file which can be executed as a jar (via java -jar).
As per spring boot there are Limitations when it comes to jsp's.
To overcome these limitations we need to have the configuration made in the application to render jsp by placing the jsp's under src/main/resources/META-INF/resources/WEB-INF/jsp folder.
Sample Code: Click Here
References:
https://dzone.com/articles/spring-boot-with-jsps-in-executable-jars-1
https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample

Thymeleaf cache set to false not working

I have a spring application under development.
The configuration of the project is as follows:
spring-dev-tools dependency is one of the maven dependencies
thymeleaf dependency is added through spring-boot-starter, that is org.springframework.boot:spring-boot-starter-thymeleaf
I have explicitly disabled template cache with spring.thymeleaf.cache=false
The src/main/resources folder is marked as resource in my IntelliJ IDEA project.
I am using IntelliJ IDEA project to run the application.
When I run the application, I can see in the console output that: LiveReload server is running on port 35729
Although, it seems like all my configuration is correct, the running application is not loading any changes I made to the templates while it is running.
If you are using Spring Tool Suite 4, you need to implement the following in your application.properties to disable the cache from Thymeleaf:
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=file:src/main/resources/templates/
Adding the spring.thymeleaf.prefix value worked for me.
For more information about the solution:
https://github.com/spring-projects/spring-boot/issues/34#issuecomment-316295791
The LiveReload server loads any changes only when one of the files on the classpath is modified Spring Doc.
Though I have resources, the folder containing templates folder, on the classpath, the changes to the html files in templates folder are not reflected in the running application.
What worked for me is to also add src/main/resources/templates to the classpath i.e. marked as resource in IntelliJ
Thanks

Deploying a 'JAR' file instead of 'WAR' file for Spring-MVC and Maven

I am working on a Spring-MVC application which uses Maven. In the POM.xml I noticed that I can denote the file-type in which I can select if I want to deploy the project as a JAR or WAR.
Mostly I select a WAR file and then deploy it in Apache tomcat. My question is, If the application is Spring-MVC based, with Spring-Security, Hibernate and other libraries, can I package it as JAR by simply denoting it in POM.xml and deploy it in Apache webserver instead of using Apache tomcat? Or do I need to make some modifications somewhere for this to work. Kindly let me know. Thank you.
No, you can't. Apache httpd knows nothing about how to handle jar files.
What you can do though is to provided an embedded webserver (such as jetty) in your package and define in your MANIFEST.MF file a main class that will launch it and register your application to it.
That way you can package is a an auto-executable jar, or as a war that can be run on his own or deployed in a classical webserver.

Webapp running in Jetty but not in Tomcat

I made a Spring MVC webapp in which I used JSTL tags. I launched it by Gradle and it worked perfectly fine:
gradle jettyRunWar
However, when I used the same WAR file with my Tomcat, I got error:
The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application.
Using stackoverflow, I was able to fix. Problem was in my build.gradle file where I didn't specify the JSTL dependencies. I fixed that and I was able to run my webapp in Tomcat as well.
My question is why Jetty was able to run this app when Tomcat wasn't. How could I have avoided this issue?
Using JSTL Taglibs
The JavaServer Pages Standlard Tag Library (JSTL) is part of the Jetty distribution and is automatically put on the classpath when you select your flavour of JSP. It is also automatically on the classpath for the jetty maven plugin, which uses the Apache JSP engine as of jetty-9.2.
Embedding
If you are using jetty in an embedded scenario, and you need to use JSTL, then you must ensure that the JSTL jars are included on the container's classpath - that is the classpath that is the parent of the webapp's classpath. This is a restriction that arises from the Java EE specification.
The jars that you will include will depend on the flavour of JSP that you are using.
Tomcat has never included JSTL.
You should put the jstl and standard jars in WEB-INF/lib

Resources