Building and Deploying Java EE EAR with Maven to Java EE Application Server (Part 3) – Deployment To Glassfish 3.x

In Part 1 and Part 2 of this series, we have discussed about the directory layout and the Java EE demo project source codes that we had and further to be used to demonstrate the whole Java EE project development and deployment through Maven. From this part onwards, I will only be writing Application Server specific deployment configuration in Maven (using the contents from Part 1 and Part 2), at the same time, utilizing various plugins that are available. In this part, it is specifically about the deployment of the demo Java EE 5 project to Glassfish Ver 3.x Application Server. Again, for folks how came to this article directly, I would highly suggest that you read both Part 1 and Part 2 of this series to know what’s had been going on.

Warning On Compatibility:

The contents outlined in the article are NOT compatible with Glassfish 2.x.

Out of all the Java EE Application Servers, there is a reason why I would like to first address the Glassfish App Server deployment: Glassfish, though it had been made publicly available and had been widely used by many developers and companies; it lacks much support for Maven at the time this article was written, especially in the area of dependencies resolution and documentation in this aspect. The number of questions appeared in was quite overwhelming. Here, I will just document what worked for me and I believe the contents written here should be applicable to most of the development and deployment scenario for Glassfish 3.x.

Here, we will only be working on the manually installed standalone version of Glassfish and NOT the embedded version. I do understand that many developers had find the embedded version convenient for development and it has ts merits, but it is not the objective of this article as in my believe, it is better to run the codes with the version that always matches the version in the production environment as close as possible. At the date of writing, the Glassfish version used was


Glassfish 3.x Application Server Setup Brief

Let’s start with a brief explanation on how the App Server will be setup. Please do not be hasty to perform the below, we’ll go through this step-by-step:

  • We’ll create a separate domain call “DummyDemoDomain” just for this demo.
  • The database used as written in the previous parts is MySQL. You may please download the JDBC driver jar file for this demo.
  • The JDBC, JMS and other resources that is needed to be configured in the App Server will be placed in the glassfish-resources.xml file. We’ll go through this later.

Using & Configuring The Maven-Glassfish-Plugin

In order to deploy the EAR to the GLassfish 3.x App Server, we need the Maven-Glassfish-Plugin. This plugin might not be constantly maintained, but it is still usable. You may go to the plugin’s website and make a detail study of it, but anyway, here’s how to have the plugin configured with the very comprehensive config options which could be used in most deployment scenario. We’ll define the plugin in the DummyDemo-ear’s pom.xml file.

Please edit/replace the <path>/DummyDemoavaEE5/project/DummyDemo-ear/pom.xml to the below:

pom.xml Explained:
Looking at the new pom.xml, I have defined a property at the top called, with the value that points to the absolute path of the Glassfish App Server directory. Please change this value accordingly. Moving down to the plugin section, both the gf-deploy and gf-undeploy plugin goals will only be executed during the package (I want to make it deployed to the application server just after packing everything to an EAR file) and the clean build cycle respectively.

As for the configuration section, there are many options to be configured, which you can find them at the plugin page, but as for now, the above is sufficient. However, do take note on a few options listed below:

Option Description
user The user that could access to the Glassfish App Server, typically the admin. I have configured my Glassfish appserver’s administrator username as “admin“.
glassfishDirectory The absolute path to the Glassfish App Server directory. I’ll use the property ${} defined above.
domainDirectory The directory that stores the domain specific files, typically will be located at “${}/glassfish/domains“. Just change this accordingly.
passwordFile The absolute path to the Glassfish password file. The contents within the password file is shown below as you read on. I choose to use the password file as it could have the master password defined, which is crucial for domain creation.
domain -> name The domain name that is used for domain creation, running and deployment.
components -> component -> name The preferred name of the deployed application
components -> component -> artifact The absolute or relative path to the EAR file that is needed to be deployed.

The options of the ports are self explainable. So, basically, this is what the plugin section should look like.

The Glassfish Password File

