Simple Jersey Web App With Maven Take 2: Replacing Tomcat With GlassFish

Yesterday, i wrote about how to put together a simple Jersey based RESTful web application
using just CLI and maven. I have ended with running the application using mvn tomcat:run. In this entry i would like to show, ho to easily update the pom.xml file, so that you can use


mvn glassfish:run

instead. I.e. how to run the thing on GlassFish V3 application server instead of Tomcat.

Updating Your pom.xml


Following is the updated pom.xml. Newly added pieces are red:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.example.com</groupId>
<artifactId>SimpleRESTApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SimpleRESTApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>glassfish-repository</id>
<name>Java.net Repository for Glassfish</name>
<url>http://download.java.net/maven/glassfish</url>
</repository>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven2.java.net</id>
<name>Java.net Repository for Maven 2</name>
<url>http://download.java.net/maven/2</url>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey</artifactId>
<version>0.8-ea-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.glassfish</groupId>
<artifactId>maven-glassfish-plugin</artifactId>
<version>1.0-alpha-4</version>
</plugin>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
<finalName>SimpleRESTApp</finalName>
</build>
</project>

A simple update, isn't it?

Running The WebApp

Now you can try to type:


mvn glassfish:run

and will get:


[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'glassfish'.
[INFO] ------------------------------------------------------------------------
[INFO] Building SimpleRESTApp Maven Webapp
[INFO] task-segment: [glassfish:run]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing glassfish:run
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [glassfish:run]
Jun 3, 2008 10:55:20 AM com.sun.enterprise.v3.server.AppServerStartup run
INFO: HK2 initialized in 316 ms
Jun 3, 2008 10:55:20 AM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.naming.impl.ServicesHookup@16089a5 Init done in 323 ms
Jun 3, 2008 10:55:20 AM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.server.Globals@15fb38 Init done in 325 ms
Jun 3, 2008 10:55:20 AM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.server.SystemTasks@878c4c Init done in 330 ms
Jun 3, 2008 10:55:20 AM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.services.impl.HouseKeeper@8f9a32 Init done in 333 ms
Jun 3, 2008 10:55:20 AM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.services.impl.CmdLineParamProcessor@1581e80 Init done in 336 ms
JMXMP connector server URL = service:jmx:jmxmp://localhost:8888
Jun 3, 2008 10:55:21 AM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 8080
Jun 3, 2008 10:55:21 AM com.sun.enterprise.v3.server.AppServerStartup run
INFO: com.sun.enterprise.v3.services.impl.GrizzlyService@5dfaf1 startup done in 962 ms
Jun 3, 2008 10:55:21 AM com.sun.enterprise.v3.services.impl.ApplicationLoaderService postConstruct
INFO: loader service postConstruct started at 1212483321145
Jun 3, 2008 10:55:21 AM com.sun.enterprise.v3.server.AppServerStartup run
INFO: Application Loader startup done in 993 ms
Jun 3, 2008 10:55:21 AM com.sun.enterprise.v3.server.AppServerStartup run
INFO: Glassfish v3 started in 994 ms
Jun 3, 2008 10:55:25 AM com.sun.enterprise.web.WebModuleContextConfig authenticatorConfig
SEVERE: webModuleContextConfig.missingRealm
Jun 3, 2008 10:55:25 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
com.example.my
Jun 3, 2008 10:55:25 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Root resource classes found:
class com.example.my.SimpleResource
Jun 3, 2008 10:55:25 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Provider classes found:
Hit ENTER for redeploy

Please note, that GlassFish has started in less than a second!

Testing The REST Resource

Now we can also test the web resource:


%curl -i http://localhost:8080/SimpleRESTApp/greeting
HTTP/1.1 200 OK
X-Powered-By: Servlet/2.5
Server: GlassFish/v3
Content-Type: text/plain
Transfer-Encoding: chunked
Date: Tue, 03 Jun 2008 09:01:08 GMT
Hi there

Comments:

Japod

Very useful. However for Glassfish V3 to be truly usable from Maven we need the Cargo plugin completed. See http://jira.codehaus.org/browse/CARGO-491. Kohsuke half did it, but it needs to be finished.

Without Cargo, you need to use something other than Glassfish for integration tests.

It looks tantalisingly close. I vote for you to finish it :)

Posted by Greg Luck on June 06, 2008 at 06:36 AM CEST #

Hi Greg, thanks. Regarding the Cargo plugin, it seems Vlado already finished the work.

Posted by Jakub on June 12, 2008 at 03:36 AM CEST #

How do I remove the "SimpleRESTApp" from the REST URL? The maven plugin uses the artifactID and I can't make it to use just / as the content root.

(The Jetty maven plugin uses contextPath)

Peace
-stephan

Posted by stephan on July 15, 2008 at 02:27 PM CEST #

When trying this it fails with the following error. The same project works fine with tomcat:run

PWC4430: Document base /Users/damianoneill/Documents/subversion/FS/sourceCode/test/src/main/webapp does not exist or is not a readable directory

Any ideas why would it work correctly with tomcat and not glassfish?

Thanks in advance

Posted by Damian ONeill on July 19, 2008 at 08:15 AM CEST #

On the previous post it seems that the glassfish plugin does not like being executed from the top level in a multiproject, which works fine with the tomcat plugin.

If I move into the web module and execute from here it works fine.

Posted by Damian ONeill on July 19, 2008 at 08:41 AM CEST #

Quiestion.
do i have to use grassfish server in a computer with maven?

Posted by peter kanduma on March 13, 2009 at 07:16 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Jakub Podlesak

Search

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