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=https://nexus.codehaus.org/content/repositories/snapshots/ -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
    <plugin>
           <groupId>org.glassfish.embedded</groupId>
             <artifactId>maven-embedded-glassfish-plugin</artifactId>
             <version>3.1.2.2</version>
             <configuration>
                <app>target/${project.artifactId}-${project.version}</app>
                <port>8282</port>
                <contextRoot>${project.artifactId}</contextRoot>
             </configuration>
             <dependencies>
               <dependency>
               <groupId>org.glassfish.main</groupId>
               <artifactId>simple-glassfish-api</artifactId>
               <version>4.0-b79</version>
             </dependency>
             <dependency>
               <groupId>org.glassfish.main.extras</groupId>
               <artifactId>glassfish-embedded-all</artifactId>
               <version>4.0-b83</version>
             </dependency>
           </dependencies>
         </plugin>
  4. UPDATE 04-10-2013: Don't forget to add the following pluginRepository configuration inside your POM
       <pluginRepositories>
            <pluginRepository>
                <id>maven.java.net</id>
                <name>Java.net Repository for Maven</name>
                <url>https://maven.java.net/content/groups/promoted/</url>
            </pluginRepository>
            <pluginRepository>
                <id>maven2-repository.dev.java.net</id>
               <name>Java.net Repository for Maven</name>
                <url>http://download.java.net/maven/glassfish/</url>
            </pluginRepository>
        </pluginRepositories>
  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!

 

 

Comments:

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=https://nexus.codehaus.org/content/repositories/snapshots/ -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] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO]
[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] BUILD FAILURE
[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]
[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]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

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

And with version 3.1.2.2 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
About


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

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today