GlassFish 4 beta and Maven Embedded Plugin

Everyone is looking for an easy way to try out Java EE 7, even before the launch. Specially now where some JSRs just got Final, like JMS, Batch, JSON, and others. Arun already gave a great tip for anyone that want to try a Java EE 7 project with Maven, but the archetype offered by the Mojo project at Codehaus is missing one important thing: the Maven Plugin for GlassFish Embedded.


This plugin is great because developers don't even need to download, install and configure GlassFish locally. All they need to do is to have JDK and Maven installed. Then they can choose whatever IDE works best (I'm working right now with NetBeans, but I'm also a Sublime Text 2 big fan). It's a simple 3-step command:

  1. $ git clone http://myrepo/myproject
  2. $ cd myproject
  3. $ mvn embedded-glassfish:run
    ... then you wait until the Internet is being downloaded into your local repository ...

Done! Simple as that, a project that does not depend on any installation besides Maven and JDK (ok... Git too, in this case). Now how does the pom.xml must be set up with the Embedded plugin? Follow these steps:

  1. Create a project based on Codehaus archetype, like Arun mentioned in his blog. I'm copying the command line here for the sake of browser history:
    mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=webapp-javaee7 -DarchetypeVersion=0.3-SNAPSHOT -DarchetypeRepository= -DgroupId=org.glassfish -DartifactId=javaee7-sample -Dversion=1.0-SNAPSHOT -Dpackage=org.glassfish.javaee7-sample -Darchetype.interactive=false --batch-mode --update-snapshots archetype:generate 
  2. Now open pom.xml, and go to the end of the <plugins> section
  3. Add the following plugin to the <build><plugins> section
  4. UPDATE 04-10-2013: Don't forget to add the following pluginRepository configuration inside your POM
                <name> Repository for Maven</name>
               <name> Repository for Maven</name>
  5. Execute the following Maven command and then point your browser to http://localhost:8282/javaee7-sample
    $ mvn package embedded-glassfish:run

The reason you must modify the dependencies of the plugin is that there's no released yet of the Embedded Plugin pointing to the GlassFish 4 promoted builds, although some artifacts are already there (i.e. simple-glassfish-api for b79; version is different because until now, there's no release of b83). We hope that the plugin will be updated together with the official release, as soon as possible.

By the way, with this configuration of the Embedded Plugin, you can edit any JavaServer Faces page, and just hit refresh in the browser. The configuration is poiting to the exploded WAR in Maven's target directory. Great setup for designers. 

Happy Java EE 7 coding until the launch!




Is it now possible to setup datasources with GlassFish embedded for testing purposes?

Posted by Alexis MP on April 10, 2013 at 01:28 AM PDT #

Why do you use different version for simple-glassfish-api and glassfish-embedded-all ?

Posted by guest on April 10, 2013 at 11:18 AM PDT #

Because there's not yet a release of simple-glassfish-api for b83. But as the changes are minimal, if they exist at all, this release works fine.

Posted by Bruno Borges on April 10, 2013 at 11:21 AM PDT #

Output of mvn archetype command (yes I am using a local nexus):

ian@sumo Wed Apr 17 10:08 AM ~/projects/tmp
$ mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=webapp-javaee7 -DarchetypeVersion=0.3-SNAPSHOT -DarchetypeRepository= -DgroupId=org.glassfish -DartifactId=javaee7-sample -Dversion=1.0-SNAPSHOT -Dpackage=org.glassfish.javaee7-sample -Darchetype.interactive=false --batch-mode --update-snapshots archetype:generate
[INFO] Scanning for projects...
Downloading: http://sumo:8080/nexus/content/groups/public/org/apache/maven/plugins/maven-metadata.xml
Downloading: http://sumo:8080/nexus/content/groups/public/org/codehaus/mojo/maven-metadata.xml
Downloaded: http://sumo:8080/nexus/content/groups/public/org/codehaus/mojo/maven-metadata.xml (22 KB at 38.7 KB/sec)
Downloaded: http://sumo:8080/nexus/content/groups/public/org/apache/maven/plugins/maven-metadata.xml (11 KB at 18.7 KB/sec)
Downloading: http://sumo:8080/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
Downloaded: http://sumo:8080/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (701 B at 1.6 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] Archetype defined by properties
Downloading: http://sumo:8080/nexus/content/groups/public/org/codehaus/mojo/archetypes/webapp-javaee7/0.3-SNAPSHOT/maven-metadata.xml
Downloading: http://sumo:8080/nexus/content/groups/public/org/codehaus/mojo/archetypes/webapp-javaee7/0.3-SNAPSHOT/maven-metadata.xml
Downloading: http://sumo:8080/nexus/content/groups/public/org/codehaus/mojo/archetypes/webapp-javaee7/0.3-SNAPSHOT/webapp-javaee7-0.3-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.267s
[INFO] Finished at: Wed Apr 17 10:08:47 BST 2013
[INFO] Final Memory: 9M/61M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate (default-cli) on project standalone-pom: The desired archetype does not exist (org.codehaus.mojo.archetypes:webapp-javaee7:0.3-SNAPSHOT) -> [Help 1]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1]

Posted by guest on April 17, 2013 at 02:16 AM PDT #

And with version even Spring works with embedded Glassfish. Thanks for the sample. Much appreciated.

Posted by Guest on June 06, 2013 at 09:59 AM PDT #

Thanks for the nice post. I got it working with the 4.0 version of maven-embedded-glassfish-plugin. The only problem I have is I get OutOfMemoryError when I add Oracle DB jdbc driver(ojdbc7.jar) to my application dependency. Increasing memory does not help. Any ideas?

Posted by guest on March 27, 2014 at 11:21 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Bruno has been having fun working with Java since 2000 and now helps Oracle on sharing the technology accross all Latin America. Also plays videogames, does trekking and loves beer.

Follow me on Twitter! @brunoborges


« June 2016