The Glassfish Password File is necessary for this deployment (if not, compulsory), as it allow the storing of the master password. At the time of writing, I have tried doing this without the password file, but the plugin complained about the missing master password and it couldn’t move on. On my side, I had great difficulty in feeding the master password to the plugin through normal means. But unfortunately the only way the plugin could work correctly is by the use of password file with a master password defined. Bugs bugs bugs…

Anyway, the below are the contents of the password file. Please create a the file with the contents below:


Contents of glassfishpasswordfile:

The above are the passwords used in this demo. You may change them accordingly.

The Glassfish Resources File

The good thing about Glassfish is it allows you to add the necessary app server resources through loading/parsing an XML file, instead of configuring the resources through the admin console. The resources used are shown below. Please create the glassfish-resources.xml file at the below path and write the contents below into the file:


Contents of glassfish-resources.xml:

glassfish-resources.xml Explained:
In the glassfish-resources.xml, I have just defined the database connection, the Queue and the Queue Connection Factory, which is sufficient for the demo application. The database connectivity parameters are at the <jdbc-connection-pool /> section. Please change the property values accordingly. The Queue and Queue Connection Factory are both defined in the <onnector-connection-pool /> and the <admin-object-resource /> section. Please take note of the JNDI name, it must match the annotation in the MDB and vice-versa.

The Right Dependency For Integration Test

Before we go ahead with the deployment and integration test, since we are running the app on Glassfish, there are a few tweaks to the dependency used in the integration-test module as published in Part 2 of the series.

Many developers had faced issues with getting the right dependency working with Glassfish for their JMS, naming context environment, etc. and sad to say, there ain’t “component-ized” or “nice and lean” Maven artifacts/dependencies available for the Glassfish environment. Some developers have to manually insert the imqbroker.jar or the imqjmsra.jar taken from Glassfish to Maven’s Repository as an artifact, just to get things working.

In my opinion, there is no right or wrong way to this. But for conveniences sake, I’ll just use glassfish-embedded-all Maven dependency as a library for the integration test codes for the integration-test module. You may insert this in the <path>/DummyDemoJavaEE5/integration-test/pom.xml file (the file size is more than 60MB):

You’ve seen it correctly. It is the Embedded Glassfish’s Library that could solve much of the dependency hassle. This is by far, the most painless approach that I could have right now. A word of caution, please do check on the compatibility of this dependency version against the actual development or production Glassfish Application Server in any future usage of this.

The contents of <path>/DummyDemoJavaEE5/integration-test/pom.xml should be like the below:

Making Things Happen Step-By-Step

Once you’ve gotten all of the above configured, it is time to put the plugin in good use. We’ll perform steps listed below.

Step 1: Creating the Domain in Glassfish through Maven-Glassfish-Plugin (DummyDemoDomain)

The DummyDemo project will run on a new domain call DummyDemoDomain. To create the domain, just follow the steps below (you may need to open two or more terminal/command prompt, depending on your style of approach):

  1. Change the working directory to <path>/DummyDemoJavaEE5/project/DummyDemo-ear/.
  2. Execute the command: “mvn glassfish:create-domain” and wait for Maven to do its job.
  3. Once the domain was created, remember to place the JDBC driver jar file into the <path_to_glassfish>/glassfish/domains/DummyDemoDomain/lib/ext/ directory. This is crucial before you start the App Server later.

Step 2: Starting the DummyDemoDomain

To start the newly created domain, follow the below:

  • While still at the working directory of <path>/DummyDemoJavaEE5/project/DummyDemo-ear/, execute the command “mvn glassfish:start-domain“. This will start the DummyDemoDomain.

Step 3: Add Resources to the DummyDemoDomain

This is the time to use the glassfish-resources.xml that you have prepared earlier. To have the resources added into Glassfish:

  1. Change the working directory to <path_to_glassfish>/bin/.
  2. Execute the following command:
    asadmin add-resources "<path>/DummyDemoJavaEE5/project/DummyDemo-ear/ \

    It will ask you for the administrator’s password, just type the password to proceed.

  3. When the resources were added, it will show you something like:
    Command : JDBC connection pool DummyDemoPool created successfully.
    Command : JDBC resource jndi/DummyDemoMySQLDB created successfully.
    Command : Connector connection pool TestQueueConnectionFactoryPool created.
    Command : Administered object jms/TestQueue created.
    Command : Connector resource jms/TestQueueConnectionFactory created.
    Command add-resources executed successfully.

