How to auto reload app engine dev server? - java

Im following the tutorial on the App Engine website for 'Google Cloud Endpoints' in Java. Everything works fine and I can run the development server using mvn appengine:devserver. The problem is that when I make any changes to a file (for example, any java file) the dev server doesnt automatically recompile. I need to ctrl-c to kill the dev server and restart it for every code change I make.
Is there a way to have maven automatically detect changes to any files in my project and have it automatically rebuild and restart the dev server?

Unfortunately no. If you want this behavior on your dev server, you need to use Python.
I run in the same issue and there is no real workaround provided by the App Engine to help you doing this.
From the "Using The Google plugin for Eclipse":
With Eclipse, you can leave the server running in the debugger while you make changes to source code, JSPs, static files and appengine-web.xml. When you save changes to source code, Eclipse compiles the class automatically, then attempts to insert it into the running web server dynamically. In most cases, you can simply reload the page in your browser to test the new version of the code. Changes to JSPs, static files and appengine-web.xml are recognized by the development server automatically, and also take effect without restarting the server. If you change web.xml or other configuration files, you must stop and start the server for the changes to take effect.
(https://developers.google.com/appengine/docs/java/tools/eclipse#Running_the_Project)
There is NOTHING comparable in Java (link from "The Java Development Server") (https://developers.google.com/appengine/docs/java/tools/devserver)

There's currently nothing in the App Engine SDK to automatically restart when files change, but that's not to say you can't do it. I ran into the same problem and wrote up a script to listen for file changes as triggers to restart App Engine. It's in JavaScript, so you'll need to install Node.js if you haven't already.
// Install watch-exec
$ npm install -g watch-exec
// Watch the current directory
$ watch-exec --command "mvn appengine:devserver" --watch .
This will immediately start App Engine, and then restart it any time a file changes. If the app crashes for some reason, the script will wait for your next edit before trying to restart.
P.S. That entire script is about 40 lines of code, and you could probably do the same thing in other scripting languages. If you haven't tried writing your own automation before, I'd definitely recommend checking out the source code to see how this works.

I've found using Gradle, GAE, and Spring MVC, the assemble command will put the correct artifacts in place, and the server will re-init the app. It's a little quicker than a server restart.

Using App Engine standard with the cloud.tools appengine-maven-plugin hot swap works fine (most of the time, can be problems when setting up the workspace).
For a multi-module maven project: no need to stop the server or browser,
just push the code changes (maven command package -pl *-server)
& refresh the browser.
Debugging with a debug client currently works perfectly for changing / adding code within methods.

Related

How to hot reload Tomcat server in VSCode

I'm migrating from Eclipse IDE (+ VSCode for coding Java servlets and HTML/CSS/JS webpages, respectively) to only Visual Studio Code for its lightweight.
I have several Java extensions installed for VSCode:
Language Support for Java(TM) by Red Hat
Tomcat for Java
Debugger for Java
Eclipse has a series of settings for hot reloading:
- Automatically publish when resources change
- Refresh using native hooks or polling
while VSCode doesn't seem to have any for me.
A few things I've try to reload my Java and web code:
Restart Tomcat server
Delete and re-adding Tomcat server
Delete and regenerate .war package (not sure if this does anything, it can run well without a .war package)
Good news...
It works automatically now. With Tomcat for Java Extension (And the rest of the Java Extension Pack):
https://marketplace.visualstudio.com/items?itemName=adashen.vscode-tomcat
Just make sure your settings are like this (search for "java.autobuild" to get those two in the first results):
Very important is, that you wont see any logs in the console on the HCR (Hot Code Replacement) like it happens in Eclipse ... but you in fact will see the replaced code behavior. Just debug over the piece of code you changed, and you will see it in fact changed in the running server.
UPDATE: I've found it works better with the 'manual'(default) setting. Just clicking once in the lightning icon. (Testing in Ubuntu 18.04 LTS)
[I say it works better because if I added lines to a Class' code with the 'auto' setting it will not add that code ... only if I change code in the existing lines. But with the manual setting I just save the file, and then click the lightning icon wait ~3 seconds myself and debug over the new code ... and everything goes well!!
... This solves my coding needs(lightweight Editor/IDE with Hot-Code-Replacement in Tomcat)]
Enjoy !
This article may be helpful, I migrate from other IDE to Vscode.
According to the article, if you install Debugger for Java, it enabled Hot code replacement(HCR) and then :
You can start a debugging session and change a Java file in your development environment, and the debugger will replace the code in the JVM running your code.
Hot code replacement for java comes to visual studio code
Install “Tomcat for java” extension in VScode.
Configure the Path of Tomcat in the above extension.
Now you should be able to run tomcat in usual build -> deploy mode. Now install DCEVM, By using “java -jar installer-light.jar”. (Select “Install DCEVM as altjvm”)
Place HotSwap Agent Jar in directory of your choice.
Now In VScode right-click the Tomcat server you have created. (The one from Step 3 ) And select “Customise JVM Options”.
In the file opened, Place “-XXaltjvm=dcevm
-javaagent:/[your_directory]/hotswap-agent-1.3.1-SNAPSHOT.jar” Now Right-click the tomcat server in VScode and select “Debug WAR package”, And select the WAR file.
If everything goes well, The WAR will be start in TOMCAT in debug mode with text “HOTSWAP AGENT” in the log.
Now your every save to your file will trigger Hot Reloading.
https://medium.com/#manoj_makkuboy/hot-reload-java-8-tomcat-server-in-vscode-ba6233d632e?

When adding "<add-linker name="xsiframe"/> to GWT 2.6 project, I get errors

I am part of a team that manages a GWT application. We use GWT 2.6.
I have been able to successfully debug this project in my browser in the past. A couple of days ago, debugging stopped working altogether and the web app no longer loads in my browser. When trying to open it in a browser, I get the following message:
Javascript error message
If I remove the following line from my .gwt.xml file, the web app works as expected but I lose the ability to debug in my IDE and in my browser:
<add-linker name="xsiframe" />
Here's what I've done so far:
Invalidate Cache / Restart (IDE)
mvn -U clean install
git clean (removed all unversioned files and directories)
Cleared my browser cache, cookies, history, etc...
Restarted IDE, browser, computer
Tried in different browsers on different machines
Even after all of that, I still get the same error message.
Any suggestions would be greatly appreciated.
*UPDATE:
What's interesting is if I remove that line from my .gwt.xml file, perform a mvn -U clean install, clear my browser cache, and run my application - everything works. If I then add the line back to my gwt.xml file, do a mvn -U clean install, and DON'T clear my browser cache, then the debugging works as well.
Is this because my browser is using a cached version of my GWT generated Javascript?
I almost never clear my browser cache on a regular basis, it's possible that debugging has only ever worked in the past because it was using a cached version of the GWT Javascript files. The problem occurred when I cleared my browser cache in an attempt to solve a different issue that I was having.
That error message is part of the com.google.gwt.i18n.client.Dictionary class. Either there is now a timing error (gwt app loads too early and the dictionary isn't ready yet), or something else has also changed, and not just the linker.
To be more specific, your question would need more detail.

How to display changes in web browser when Building a RESTful Web Service using Spring?

I am following this tutorial:
https://spring.io/guides/gs/rest-service/
After completing the tutorial I run everything in my web browser on my local host and everything seems to work.However when I make changes in the java code (I made errors on purpose) I can still run everything in the web browser. What do I need to do in order for it to change due to my code changes? I guess I should mention that I am using eclipse and have installed maven as a plugin.
You are changing the source code, but not the compiled files. Your service is using the created "gs-rest-service-0.1.0.jar" file. This is not affected by your code changes.
Jump into the tutorial again where it says:
"Build an executable JAR"
This will rebuild your jar file. Afterwards you will have to restart your service like you did in the chapter "Run the service".
Instead of using maven as a plugin a choose to install it in order to use the command prompt.

How to run GWT in production mode

I am trying to run GWT project in production mode, as per the instructions at https://developers.google.com/web-toolkit/usingeclipse. So my project foo is located as a folder foo in my desktop under th workspace folder. When I right click on the foo project in the Project Explorer, click Google - GWT Compile, I see some message like permutations 1, 2.. etc in the debugging console, after which it says that compilation is complete.
However, contrary to what the Google page says, no additional HTML/Javascript files are generated in the war folder. Also, if I enter foo/war/foo.html in the URL bar, I am getting a timed-out error (even though the page contains only a simple alert call).
Can anyone tell me what is going on here?
If you are running in GWT development mode after the compile
remove the parameter gwt.codesvr=127.0.0.1:9997 in the url given by eclipse
After you compile the code the whole gwt code will convert in to javascript so you can access like an normal HTML page with proper paths .
If your GWT project is a Maven project, and you are using gwt-maven-plugin, you can run mvn jetty:run-war (for example, using that as the goal of an Eclipse Maven Run Configuration): That will GWT compile your project into a war, and run it on localhost, thus running your Prod mode.
This can test ui-only features in Production Mode. That's useful if that's all you need to do, especially if otherwise the running of a server for Production Mode occupies limited resources of your development machine that you'd like to save (whereas occupying them with a running server would be a "waste" if your testing doesn't need to make requests to that server anyways).
If your GWT project...
doesn't make any server requests for data yet (maybe your project is still in its infancy as well), and...
is a Maven project
... you can...
GWT compile your project
Maven package (those directions use gwt-Eclipse-maven-plugin) your project
open the packaged GWT host page (i.e: index.html, or < Your Module >.html, under yourProject/target/yourProject-< snapshot version >/) in a browser. Profit!
This is sufficient to test the project's ui-only features, thus in Production Mode. In fact, if your project has features that make server requests for data, but you aren't testing any of them, then this will still work to test just those ui-only features. Basically, any features requesting server data will not have those requests returned (as there is no server running to do that), but all other features will function, as they are ui-only.
If you need to test features that make server requests for data, then you'll have to run Production Mode via a server (that would respond to the requests). For example with Jetty server, see my other Answer here.

How can I deploy web app on Tomcat from running app

I would like to develop updater app. Lets say I've got:
- Running Tomcat
- Running Updater App on the Tomcat
- Running old version of the app on the same Tomcat
I would like to
- Stop old app
- Undeploy old app
- Deploy new version of app (lets assume that I've got new app war file somewere in Updater app resourcess.
Is it possible to do such remote deployment on Tomcat from another app at all?
To deploy a project to your Tomcat Server from another app you need to:
Add a library into your project “catalina-ant.jar” from the folder: {TOMCAT_PATH}\lib
And in your project to deploy a war file you need to only do:
org.apache.catalina.ant.DeployTask task = new org.apache.catalina.ant.DeployTask();
task.setUrl("http://localhost:8084/manager");
task.setUsername("managerLogin");
task.setPassword("managerPassword");
task.setPath("/UrlToYourDeploadingProject");
task.setWar(new File("c:/Project.war").getAbsolutePath());
task.execute();
Thanks to this solution I'm able to develop project that can update automatically my apps.
Yes. There's a set of tools (well, a .jar) released with Tomcat that's usually used by ant but that can be used by anyone willing to call the code. There are tasks for starting and stopping applications, deploying, undeploying, listing applications and so forth; essentially it's an API that wraps the functionality of the manager servlet.
One moment while I try to dig up the name or a link for you...
Here's Apache's information on this stuff: http://tomcat.apache.org/tomcat-5.5-doc/manager-howto.html#Executing Manager Commands With Ant
... and here's a user, elsewhere, describing in detail how he uses these tasks: http://raibledesigns.com/wiki/Wiki.jsp?page=TomcatAntTasks
You may object that you're not ant. That's true, but there's nothing magic about ant, it's just calling code inside the jar. That code can be called from anywhere else that follows the API.
Here's a tutorial on writing ant tasks: http://ant.apache.org/manual/tutorial-writing-tasks.html
Not quite the same, but it should give you an idea of how the API works.
I see no reason why you shouldn't be able to call this API from within a servlet if you want to.
Alternatively, you could run the manager servlet in your Tomcat and simply access the manager application like a Web service: Use your browser or WireShark to snoop on the traffic between your browser and Tomcat when you deploy an app, and then duplicate that to remote control manager. Me, I think using the tools jar is simpler.
If you use a "project lifecycle" tool, this undeploy/deploy can be automatized in the development cycle.
Saying that, I think about Maven2 and his "maven-deploy" plugin :
mvn compile : java compilation
mvn package : creation of the war file (for a webbapp project)
mvn deploy : stop, undeploy the old application, deploy and start the new version !
After, you can couple this automatization with a continous integration server (like Hudson).

Categories

Resources