JavaEE 6 Web Profile and the Instant Developer Experience
When starting a new project, its common to create an "Instant Developer Experience": a new developer can get started by issuing two commands: get the code, and build&run the application. The only things needed are Java, Maven and an IDE.
Doing this for a stack with Spring, Hibernate/JPA, HsqlDB and Jetty has become common knowledge. But using JavaEE 6, it has been somewhat problematic. The only sufficiently well working Maven plugin was by Oracle (an embedded GlassFish). But to configure an entire domain with HTTP listeners, EJB settings, etc. just to get a DataSource is of course plain silly at best &mdash it’s actually a pain from a systems point of view.
Until now. Read this short blog post to learn how an Instant Developer Experience is also possible with JavaEE 6.
In Code Review – Niet alleen de Code!, Erik Hooijmeijer mentions the Instant Developer Experience as one of the things he looks for when reviewing code. Simply put, you have an Instant Developer Experience when all you need to get started on a project is to checkout the code, and issue a single command like e.g.
mvn jetty:run. Such a setup is commonly achieved using Spring, Hibernate/JPA, HsqlDB and Jetty. This is one reason why Jetty is such a popular servlet container: it can be started as embedded container in a Maven build.
Wouldn’t it be nice if you could also create an Instant Developer Experience, and also work with the now standardized dependency injection (CDI), lightweight EJB’s (EJB 3.1, really!)? In other words, using JavaEE 6?
But when you’re developing an application for JavaEE 6, whether you’re using the full API or the Web Profile, setting up an Instant Developer Experience is a lot trickier. It’s not that there are no options available for using an embedded container. In fact, there are several:
- Tomcat 7 + OpenEJB (Apache plugin)
- Tomcat 7 + OpenEJB (GoogleCode plugin)
- Embedded GlassFish (Oracle plugin)
Sadly, using either Tomcat plugin, it’s not possible to get OpenEJB working correctly. Either OpenEJB fails to start, or it doesn’t seem top run correctly. Also trying other plugins didn’t work: the plugins for Tomcat 6 cannot work, as the API is different.
The embedded GlassFish plugin works better. In fact, it just works. Until you try to add a datasource that is. It’s not possible to just add a datasource, without also completely (re)configuring the entire domain, including HTTP(s) listeners, SSL, IIO, EJB, security, etc. Although you can simply copy an existing
domain.xml, this is not the point: you’re still (re)configuring the entire domain.
While researching the three options above, all the time spent trying to get is to work reliably did earn me the knowledge that actually using the embedded API from GlassFish is quite easy. So after the umpteenth attempt at getting the GlassFish plugin working, I simply decided to write my own. I was in for a pleasant surprise: writing a Maven plugin is actually quite easy.
Now, a few weeks later, the end result of this effort is a Maven plugin that runs your web application in a JavaEE Web Profile container. You can use it thus:
<!-- To define the plugin version in your parent POM -->
<!-- To use the plugin goals in your POM or parent POM -->
More information can be found on the plugin homepage: http://opk.sf.net/embedded-glassfish-web-plugin/
Some things you can do with it
When developing a complete application, it’s not uncommon to combine the following:
- your web application
- a database (a datasource, available via JNDI)
- container managed authentication
- a custom logging configuration
- an automated run to perform integration tests
The embedded GlassFish plugin allows all of these. In addition, it also allows the following:
- starting extra web applications – simply define them as dependencies with type
ear(the latter if you're using the full JavaEE API)
- running with the test classpath (instead of the runtime classpath) – useful for example, when defining a development only web fragment to insert test data
- running the full JavaEE API instead of the Web Profile
All these features combined allow you to professionally develop almost any web application, with a database, a mail server, declarative & programmatic security, customized logging, and inside its ecosystem of dependent web applications.
One caveat: EclipseLink
When you’re setting up an Instant Developer Experience using the plugin, you’ll notice that your in-memory database remains empty. This is disappointing, but correct. The reason is that GlassFish ships EclipseLink as JPA provider instead of Hibernate. Sourcing a file named
import.sql is a Hibernate-specific feature. JPA does not provide for it.
But with the bad, there is also some good: in JavaEE 6, there is a new feature called web fragments. Using this feature, you can add a library to the classpath that defines and configures a
javax.servlet.ServletListener for your project, that inserts test data upon application start. In fact, such a web fragment exists (as of 6 februari). Simply add the following test dependency, and configure the Embedded GlassFish Web Plugin to use the test classpath:
An Instant Developer Experience is perfectly possible with JavaEE 6. And given its advantages, there’s no longer a reason not to have an Instant Developer Experience when working with JavaEE 6.
Oscar Westra van Holthe - Kind – has written 12 posts on this site.
The blood of the mathematician runs thick through Oscar's veins. There are few algorithms that do not reveal themselves under his scrutiny. A shrewd observer of a problem domain with a quick mind for understanding its intricacies, Oscar is a favorite for discussing architecture issues and finding solutions for problems found.
Oscar understands the need for Don't Repeat Yourself and goes a long way towards implementing the best solution for the customer. He is not one for cutting corners, preferring the long-term solution over the quick fix.
He has a mindset for the quality approach, aspiring for high test coverage, architecture compliance and low complexity. Oscar helps his team to live up to these high standards, by coaching and mentoring.