Play! Java web framework. How does their development server compile automatically? - java

After looking at the Play! framework I find it really productive that the development server that it comes with automatically is able to compile .java files and show the changes, immediately. There's no hot deployer scanner that runs every tot seconds or so. The compilation happens when you hit refresh and it's extremely faster than my incremental mvn package. How do they do this?
I would like to know, well because I'm interested in knowing, but also because I don't want to use the entire Play! framework for my small project. I'm only interested in their development compilation process because I would like to adopt it :).
Any ideas?

I was reading about this just this morning. It actually takes your changed source files and uses the Eclipse Java Compiler (ECJ) internally before spitting out the compiled files to the built-in dev server.
The thing is, you probably don't want to go to the effort of wiring the ECJ into your "small project".
You can definitely approximate it though - the trick is to not do a mvn package, instead you want to be dropping the changed .class files into your webapp's exploded warfile directory on the filesystem.
If you're not tied to a particular app server/container, have a look at the Eclipse Jetty Plugin - looks like it's what you need, and Jetty is quick

JDT -- Play! uses Eclipse JDT to compile and load classes dynamically. Much the same way you code in Eclipse and you see an error or warning messages as soon as you type in something not desirable. See ApplicationCompiler class.
You may also want to look into JDT.

Play uses the Eclipse compiler to compile code at run-time.
Take a look at the following class, that is used by Play to perform the necessary compilation at run time.
https://github.com/playframework/play/blob/master/framework/src/play/classloading/ApplicationCompiler.java

The way they do it is by using a custom classloader that will detect changes to source files, use the Eclipse Java Compiler to compile the files and then hot swap the appropriate classes in the JVM. If you are looking for something similar, checkout ZeroTurnaround's JRebel
it is not free, but well worth the time savings when you need to redeploy a large project.

I'm not a Play developer however Struts2 is also capable of this but though the struts2-spring-plugin. Since the class reloading is provided by Spring it might be possible to use this spring feature by any project.
http://struts.apache.org/2.2.1/docs/spring-plugin.html
Search the page for "Class Reloading".

Related

Test dependencies of Java web app library

I am coding a java web app.
When I started, every time I needed to use an external package, I would download the jars manually and download all dependencies of each jar manually and place them in the libraries folder (in Netbeans).
As time went on, I started using a dependency manager (Ant).
Now, I would like to use my dependency manager for all of my external libraries.
If, after executing this change I run my application and it successfully deploys (no ClassNotFoundExceptions and no NoClassDefFoundErrors), is it safe to assume that I have not missed anything and that my application will run smoothly as far as the external packages go?
Or, do I need to individually test out each functionality in my web app to confirm that the changes I made to the libraries didn't change how the application runs?
It's actually depends on the code inside these libraries. Only part of classes are loaded at startup, thus you can miss something. Also there might be a possibility that you're loading some classes in runtime manually, i.e. Class.forName(String) and this code has not been triggered at startup. Thus, I would say you can't be 100% sure.
Generally in Java here are 3 build approaches:
Imperative - you're saying "How to assembly your code". The typical example of this is Apache Ant.
Declarative - you're saying "Which code you want to assembly". The typical example of this is Apache Maven
Mixed - which takes benefits of previous systems. This is Gradle.
How it helps!

Methods to see result fo a code change faster

This question came to me when developing using Eclipse.
I use JBoss Application Server and use hot code replacement. But this option requires that the 'build automatically' option to be enabled. This makes Eclipse build the workspace automatically (periodically or when a file is saved?) and for a large code base this takes too much time and processing which makes the machine freeze for a while. Also sometimes an error message is shown saying that hot code replacement failed.
The question that I have is: is there a better way to see the result of a code change?
Currently I have the following two suggestions:
Have unit tests - this will allow to run a single test and see the result of a code change. ( But for a JavaEE application that uses EJBs is it easy to setup unit tests?)
Use OSGi - which allows to add jars to the running system without bringing down the JVM.
Any ideas on above suggestions or any other suggestion or a framework that allows to do this is welcome.
Did you take a look at http://zeroturnaround.com/jrebel/?
I tell you how I work. I hope it is useful. First of all, I disable "Build Automatically". Maybe it is better if you do a simple ANT script to compile and see errors/exceptions. Also, I make jar file with the same script. Afterwards, I use OSGi to check the application. Yo do not need to stop server, only you need to change versions in deployed bundles. Intead of JBoss Server I use Equinox which includes Jetty Web Server.
May you have a nice day!
With JRebel, you wouldn't have to build your project (skip the build totally). Plus, only the changed resources will be reloaded so the update happens instantly. Plus, session is preserved so you do not have re-login to the application after the update was performed.
Even though the question I asked was quite specific to Java, I thought that mentioning using an interpreted programming language that avoids the compilation step is anther way of seeing result of a code change faster.

