Tuesday Apr 09, 2013

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!

 

 

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 2013 »
SunMonTueWedThuFriSat
 
1
2
3
4
5
6
7
8
11
12
13
14
15
16
17
18
19
20
21
23
24
25
26
27
28
29
30
    
       
Today