Step 4: Deploying the EAR File to the Glassfish Application Server

This is a very “sensitive” step and the failure rate is usually high. If anything goes wrong, I would highly suggest that you revisit Step 1 to Step 4 or back track Part 1 and Part 2 of the series. Anyway, to deploy the EAR file, follow the steps below:

Full Deployment Including Running The Integration Test

  1. Please change the working directory to <path>/DummyDemoJavaEE5/.
  2. Execute the “mvn install” command. This command will start the Glassfish domain (if it is not started) and it will deploy the EAR to the application server, and then, it will run the integration tests within the integration-test module.
  3. Just wait for Maven to download the dependencies through the internet. But if things broke down due to network failure or if you have difficult resolving dependencies, I’ll use the “-U” flag to perform this again, e.g. “mvn install -U
  4. If it deploys properly, you should be able to see the below in the terminal:
    [INFO] Reactor Summary:
    [INFO] DummyDemoJavaEE5 .............................. SUCCESS [0.233s]
    [INFO] project ....................................... SUCCESS [0.016s]
    [INFO] DummyDemo-api ................................. SUCCESS [2.436s]
    [INFO] DummyDemo-ejb ................................. SUCCESS [1.683s]
    [INFO] DummyDemo-web ................................. SUCCESS [1.043s]
    [INFO] DummyDemo-appclient ........................... SUCCESS [1.674s]
    [INFO] DummyDemo-ear ................................. SUCCESS [5.131s]
    [INFO] integration-test .............................. SUCCESS [19.942s]
    [INFO] -----------------------------------------------------------------
    [INFO] -----------------------------------------------------------------

If you have successfully completed this step, CONGRATULATIONS, your EAR file had been deployed! You may access the admin console of Glassfish to check on the deployment status.

For Development Deployment (Without Running The Integration Test)

During development, we don’t need to always run the integration test for every single deployment, to achieve this while at your development with the Glassfish running, just perform the following:

  1. Change the working directory to <path>/DummyDemoJavaEE5/project/.
  2. For first time deployment of the EAR, just execute the command “mvn package” or “mvn install“.

Step 5: How to Undeploy or Redeploy the EAR to Glassfish

In the midst of development, there will always be time when we need to redeploy the application again and again. To do that, just execute this at the <path>/DummyDemoJavaEE5/project/ working directory:

Just use the command: “mvn clean“. Since the the “undeploy” plugin goal was tagged with “clean” build-phase (just check on <path>/DummyDemoJavaEE5/project/DummyDemo-ear/pom.xml), when ever you execute the “clean” build cycle, it will perform the undeployment.

** Please take note that this command will fail if there is nothing to be undeploy on the Glassfish App Server.

To RE-DEPLOY (only when there is an existing deployment in the Glassfish app server):
Execute “mvn clean install” or “mvn clean package” at the terminal.

Summary For Part 3

I sincerely hope that after reading Part 1, Part 2 and Part 3 of this series, you are able to get your project deploy and tested successfully to Glassfish with Maven. In fact, getting the deployment done right is not much of a hassle, but rather getting the integration test module dependencies right is quite a challenge for developers whom had tried. Dependencies are not quite available in the general public Maven Repositories. I’m guessing that it might be due to licensing issues from Oracle or they are just simply lacking of staff to maintain the Maven side of things for Glassfish.

To Be Continue…

From here, I’ll be posting more of the deployment configuration for other Application Servers. JBoss and WebLogic will be next couple of articles, so stay tune.

Proceed To Part 4:

Building and Deploying Java EE EAR with Maven to Java EE Application Server (Part 4) – Deployment To JBoss AS 5.x & JBoss AS 6.x

Related Articles:

Born and currently resides in Malaysia, a seasoned Java developer whom had held positions as Senior Developer, Consultant and Technical Architect in various enterprise software development companies.