Best ways to manage generated artifacts for web service/xml bindings in a java webapp/client?

I'm working on a couple of web services that use JAXB bindings for the messages (in JAX-WS or spring-ws). When using these bindings there's always some code that is automatically generated from the WSDL to bind the message objects. I'm struggling to figure out the best way I can make this work so that it's easy to work with, hard to break and integrates nicely with IDEs (mostly using eclipse).
I think there are a couple of ways to go about this. The three main options I see right now are:
Generate code, keep the source artifacts and check them into the repository. Pros: integrates easily with IDEs (source highlighting etc), works within the build system. Cons: generated code changes each time you regenerate it, possibly creating noisy commits. It's also redundant since the WSDL file is already checked in, usually.
Generate code as part of the build process. Don't keep source artifacts or only keep them in output directories. Pros: fixes all the cons from the previous one. Cons: harder to integrate with IDE, though maybe this build step can be run automatically? I currently use this on one of my projects but the first time I checkout the project it appears broken, which is a minor nuisance.
Keep generated bindings in separate libraries (jars) included with maven or manually updated jars, depending on your build process. I got the idea from a thread on java.net. This seems more stable and uses explicit versioning but seems a bit heavyweight.
Which one of these options would you implement and how? We're currently using maven and eclipse, so any ideas in that regard would be great. I think this problem generalises to most other build systems and IDE combinations though, even other languages perhaps.
I went for option 3. If you already host your own repository (and optionally CI), it's not that heavyweight. All it takes is a simple POM. It's even possible to include some utility/wrapper/builder classes (that often make life easier with generated classes) and use them in several projects.
I'd go for option 2 and generate code in the "standard" ${project.build.directory}/generated-sources/<toolname> location as part of the build process. Using generated sources is well supported by m2eclipse (use Maven > Update Project Configuration once sources have been generated) and, if I remember well, by the maven eclipse plugin as well (i.e. the folder will be added to the Java Build Path). Actually, I think NetBeans also handle this fine. Not sure for Idea.
For the generation itself, you may need the maven-jaxb2-plugin if I understood correctly.

How to automate a build of a Java class and all the classes it depends on?

I guess this is kind of a follow-on to question 1522329.
That question talked about getting a list of all classes used at runtime via the java -verbose:class option.
What I'm interested in is automating the build of a JAR file which contains my class(es), and all other classes they rely on. Typically, this would be where I am using code from some third party open source product's "client logic" but they haven't provided a clean set of client API objects. Their complete set of code goes server-side, but I only need the necessary client bits.
This would seem a common issue but I haven't seen anything (e.g. in Eclipse) which helps with this. Am I missing something?
Of course I can still do it manually by: biting the bullet and including all the third-party code in a massive JAR (offending my purist sensibilities) / source walkthrough / trial and error / -verbose:class type stuff (but the latter wouldn't work where, say, my code runs as part of a J2EE servlet, and thus I only want to see this for a given Tomcat webapp and, ideally, only for classes related to my classes therein).
I would recommend using a build system such as Ant or Maven. Maven is designed with Java in mind, and is what I use pretty much exclusively. You can even have Maven assemble (using the assembly plugin) all of the dependent classes into one large jar file, so you don't have to worry about dependencies.
http://maven.apache.org/
Edit:
Regarding the servlet, you can also define which dependencies you want packaged up with your jar, and if you are making a stand alone application you can have the jar tool make an executable jar.
note: yes, I am a bit of a Maven advocate, as it has made the project I work on much easier. No I do not work on the project personally. :)
Take a look at ProGuard.
ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or for Java Micro Edition.
What you want is not only to include the classes you rely on but also the classes, the classes you rely on, rely on. And so on, and so forth.
So that's not really a build problem, but more a dependency one. To answer your question, you can either solve this with Maven (apparently) or Ant + Ivy.
I work with Ivy and I sometimes build "ueber-jar" using the zipgroupfileset functionality of the Ant Jar task. Not very elegant would say some, but it's done in 10 seconds :-)

