Home > Java > How to use embedded glassfish maven plugin?

How to use embedded glassfish maven plugin?

You already know how to use embedded glassfish from junit now it’s time to show you how to use the maven plugin to achieve the same.

You can download the full package here.

As always we start off with pom.xml

<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>coralic</groupId>
	<artifactId>ejbembeddedtest</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>embeddedtest</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.glassfish.extras</groupId>
			<artifactId>glassfish-embedded-all</artifactId>
			<version>3.0</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.glassfish</groupId>
				<artifactId>maven-embedded-glassfish-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<serverID>server</serverID>
					<name>server</name>
					<app>${project.build.directory}/${build.finalName}.war</app>
					<port>8888</port>
					<instanceRoot>${project.build.directory}/gfe-${maven.build.timestamp}
			</instanceRoot>
					<autoDelete>true</autoDelete>
					<configFile>${basedir}/domain.xml</configFile>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Then we place an web.xml in src/main/webapp/WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>EmbeddedGlassfishMavenPlugin</display-name>
  <welcome-file-list>
    <welcome-file>hello.html</welcome-file>
  </welcome-file-list>
</web-app>

Infortenatyl for this type of server we again need an domain.xml (use the one here) and copy next to pom.xml file.

Only thing left to do is create an hello.html file in src/main/webapp

<html>
  <head>
    <title>Tutorial: HelloWorld</title>
  </head>
  <body>
    <h1>HelloWorld Tutorial</h1>
  </body>
</html>

To start the embedded glassfish server do

mvn package embedded-glassfish:run

If you change something to hello.html while the embedded server is running you need to package and redeploy the application. How to do so, leave the embedded server running. Change something to hello.html. Open another commandline and fire up the following maven comamnd

mvn package

Now go back to your commandline where the embedded server is running and press enter. Your application will be re-deployed.

To test your hello.html brows to http://localhost:8888/server

Share
  1. Luk Vervenne
    May 16th, 2011 at 09:29 | #1

    Hello Armin,

    I’ve lost some money for my current research project at synergetics, but perhaps you are willing to work together on this idea? I have some interesting idea, that could make us rich if we join forces.

    Hope to hear from you soon,

    Luk

  2. vasile
    February 15th, 2012 at 06:03 | #2

    Hi Armin,

    Nice sample.
    It works everything fine with the version 3.0 of the maven-embedded-glassfish-plugin.
    I just tryed to update the sample to the version 3.1.1 to use the glassfish release 3.1.1.
    The I get a strange error:
    15.02.2012 10:58:46 com.sun.enterprise.v3.server.DomainXmlPersistence getPidFile

    ERROR: Cannot obtain lockfile location T:\workspace2\web-app\
    target\glassfish-20120215-1058\config\lockfile, configuration changes will not b
    e persisted
    java.io.IOException:The System cannot find the specified path
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)

    My maven configuration is :

    org.glassfish
    maven-embedded-glassfish-plugin
    3.1.1

    embedded-glassfish
    localhost
    ${build.finalName}
    ${project.build.directory}/${build.finalName}.war
    8080

    ${project.build.directory}/glassfish-${maven.build.timestamp}

    true
    ${basedir}/src/test/resources/domain_eclipse.xml

    start-glassfish
    pre-integration-test

    start

    glassfish-deploy
    pre-integration-test

    deploy

    glassfish-undeploy
    post-integration-test

    undeploy

    stop-glassfish
    post-integration-test

    stop

    Of course I am using another domain.xml file, the default one after installing the glassfish 3.1.1.
    Any idea what is wrong ?

    Thanks,
    Vasile

  3. vasile
    February 24th, 2012 at 13:13 | #3

    Instead of instance root in the new plugin version you must use:

    glassfish.embedded.tmpdir=target/glassfish

    the instanceroot you can use only with an existing installation.

  1. No trackbacks yet.