March 23rd, 2010 by Lincoln Baxter III

Using Embedded Glassfish with Maven

Three steps to add Embedded GlassFish to any existing project POM. The Embedded Glassfish project is hosted on java.net, and as it is described, is very simple: “This project develops APIs and tools around “embedded GlassFish v3″. Embedded GFv3 is a delivery vehicle of GFv3 so that applications and tools can use GFv3 just as a library, inside their JVM, instead of having GFv3 as the overload of the entire JVM process. (And no, embedded GFv3 is not meant to be run on JavaME.)”

1. You’ll need to add this plugin repository to your POM:

        <pluginRepositories>
		<pluginRepository>
			<id>Glassfish</id>
			<url>http://download.java.net/maven/glassfish/</url>
		</pluginRepository>
	</pluginRepositories>

2. Then add the following plugin to your POM:

          <build>
                 <plugins>
			<plugin>
				<groupId>org.glassfish</groupId>
				<artifactId>maven-embedded-glassfish-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<goalPrefix>glassfish</goalPrefix>
					<app>test.war</app>
					<port>8080</port>
					<contextRoot>test</contextRoot>
				</configuration>
				<executions>
					<execution>
						<phase>install</phase>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
                </plugins>
         </build>

3. Commands

  • embedded-glasfish:run – starts the embedded server if it is not already running and deploys the specfied application. The goal keeps the server running, so the user can make changes in the application, reploy and test iteratively.
  • embedded-glassfish:start – starts the embedded server with the specified parameters
  • embedded-glassfish:deploy – deploys the specified application
  • embedded-glassfish:undeploy – undeploys the application identified by the “name” parameter. If no name is specifed all applications are undeployed.
  • embedded-glassfish:stop – stops the embedded server
  • embedded-glassfish:admin – executes the specified admin command on the embedded server. For example, you could create a jdbc resource.

Reference:

http://blogs.sun.com/sirajg/entry/using_maven_plugin_for_v3
Lincoln Baxter, III

About the author:

Lincoln Baxter, III is the Chief Editor of Red Hat Developers, and has worked extensively on JBoss open-source projects; most notably as creator & project lead of JBoss Forge, author of Errai UI, and Project Lead of JBoss Windup. This content represents his personal opinions, not those of his employer.

He is a founder of OCPsoft, the author of PrettyFaces and Rewrite, the leading URL-rewriting extensions for Servlet, Java EE, and Java web frameworks; he is also the author of PrettyTime, social-style date and timestamp formatting for Java. When he is not swimming, running, or playing competitive Magic: The Gathering, Lincoln is focused on promoting open-source software and making technology more accessible for everyone.

Posted in Java, OpenSource

11 Comments

  1. Looks simple enough for sure! So how can I run unit tests against this embedded glassfish? Or is that where Arquillian comes in?

  2. Lincoln says:

    That’s where Arquillian comes in 🙂

    Arquillian Examples:
    http://community.jboss.org/docs/DOC-14496

  3. Gene De Lisa says:

    Glad I got you started on a new post.

    This works ok but here’s a few observations.

    It requires a war, so you have to say
    mvn package embedded-glassfish:run
    I see that the config element has as a child. Would this be the way to run exploded?

    The only way I can tell how to set up a datasource is like this in the config element.

    ${basedir}/domain.xml

    Is there an easier way?

    To run Selenium during integration tests you have to do a bit more with the executions. Like this (for just the gf part. you have to add similar executions for Selenium):

    start-glassfish
    pre-integration-test

    start

    glassfish-deploy
    pre-integration-test

    deploy

    glassfish-undeploy
    post-integration-test

    undeploy

    stop-glassfish
    post-integration-test

    stop

    And finally this should be in a profile; especially since you’re about to do a JBoss one right?

    It would be nice if this plugin were better documented.

    1. Gene De Lisa says:

      your blog eats XML – as you can tell by the executions.

      The config child I meant was
      instanceRoot in regard to running exploded

      1. Lincoln says:

        Actually you can also wrap the text in

        <pre>code here<//pre>

        (just one closing /, had to prevent the nested pre tag from being stripped 😉

      2. Lincoln says:

        If you re-post your comment in a ‘pre’ tag, I’ll fix this one.

  4. Lincoln says:

    Yeah, you have to URLencode it. Sorry. I should really add a warning. I don’t really have too much insight on embedded glassfish, since I mostly use JBoss AS6 right now.

    I just went, found a blog, got it working, and posted it.

  5. admin says:

    Test

    public class Test {
     
    }
  6. Alberto Gori says:

    I know that you are the main developer of the Seam faces module.
    Could you explain how to setup a good integration test for JSF using Arquillian?

    For example, I’d like to test what happen when the client requests “/home.jsf?id=3”.
    For what you know, is actually possible?

  7. Stas says:

    Hi!

    I couldn’t get work JSPs hot-swapping. What are the main points to take into consideration?

    Here is my configuration

     
        org.glassfish
        maven-embedded-glassfish-plugin
        3.0.1
     
            ${project.build.directory}
            embedded-glassfish
            target/myapp.war
            8080
            myapp
            true
  8. War file not generated says:

    Configured as stated in your post but when executing "mvn -e clean install" then maven throws a LifecycleExecutionException with caused by: java.io.FileNotFoundException: test.war. Looking on the file system the war-file "test.war" wasn’t even created. If I change to version 3.1 then no exception is thrown but
    > PlainTextActionReporterFAILUREFile not found : /Users/grudom/Daten/IDE/EclipseProjects/workspace/transact/test.war06.09.2012 09:18:03 PluginUtil doDeploy
    > INFO: Deployed null
    Why is no war file being generated?

Leave a Comment




Please note: In order to submit code or special characters, wrap it in

[code lang="xml"][/code]
(for your language) - or your tags will be eaten.

Please note: Comment moderation is enabled and may delay your comment from appearing. There is no need to resubmit your comment.