How to compile single/multiple java files without server restart? Is there any Eclipse plugin for the same?

I want to compile multiple java files in my Application without restarting Weblogic and Tomcat. Otherwise, this takes a lot of time. For this I got one Hotswap plugin in Eclipse, but this is not working in all the cases. It says it works for single file compile. Even if I use that it is not working in all the cases.
Is there any other way that I can do this or is there any other plugin/script/software which can help me in this?
If there are some open source ones, it will be very helpful.
Thanks in Advance.
One thing is compiling the classes, but you also need the JAVA VM to reload the classes and use them, which is called hot-swapping. For the best available hot-swapping of classes you'll need something like javarebel. It allows you to hot-reload a lot more types of code-changes than the regular SUN JVM. If you run your deployment in exploded-mode you're home free, and can test any code change in a second or so. We're fairly test-driven, so I only use javarebel in that short phase when I assemble the whole application, but it works really well.
The Java HotSpot VM does this automatically, but not in all cases...
First, you must be running a "debug" session, not a "run" session.
Next, some changes will force a restart. The basic idea is that if the interface to the class change (the method sigs, not an actual Java interface), the VM can't replace the class contents inline.
You shouldn't need a plugin for this, just a recent-ish VM.
This happens under several circumstances like
adding methods
removing methods
changing method signatures
changing the class hierarchy (superclasses, implemented interfaces)
It can also happen if you introduce an error into the class. (For errors like mismatched curly braces)
When you save a Java file, eclipse compiles it. If there is an error, eclipse inserts an exception throw to indicate that there is an unresolved compilation error. (It does this so when you run you don't just see the last successful compilation, making it obvious you have a compiler error)
I don't know Eclipse, but I do use Netbeans. Netbeans does this pretty well. The latest version even has an option to automatically recompile when you save a java file.
I know this doesn't exactly answer your question. You can probably use both Netbeans and Eclipse depending on what part of the project you're working on.
EDIT:
With Tomcat you can reload the web app. This is really only useful if Tomcat is looking at the new class. If your project is compiled to a build directory first and a WAR is then created from this you can go into Tomcat and install the web app and instead of pointing at a WAR point at the build directory.
In Tomcat you may have to put a site config file under tomcat/conf/Catalina/localhost. The contents of this file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:/Projects/MyWebApp/build/web" path="/MyWebApp"/>
Instructions for reloading here:
http://www.cs.ucl.ac.uk/teaching/java/tomcatfaq.html#changeservlet
If you do this a few times though Tomcat will run out of memory. This is because of something called PermGenSpace. Read up about it if you want to know more. The solution is to increase the JVM memory, the PermGenSize (with -XX:MaxPermSize) and finally restarting Tomcat occasionally.
EDIT2:
If reloading the app causes you to be logged out you may be able to easily get the container to serialize your session data to disk by adding 'implements Serializable' to some of your classes. Then you should not need to login after reloading the app.
I agree that it is very tedious to redeploy all the time when developing.
I would suggest you look into MyEclipse which has a very good hotdeploy mechanism which works well with Tomcat, and which is quite affordable and has a 30 day trial.
The stock Java EE mechanism in Eclipse for redeploying to servers is nowhere as fast.
I guess I don't really see where the problem is. Here is what I do and the changes load almost instantaneously. I have an Ant script that compiles the .java and .jsp files for me, puts them in the appropriate directories under webapps and changes the web.xml file if necessary (or at least touches it to notify tomcat of the changes). If you need help on doing any of that with Ant, I'd be happy to help. Btw I do not use WAR files for deployment on my testing machine. That would be a lot slower I guess.
very easily done if you read this page:
See http://blog.redfin.com/devblog/2009/09/how_to_set_up_hot_code_replacement_with_tomcat_and_eclipse.html
Thank you Dan Fabulich

Categories